一、顯示當前系統上root、fedora或user1用戶的默認shell;
java
PS:第一眼看到問題可能會有點頭疼,那就把問題拆分完成,組合多個簡單命令完成複雜工做 第一步,查找到這些用戶並顯示: 使用|或銜接多個過濾條件: [root@CentOS7 ~]# grep -E "^root\>|^fedora\>|^user1\>" /etc/passwd #grep -E也可以使用egrep root:x:0:0:root:/root:/bin/bash user1:x:1003:1003::/home/user1:/bin/bash fedora:x:1005:1005::/home/fedora:/bin/bash
第二步,將這些用戶默認的shell顯示出來: [root@CentOS7 ~]# grep -E "^root\>|^fedora\>|^user1\>" /etc/passwd | cut -d : - f1,7 root:/bin/bash user1:/bin/bash fedora:/bin/bash
二、找出/etc/rc.d/init.d/functions文件中某單詞後面跟一組小括號的行,形如:hello();node
#方法一:使用grep正則表達式過濾,-o只顯示匹配到的行: [root@mylinux ~]# grep -Eo ".*[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions checkpid() __pids_var_run() __pids_pidof() daemon() killproc() pidfileofproc() pidofproc() status() echo_success() echo_failure() echo_passed() echo_warning() update_boot_stage() success() failure() passed() warning() action() strstr() is_ignored_file() is_true() is_false() apply_sysctl()
三、使用echo命令輸出一個絕對路徑,使用grep取出其基名;linux
擴展:取出其路徑名正則表達式
使用grep取出其基名:其中[^/]表明不是以/開頭的,/?是考慮到路徑有可能爲目錄 [root@mylinux tmp]# echo "/tmp/abc/12/122cb/mylinux.test" | grep -Eo "[^/]+/?$" mylinux.test
取出其路徑名: [root@mylinux tmp]# echo "/tmp/abc/12/122cb/mylinux.test" | grep -o "/[A-Za-z0-9]\+.*/" /tmp/abc/12/122cb/ 或者: [root@mylinux tmp]# echo "/tmp/abc/12/122cb/mylinux.test" | grep -o "/[[:alpha:]]\+.*/" /tmp/abc/12/122cb/
想一想,這會不會太麻煩了點,記得在bash特性之一路徑補全教程上有關於基名和路徑名的獲取方式,basename和dirname: [root@CentOS7 ~]# basename /tmp/abc/12/abd abd [root@CentOS7 ~]# dirname /tmp/abc/12/abd /tmp/abc/12
四、找出ifconfig命令結果中的1-255之間數字;shell
第一步,先查看ifconfig命令有哪些值: [root@CentOS7 ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.15.35.204 netmask 255.255.254.0 broadcast 10.15.35.255 inet6 fe80::20c:29ff:fe24:16e0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:24:16:e0 txqueuelen 1000 (Ethernet) RX packets 44040 bytes 3257103 (3.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1240 bytes 149473 (145.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
思路,利用分段,把1-255分爲一位兩位三位數字,可是200-255之間比較特殊須要格外設置 因而乎:1-9,10-99,100-199,200-249,250-255
[root@CentOS7 ~]# ifconfig | egrep -o "\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>" 10 15 35 204 255 255 254 10 15 35 255 64 29 24 16 3 2 161 5 73 127 1 255 1 128
五、挑戰題:寫一個模式,能匹配合理的IP地址;安全
合理IP範圍1.0.0.1 - 255.255.255.254bash
[root@CentOS7 ~]# ifconfig | egrep -o "(\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>).(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)" 10.15.35.204 255.255.254.0 10.15.35.255 127.0.0.1 255.0.0.0
六、挑戰題:寫一個模式,能匹配出全部的郵件地址;app
鑑於郵箱名稱可能包含的特殊符號爲下劃線_或者點. [root@mylinux ~]# echo "email: My_li.nux123@126.com.cn 123@126.com test22we%sin.com.cn" | egrep -o "\<([a-zA-Z0-9_.]+)@([a-zA-Z0-9_.]+)\.([a-zA-Z]*)\>" My_li.nux123@126.com.cn 123@126.com
七、查找/var目錄下屬主爲root,且屬組爲mail的全部文件或目錄;ide
[root@CentOS7 tmp]# find /var -user root -group mail -ls 33596404 0 drwxrwxr-x 2 root mail 90 9月 1 15:58 /var/spool/mail 34464012 4 -rw------- 1 root mail 632 8月 15 11:24 /var/spool/mail/root
八、查找當前系統上沒有屬主或屬組的文件;oop
[root@CentOS7 ~]# find / \( -nouser -o -nogroup \) -ls find: '/proc/8148/task/8148/fd/6': 沒有那個文件或目錄 find: '/proc/8148/task/8148/fdinfo/6': 沒有那個文件或目錄 find: '/proc/8148/fd/6': 沒有那個文件或目錄 find: '/proc/8148/fdinfo/6': 沒有那個文件或目錄 33781245 0 -rw-rw---- 1 1006 mail 0 9月 2 14:52 /var/spool/mail/test1 778 0 drwx------ 2 1006 1006 59 9月 2 14:52 /home/test1 1147 4 -rw-r--r-- 1 1006 1006 18 8月 3 00:00 /home/test1/.bash_logout 363744 4 -rw-r--r-- 1 1006 1006 193 8月 3 00:00 /home/test1/.bash_profile 517241 4 -rw-r--r-- 1 1006 1006 231 8月 3 00:00 /home/test1/.bashrc
進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;
測試環境創建:因爲userdel不加參數-r的時候家目錄文件不會刪除。 [root@mylinux ~]# useradd test12 [root@mylinux ~]# userdel test12
[root@mylinux ~]# find / \( -nouser -o -nogroup -a -atime -3 \) -ls 139 0 drwx------ 3 1000 1000 74 Sep 5 02:22 /home/test12 134217986 0 drwxr-xr-x 4 1000 1000 37 Aug 27 09:36 /home/test12/.mozilla 268656290 0 drwxr-xr-x 2 1000 1000 6 Jun 10 2014 /home/test12/.mozilla/extensions 402653442 0 drwxr-xr-x 2 1000 1000 6 Jun 10 2014 /home/test12/.mozilla/plugins 140 4 -rw-r--r-- 1 1000 1000 18 Aug 3 00:00 /home/test12/.bash_logout 141 4 -rw-r--r-- 1 1000 1000 193 Aug 3 00:00 /home/test12/.bash_profile 142 4 -rw-r--r-- 1 1000 1000 231 Aug 3 00:00 /home/test12/.bashrc find: ‘/proc/4623/task/4623/fd/6’: No such file or directory find: ‘/proc/4623/task/4623/fdinfo/6’: No such file or directory find: ‘/proc/4623/fd/6’: No such file or directory find: ‘/proc/4623/fdinfo/6’: No such file or directory 34465570 0 -rw-rw---- 1 1000 mail 0 Sep 5 02:22 /var/spool/mail/test12
查看文件訪問時間:使用stat命令 [root@mylinux ~]# stat /home/test12 File: ‘/home/test12’ Size: 74 Blocks: 0 IO Block: 4096 directory Device: fd02h/64770d Inode: 139 Links: 3 Access: (0700/drwx------) Uid: ( 1000/ UNKNOWN) Gid: ( 1000/ UNKNOWN) Context: unconfined_u:object_r:user_home_dir_t:s0 Access: 2016-09-05 02:36:22.150270911 +0800 Modify: 2016-09-05 02:31:46.066277576 +0800 Change: 2016-09-05 02:31:46.066277576 +0800 Birth: -
九、查找/etc目錄下全部用戶都有寫權限的文件;
[root@mylinux ~]# find /etc -perm -222 -ls 33595524 0 lrwxrwxrwx 1 root root 17 Aug 27 09:36 /etc/mtab -> /proc/self/mounts 33975983 0 lrwxrwxrwx 1 root root 49 Aug 27 10:45 /etc/pki/tls/certs/ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 33975984 0 lrwxrwxrwx 1 root root 55 Aug 27 10:45 /etc/pki/tls/certs/ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
十、查找/etc目錄下大於1M,且類型爲普通文件的全部文件;
[root@mylinux ~]# find /etc \( -size +1M -a -type f \) -ls 594150 6824 -r--r--r-- 1 root root 6984832 Aug 27 10:45 /etc/udev/hwdb.bin 34988614 1304 -rw------- 1 root root 1333775 Aug 27 10:46 /etc/selinux/targeted/contexts/files/file_contexts.bin 944900 3688 -rw-r--r-- 1 root root 3773563 Aug 27 10:46 /etc/selinux/targeted/policy/policy.29
十一、查找/etc/init.d/目錄下,全部用戶都有執行權限,且其它用戶有寫權限的文件;
[root@mylinux ~]# find /etc/init.d/ \( -type f -a -perm -112 \) -ls
十二、查找/usr目錄下不屬於root、bin或hadoop的文件;
[root@mylinux ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls 17491411 0 drwx------ 2 polkitd root 6 Jun 24 02:13 /usr/share/polkit-1/rules.d 51170399 16 -rwsr-sr-x 1 abrt abrt 15336 Dec 1 2015 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
1三、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;
[root@mylinux ~]# find /etc -not -perm /222 -ls 33978125 192 -r--r--r-- 1 root root 194984 Aug 27 10:45 /etc/pki/ca-trust/extracted/java/cacerts 50710330 340 -r--r--r-- 1 root root 346654 Aug 27 10:45 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt 451698 256 -r--r--r-- 1 root root 262042 Aug 27 10:45 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 342390 204 -r--r--r-- 1 root root 208874 Aug 27 10:45 /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem 342391 208 -r--r--r-- 1 root root 208976 Aug 27 10:45 /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem 17606286 4 -r--r--r-- 1 root root 531 Sep 6 2015 /etc/lvm/profile/cache-mq.profile 17606288 4 -r--r--r-- 1 root root 338 Sep 6 2015 /etc/lvm/profile/cache-smq.profile 17606289 4 -r--r--r-- 1 root root 2249 Jun 29 02:01 /etc/lvm/profile/command_profile_template.profile 17606290 4 -r--r--r-- 1 root root 828 Jun 29 02:01 /etc/lvm/profile/metadata_profile_template.profile 17606291 4 -r--r--r-- 1 root root 76 Sep 6 2015 /etc/lvm/profile/thin-generic.profile 17606292 4 -r--r--r-- 1 root root 80 Sep 6 2015 /etc/lvm/profile/thin-performance.profile 33595559 4 ---------- 1 root root 457 Sep 5 02:31 /etc/gshadow- 33595567 4 ---------- 1 root root 693 Sep 5 02:31 /etc/shadow- 34465580 4 ---------- 1 root root 446 Sep 5 02:37 /etc/gshadow 594150 6824 -r--r--r-- 1 root root 6984832 Aug 27 10:45 /etc/udev/hwdb.bin 34430570 4 -r--r--r-- 1 root root 33 Aug 27 09:40 /etc/machine-id 34465576 4 ---------- 1 root root 664 Sep 5 02:37 /etc/shadow 51393329 4 -r--r--r-- 1 root root 63 Nov 20 2015 /etc/ld.so.conf.d/kernel-3.10.0-327.el7.x86_64.conf 50872368 4 -r--r--r-- 1 root root 63 Aug 19 03:17 /etc/ld.so.conf.d/kernel-3.10.0-327.28.3.el7.x86_64.conf 34297323 4 -r-------- 1 root root 45 Aug 27 09:39 /etc/openldap/certs/password 33974602 8 -r--r----- 1 root root 4188 Mar 31 23:30 /etc/sudoers
1四、查找/etc目錄下最近一週內其內容被修改過,且不屬於root或hadoop的文件;
[root@mylinux ~]# find /etc/ -type f -ctime -7 -a -not \( -user root -o -user hadoop \)
Find命令格式及參數:
一、find命令的通常形式爲;
find pathname -options [-print -exec -ok ...]
二、find命令的參數;
pathname: find命令所查找的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄。
-print: find命令將匹配的文件輸出到標準輸出。
-exec: find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式爲'command' { } ;,注意{ }和;之間的空格。
-ok: 和-exec的做用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每個命令以前,都會給出提示,讓用戶來肯定是否執行。
三、find命令選項
-name
按照文件名查找文件。
-perm
按照文件權限來查找文件。
-prune
使用這一選項能夠使find命令不在當前指定的目錄中查找,若是同時使用-depth選項,那麼-prune將被find命令忽略。
-user
按照文件屬主來查找文件。
-group
按照文件所屬的組來查找文件。
-mtime -n +n
按照文件的更改時間來查找文件, - n表示文件更改時間距如今n天之內,+ n表示文件更改時間距如今n天之前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。
-nogroup
查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。
-nouser
查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。
-newer file1 ! file2
查找更改時間比文件file1新但比文件file2舊的文件。
-type
查找某一類型的文件,諸如:
b - 塊設備文件。
d - 目錄。
c - 字符設備文件。
p - 管道文件。
l - 符號連接文件。
f - 普通文件。