馬哥教育網絡班21期+第5周課程練習
[TOC]
1. 顯示/boot/grub/grub.conf中至少以一個空白字符開頭的行。
[root@rhel-5 ~]# grep -E '^[[:space:]]+' /boot/grub/grub.conf root (hd0,0) kernel /vmlinuz-2.6.18-348.el5 ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.18-348.el5.img [root@rhel-5 ~]# grep '^[[:space:]]\+' /boot/grub/grub.conf
2. 顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面至少一個空白字符,而后又有至少一個非空白字符的行。
[root@rhel-5 ~]# grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit
3. 打出netstat -tan
命令執行結果中以LISTEN后跟空白字符結尾的行。
[root@rhel-5 ~]# netstat -tan | grep 'LISTEN[[:space:]]*' tcp 0 0 127.0.0.1:2208 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:3938 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:6000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:1521 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:6010 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:734 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:2207 0.0.0.0:* LISTEN
4. 添加用戶bash,testbash,basher,nologin(此用戶的shell為/sbin/nologin),而后找出當前系統上用戶和默認shell相同的用戶信息。
[root@rhel-5 tuser1]# useradd bash [root@rhel-5 tuser1]# useradd testbash [root@rhel-5 tuser1]# useradd basher [root@rhel-5 tuser1]# useradd -s /sbin/nologin nologin [root@rhel-5 tuser1]# grep -E "(^[[:alpha:]]+):.*\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:502:504::/home/bash:/bin/bash nologin:x:505:507::/home/nologin:/sbin/nologin
5. 顯示當前系統上root,fedora或user1用戶的默認shell。
[root@rhel-5 ~]# grep -E '^(root|fedora|user1)' /etc/passwd root:x:0:0:root:/root:/bin/bash [root@rhel-5 ~]# grep -E '^(root|fedora|user1)' /etc/passwd | cut -d: -f7 /bin/bash
6. 找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello ()。
[root@rhel-5 ~]# grep "^\<[a-z]\+\>()" /etc/rc.d/init.d/functions checkpid() { daemon() { killproc() { pidfileofproc() { pidofproc() { status() { success() { failure() { passed() { warning() { action() { strstr() { confirm() { [root@rhel-5 ~]# grep -E '^\<[[:alpha:]]+\>\(\)' /etc/rc.d/init.d/functions
7. 使用echo
命令輸出一個絕對路徑,使用grep
取出其基名。
grep -oE "[^/]+/?$" | cut -d"/" -f 1
[root@rhel-6 rc.d]# echo /usr/appsoft/software/12.txt/ | grep -oE "[^/]+/?$" | cut -d"/" -f 1 12.txt [root@rhel-6 rc.d]# echo /usr/appsoft/software/12.txt | grep -oE "[^/]+/?$" | cut -d"/" -f 1 12.txt
7.1 擴展:取出其路徑名。
[root@rhel-6 rc.d]# echo /usr/appsoft/software/12.txt | grep -oE ".*/" /usr/appsoft/software/
8. 找出ifconfig
命令結果中的1-255之間的數字。
[root@rhel-5 ~]# ifconfig | grep -wE '([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])' [root@rhel-5 ~]# ifconfig | grep -wE '[1-9][0-9]?|1[0-9][0-9]|2[0-5][0-5]'
9. 挑戰題:寫一個模式,能匹配合理的IP地址
這里認為合理的IP為:[0-255].[0-255].[0-255].[0-255] grep -wE '(([0-1]?[0-9]?[0-9]|2[0-5]{2})\.){3}([0-1]?[0-9]?[0-9]|2[0-5]{2})'
10. 挑戰題:寫一個模式,能匹配所有的郵件地址。
grep -E '[[:alnum:]]+.*@[[:alnum:]]+\.[[:alnum:]]+\.?[[:alpha:]]+'
11. 查找/var目錄下屬主為root且屬組為mail的所有文件或目錄。
[root@rhel-5 ~]# find /var -user root -group mail /var/spool/mail /var/spool/mqueue [root@rhel-5 var]# find /var -user root -group mail -ls 3381291 8 drwxrwxr-x 2 root mail 4096 8月 1 13:44 /var/spool/mail 70581 8 drwx------ 2 root mail 4096 7月 28 2011 /var/spool/mqueue [root@rhel-5 var]# find /var \( -user root -group mail \) -ls 3381291 8 drwxrwxr-x 2 root mail 4096 8月 1 13:44 /var/spool/mail 70581 8 drwx------ 2 root mail 4096 7月 28 2011 /var/spool/mqueue
11.1 關于find
命令-ls
參數的用法總結
在
find
命令中使用了-a
(邏輯與)或-o
(邏輯或)時,如果還需要使用-ls
參數,則需要注意:
-a
: 使用邏輯與時,各條件是否使用()括起來均可,建議使用()括起來;
-O
: 使用邏輯或時,各條件必須使用()括起來,否則查找結果顯示錯誤。
-o
:若不使用(),一般認為會顯示最后一個邏輯條件的查找結果,但是實驗發現,最后一個邏輯條件的查找結果顯示也是錯誤的,所以邏輯或查找條件中,必須將各條件用()括起來。
示例:
-a
: 可以省略不寫
[root@rhel-5 var]# find /var -user oracle -group mail -ls 2763626 0 -rw-r--r-- 1 oracle mail 0 8月 1 14:48 /var/123.sh 3390953 0 -rw-rw---- 1 oracle mail 0 7月 29 15:34 /var/spool/mail/oracle [root@rhel-5 var]# find /var \( -user oracle -group mail \) -ls 2763626 0 -rw-r--r-- 1 oracle mail 0 8月 1 14:48 /var/123.sh 3390953 0 -rw-rw---- 1 oracle mail 0 7月 29 15:34 /var/spool/mail/oracle
-o
:該參數配合-ls
使用時,注意以下兩點:
第一條命令中,各邏輯條件不用()括起來,一般認為其只會顯示最后一個邏輯條件的結果,即 -group mail,但是/var/123.sh的屬組為mail,結果卻沒有顯示出來,所以顯示最后一個邏輯條件的查找結果的說法也是錯誤的。
find命令使用邏輯或進行條件查找時,各條件必須使用()括起來。
[root@rhel-5 var]# find /var -user oracle -o -group mail -ls 3381291 8 drwxrwxr-x 2 root mail 4096 8月 1 13:44 /var/spool/mail 3413767 0 -rw-rw---- 1 nologin mail 0 7月 31 19:26 /var/spool/mail/nologin 3413764 0 -rw-rw---- 1 bash mail 0 7月 31 19:26 /var/spool/mail/bash 3390134 4 -rw-rw---- 1 rpc mail 0 7月 29 10:01 /var/spool/mail/rpc 3413766 0 -rw-rw---- 1 basher mail 0 7月 31 19:26 /var/spool/mail/basher 3413765 0 -rw-rw---- 1 testbasher mail 0 7月 31 19:26 /var/spool/mail/testbasher 3413768 0 -rw-rw---- 1 testbash mail 0 8月 1 13:44 /var/spool/mail/testbash 3413763 0 -rw-rw---- 1 hadoop mail 0 7月 31 19:04 /var/spool/mail/hadoop 70581 8 drwx------ 2 root mail 4096 7月 28 2011 /var/spool/mqueue [root@rhel-5 var]# find /var \( -user oracle -o -group mail \) -ls 397605 0 srwxrwxrwx 1 oracle oinstall 0 7月 29 15:59 /var/tmp/.oracle/sEXTPROC1521 397606 0 srwxrwxrwx 1 oracle oinstall 0 7月 29 15:59 /var/tmp/.oracle/s#27699.2 397604 0 srwxrwxrwx 1 oracle oinstall 0 7月 29 15:59 /var/tmp/.oracle/s#27699.1 41919 16 -rw------- 1 oracle oinstall 15000 7月 29 15:35 /var/cache/coolkey/coolkeypk11sE-Gate\ 0\ 0-500 2763626 0 -rw-r--r-- 1 oracle mail 0 8月 1 14:48 /var/123.sh 3381291 8 drwxrwxr-x 2 root mail 4096 8月 1 13:44 /var/spool/mail 3413767 0 -rw-rw---- 1 nologin mail 0 7月 31 19:26 /var/spool/mail/nologin 3413764 0 -rw-rw---- 1 bash mail 0 7月 31 19:26 /var/spool/mail/bash 3390134 4 -rw-rw---- 1 rpc mail 0 7月 29 10:01 /var/spool/mail/rpc 3413766 0 -rw-rw---- 1 basher mail 0 7月 31 19:26 /var/spool/mail/basher 3413765 0 -rw-rw---- 1 testbasher mail 0 7月 31 19:26 /var/spool/mail/testbasher 3413768 0 -rw-rw---- 1 testbash mail 0 8月 1 13:44 /var/spool/mail/testbash 3413763 0 -rw-rw---- 1 hadoop mail 0 7月 31 19:04 /var/spool/mail/hadoop 3390953 0 -rw-rw---- 1 oracle mail 0 7月 29 15:34 /var/spool/mail/oracle 70581 8 drwx------ 2 root mail 4096 7月 28 2011 /var/spool/mqueue
12. 查找當前系統上沒有屬主或屬組的文件。
[root@rhel-5 var]# useradd ies [root@rhel-5 ies]# userdel ies [root@rhel-5 ies]# find / \( -nouser -o -nogroup \) -ls 3413769 0 -rw-rw---- 1 507 mail 0 8月 1 15:23 /var/spool/mail/ies 3348827 4 drwx------ 4 507 509 4096 8月 1 15:24 /home/ies 2048441 4 -rw-r--r-- 1 507 509 176 8月 1 15:23 /home/ies/.bash_profile 2048442 4 -rw-r--r-- 1 507 509 515 8月 1 15:23 /home/ies/.emacs 3348828 4 drwxr-xr-x 3 507 509 4096 8月 1 15:23 /home/ies/.kde 3348829 4 drwxr-xr-x 2 507 509 4096 8月 1 15:23 /home/ies/.kde/Autostart 2113337 4 -rw-r--r-- 1 507 509 381 8月 1 15:23 /home/ies/.kde/Autostart/.directory 2048443 4 -rw-r--r-- 1 507 509 33 8月 1 15:23 /home/ies/.bash_logout 2048444 4 -rw-r--r-- 1 507 509 658 8月 1 15:23 /home/ies/.zshrc 2048446 0 -rw-r--r-- 1 507 509 0 8月 1 15:24 /home/ies/ies.txt 3348830 4 drwxr-xr-x 4 507 509 4096 8月 1 15:23 /home/ies/.mozilla 3348831 4 drwxr-xr-x 2 507 509 4096 8月 1 15:23 /home/ies/.mozilla/extensions 3348832 4 drwxr-xr-x 2 507 509 4096 8月 1 15:23 /home/ies/.mozilla/plugins 2048445 4 -rw-r--r-- 1 507 509 124 8月 1 15:23 /home/ies/.bashrc
12.1 查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄。
[root@rhel-5 ies]# find / -nouser -o -nogroup -a -atime -3 [root@rhel-5 ies]# find / \( -nouser -o -nogroup -a -atime -3 \) -ls
find
命令的邏輯條件查找中使用-ls
參數,需要用()將各邏輯條件括起來。
13. 查找/etc目錄下所有用戶都有寫權限的文件。
[root@rhel-5 etc]# find /etc/ -perm -222
14. 查找/etc目錄下大于1M且類型為普通文件的所有文件。
[root@rhel-6 rc.d]# find /etc/ -size +1M -type f /etc/gconf/gconf.xml.defaults/%gconf-tree.xml /etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/modules/active/policy.kern [root@rhel-6 rc.d]# find /etc/ \( -size +1M -type f \) -ls 567266 1968 -rw-r--r-- 1 root root 2014902 8月 1 17:39 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml 437789 7124 -rw-r--r-- 1 root root 7292701 8月 2 08:55 /etc/selinux/targeted/policy/policy.24 437514 7124 -rw-r--r-- 1 root root 7292701 8月 2 08:55 /etc/selinux/targeted/modules/active/policy.kern
15. 查找/etc/init.d目錄下,所有用戶都有執行權限,且其他用戶有寫權限的文件。
[root@rhel-6 rc.d]# find /etc/init.d/ -perm 113
16. 查找/usr目錄下不屬于root,bin或hadoop的文件。
[root@rhel-6 rc.d]# find /usr ! \( -user root -o -user bin -o -user hadoop \) -ls [root@rhel-6 rc.d]# find /usr ! -user root -a ! -user bin -a ! -user hadoop
17. 查找/etc目錄下至少有一類用戶沒有寫權限的文件。
[root@rhel-6 rc.d]# find /etc ! -perm -222 -ls
18. 查找/etc目錄下最近一周內其內容被修改過,且不屬于root或hadoop的文件。
[root@rhel-6 rc.d]# find /etc -mtime -7 -a ! -user root -a ! -user hadoop
原創文章,作者:N21_未來人,如若轉載,請注明出處:http://www.www58058.com/27320
寫的不錯哈!