學習內容:文件權限管理及grep正則和擴展正則表達式linux
系統環境:CentOS 6.7/7 x86_64正則表達式
1、做業(練習)內容:shell
一、總結本此課程中所涉及命令的使用方法及相關示例展現; 二、總結基本正則表達式及擴展正則表達式 三、顯示/etc/passwd文件中以bash結尾的行 四、顯示/etc/passwd文件中的兩位數或三位數 五、顯示`netstat -tan`命令結果中以‘LISTEN’後跟0個、1個或者多個空白字符結尾的行 六、添加用戶bash、testbash、basher以及nologin用戶(nologin用戶的shell爲/sbin/nologin);然後找出/etc/passwd文件中用戶名與其shell名相同的行 七、顯示當前系統上root、centos或者user1用戶的默認shell和UID (請事先建立這些用戶,若不存在) 八、找出/etc/rc.d/init.d/functions文件中某單詞(單詞中間能夠存在下劃線)後面跟着一組小括號的行 九、使用echo輸出一個路徑,然後egrep找出其路徑基名;進一步的使用egrep取出其目錄名 十、找出ifconfig命令執行結果中1-255之間的數字
2、完成時間:express
2015年9月3日上課以前centos
3、總結與練習bash
1、總結本此課程中所涉及命令的使用方法及相關示例展現;tcp
(1)Linux文件系統文件權限說明編輯器
用戶對文件的訪問權限有三種:rwxide
對文件:工具
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 total 72 -rw-------. 1 root root 1399Aug 27 18:44 anaconda-ks.cfg -rw-r--r--. 1 root root 50433 Aug 27 18:44 install.log -rw-r--r--. 1 root root 10033 Aug 27 18:41 install.log.syslog [root@MyTest-67 ~]# chmod --reference=./install.log anaconda-ks.cfg [root@MyTest-67 ~]# ll total 72 -rw-r--r--. 1 root root 1399Aug 27 18:44 anaconda-ks.cfg -rw-r--r--. 1 root root 50433 Aug 27 18:44 install.log -rw-r--r--. 1 root root 10033 Aug 27 18:41 install.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 ~]# touch b.txt [root@MyTest-67 ~]# ll b.txt -rw-r--r--. 1 root root 0 Aug 27 23:42 b.txt [root@MyTest-67 ~]# su - centos su: user centos does not exist [root@MyTest-67 ~]# useradd centos [root@MyTest-67 ~]# su - centos [centos@MyTest-67 ~]$ touch centos [centos@MyTest-67 ~]$ ll centos -rw-rw-r--. 1 centos centos 0 Aug 27 23:42 centos [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 ~]# cat grep.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:FTP User:/var/ftp:/sbin/nologin
5、顯示`netstat -tan`命令結果中以‘LISTEN’後跟0個、1個或者多個空白字符結尾的行
[root@MyTest-C67 ~]# netstat -tan| grep"LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:44907 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.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]\>" eth0 Link encap:Ethernet HWaddr00:0C:29:08:4F:6A inet addr:192.168.31.200 Bcast:192.168.31.255 Mask:255.255.255.0