2015年8月27日【文件权限管理及grep正则和扩展正则表达式】-JY15
学习内容:文件权限管理及grep正则和扩展正则表达式 系统环境:CentOS 6.7/7 x86_64 一、作业(练习)内容: 1、总结本此课程中所涉及命令的使用方法及相关示例展示; 2、总结基本正则表达式及扩展正则表达式 3、显示/etc/passwd文件中以bash结尾的行 4、显示/etc/passwd文件中的两位数或三位数 5、显示`netstat-tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行 6、添加用户bash、testbash、basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行 7、显示当前系统上root、centos或者user1用户的默认shell和UID(请事先创建这些用户,若不存在) 8、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行 9、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名 10、找出ifconfig命令执行结果中1-255之间的数字 二、完成时间: 2015年9月3日上课之前 三、总结与练习 1、总结本此课程中所涉及命令的使用方法及相关示例展示; (1)Linux文件系统文件权限说明 用户对文件的访问权限有三种:rwx 对文件: r:可使用文本查看工具查看其内容; w:可使用文件编辑工具其内容; x:可向内核请求将此文件运行为进程; 对目录: r:可使用ls命令列出目录中的文件或子目录列表; w:可以在此目录中创建或删除文件; x:可使用’ls -l’列出目录文件及子目录的详细属性信息;可使用cd命令切换工作目录为指定目录; 文件权限分为属主、属组和其他用户,分别用u:owner属主,g:group属组,o:other其他; a:all所有 某类用户对某文件的访问权限如下: --- 000,0 --x001,1 -w-010,2 -wx011,3 r--100,4 r-x101,5 rw-110,6 rwx111,7 664:rw-rw-r-- rwxr-x---:750 (2)权限管理相关命令 chmod 权限管理 常用选项: -R 递归修改目录中所有文件的权限 --reference=file 设置修改权限的文件与file相同 两种用法: =rwx 例:=rx, [+|-]rwx 例:u+w,a-x 例:--reference=file参数的使用 [root@MyTest-67~]#ll total72 -rw-------.1rootroot1399Aug2718:44anaconda-ks.cfg -rw-r--r--.1rootroot50433Aug2718:44install.log -rw-r--r--.1rootroot10033Aug2718:41install.log.syslog [root@MyTest-67~]#chmod--reference=./install.loganaconda-ks.cfg [root@MyTest-67~]#ll total72 -rw-r--r--.1rootroot1399Aug2718:44anaconda-ks.cfg -rw-r--r--.1rootroot50433Aug2718:44install.log -rw-r--r--.1rootroot10033Aug2718:41install.log.syslog chown,chgrp 所属关系管理 chown - 更改文件或目录的属主或属组 -R 递归 用法: chown centos file #更改属主 chown [.|:]mygrp file #更改属组 chown centos[.|:]mygrp file #同时更改属主和属组 chgrp - 更改文件或目录的属组 umask 文件遮罩码 目录:777-umask 文件:666-umask 说明:如果某一类用户的权限减得的结果有执行权限,此时会自动让其权限位加1. umask UMASK #设定umask:仅对当前shell进程有效。 [root@MyTest-67~]#touchb.txt [root@MyTest-67~]#llb.txt -rw-r--r--.1rootroot0Aug2723:42b.txt [root@MyTest-67~]#su-centos su:usercentosdoesnotexist [root@MyTest-67~]#useraddcentos [root@MyTest-67~]#su-centos [centos@MyTest-67~]$touchcentos [centos@MyTest-67~]$llcentos -rw-rw-r--.1centoscentos0Aug2723:42centos [centos@MyTest-67~]$umask 0002 [centos@MyTest-67~]$exit logout [root@MyTest-67~]#umask 0022 2、总结基本正则表达式及扩展正则表达式 grep: Linux文件处理三剑客: grep:文件过滤工具; sed:文本编辑器(行);streameditor awk:文本报告生成器:linux上awk的实现为gawk grep:Global search REgular expression and Print out the line. 作用:文本搜索工具,根据用户指定的“模式(pattern)”逐行去搜索目标文本,打印匹配到的行; 模式:由正则表达式的元字符及文本字符所编写的过滤条件; 元字符:字符不表示其字面意义,而用于表示通配或控制功能; 分两类: 基本正则表达式:BRE 扩展正则表达式:ERE 正则表达式引擎: grep [OPTIONS] PATTERN [FILE...] 选项: --color=auto:对匹配到的串做高亮显示 -v:显示模式匹配不到行; -i:忽略字符大小写; -o:仅显示能够被模式匹配到的串本行; -q:静默模式; -E:使用扩展的正则表达式; 基本正则表达式的元字符: 字符匹配: .:匹配任意单个字符; []:匹配指定范围内的任意单个字符; [^]:匹配指定的范围以外的任意单个字符; [:lower:],[:upper:],... 次数匹配:用于要指定其次数的字符的后面: *:任意次; abxy xay xxxxxxxxxy grep "x*y" ?:0或1次; grep "x?y" +:1或多次; {m}:精确限制为m次; {m,n}:至少m次,至多n次,[m,n] {0,n}:至多n次; {m,}:至少m次 .*:匹配任意长度的任意字符; 位置锚定: ^:行首锚定;用于模式的最左侧; $:行尾锚定;用于模式的最右侧; &;,b:词首锚定;用于表示单词的模式的左侧; &;,b:词尾锚定;用于表示单词的模式的右侧; ^$:空白行; 分组:() 分组的小括号中的模式匹配到的内容,会在执行过程中被正则表达式引擎记录下来,并保存内置的变量中,这些变量分别是1,2,... 1:从左侧起,第一个左括号,以及与之配对的左括号中间的模式所匹配到的内容; 2: ... 后向引用:使用变量引用前面的分组括号中的模式所匹配到的字符; [root@MyTest-C67~]#grep'r..t.*r..t'grep.txt root:x:34:43:/root:/bin/root root:x:56:67:/rrrt:/bin/raat [root@MyTest-C67~]#catgrep.txt abxyxyxyab xayabxyabxy xyvbxy root:x:34:43:/root:/bin/root bash:x:45:56:/home/bash:bin/tcsh root:x:56:67:/rrrt:/bin/raat [root@MyTest-C67~]#grep'r..t.*r..t'grep.txt root:x:34:43:/root:/bin/root root:x:56:67:/rrrt:/bin/raat [root@MyTest-C67~]#grep'(r..t).*1'grep.txt root:x:34:43:/root:/bin/root 3、显示/etc/passwd文件中以bash结尾的行 [root@MyTest-67~]#grep"bash$"/etc/passwd root:x:0:0:root:/root:/bin/bash 4、显示/etc/passwd文件中的两位数或三位数 [root@localhost~]#grep"[0-9]{2,3}"/etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin 5、显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行 [root@MyTest-C67~]#netstat-tan|grep"LISTEN[[:space:]]*$" tcp000.0.0.0:449070.0.0.0:*LISTEN tcp000.0.0.0:1110.0.0.0:*LISTEN tcp000.0.0.0:220.0.0.0:*LISTEN tcp00127.0.0.1:6310.0.0.0:*LISTEN tcp00127.0.0.1:250.0.0.0:* 6、添加用户bash、testbash、basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行 [root@MyTest-C67~]#tail-4/etc/passwd bash:x:3004:3004::/home/bash:/bin/bash testbash:x:3005:3005::/home/testbash:/bin/bash basher:x:3006:3006::/home/basher:/bin/bash nologin:x:3007:3007::/home/nologin:/sbin/nologin [root@MyTest-C67~]#grep"(bash).*1"/etc/passwd bash:x:3004:3004::/home/bash:/bin/bash testbash:x:3005:3005::/home/testbash:/bin/bash basher:x:3006:3006::/home/basher:/bin/bash 正确方法: [root@MyTest-C67~]#grep"^([[:alnum:]]{1,})&;.*1$"/etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt bash:x:3004:3004::/home/bash:/bin/bash nologin:x:3007:3007::/home/nologin:/sbin/nologin 7、显示当前系统上root、centos或者user1用户的默认shell和UID (请事先创建这些用户,若不存在) [root@MyTest-C67~]#grep-E"^&;(root|centos|user1)&;"/etc/passwd|cut-d:-f1,3,7 root:0:/bin/bash centos:3009:/bin/bash user1:3010:/bin/bash 8、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行 [root@MyTest-C67~]#grep-E"(&;[[:alpha:]]&;|[[:alpha:]]_[[:alpha:]]).*()"/etc/rc.d/init.d/functions fstab_decode_str(){ __umount_loop(){ __umount_loopback_loop(){ __pids_var_run(){ 9、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名 [root@MyTest-C67~]#echo"/etc/init.d/iptables"|egrep--color=auto-o"[^/]+/?$" iptables [root@MyTest-C67~]#echo"/etc/init.d/iptables"|egrep--color=auto-o".*/" /etc/init.d/ 10、找出ifconfig命令执行结果中1-255之间的数字 [root@MyTest-C67~]#ifconfig|grep-E--color=auto"&;[1-9]|[1-9][1-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5]&;" eth0Linkencap:EthernetHWaddr00:0C:29:08:4F:6A inetaddr:192.168.31.200Bcast:192.168.31.255Mask:255.255.255.0 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |