-
對grep 和 find 命令的操作
1、顯示當前系統上root、fedora或user1用戶的默認shell;
[root@localhost ~]# cat /etc/passwd | grep -E "^(root|fedora|user1)" root:x:0:0:root:/root:/bin/bash user1:x:1007:1007::/home/user1:/bin/bash fedora:x:1013:1013::/home/fedora:/bin/bash [root@localhost ~]# cat /etc/passwd | grep -E "^(root|fedora|user1)\>" | cut -d: -f1,7 root:/bin/bash user1:/bin/bash fedora:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello();
[root@localhost ~]# cat /etc/rc.d/init.d/functions | grep -E -o "\<.*\>\(\)" ## 或者 ## [root@localhost ~]# grep -E -o "[_[:alnum:]]+\(\)" /etc/rc.d/init.d/functions
3、使用echo命令輸出一個路徑,使用grep取出其基名;
擴展:取出其路徑名;
[root@localhost ~]# echo /etc/sysconfig | grep -E -o "[^/]+$" sysconfig #從尾部取到非/的部分 ## 或者 ## [root@localhost ~]# echo /etc/sysconfig/ | grep -E -o "[^/]+/?$" sysconfig/ ## 擴展:取路徑名 ## [root@localhost ~]# echo /etc/sysconfig/ | grep -E -o "^/[[:alnum:]]+[^/]" /etc [root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -E -o "^/[[:alnum:]]*[^/]" #多層路徑用這樣的匹配是沒辦法取到的,應該用下面的 /etc [root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -E -o "/.*/" /etc/rc.d/init.d/
4、找出ifconfig命令結果中的1-255之間數字;
[root@localhost ~]# ifconfig | grep -E -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
5、寫一個模式,能匹配出合理的IP地址;
[root@localhost ~]# ifconfig | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" 192.168.1.106 255.255.255.0 192.168.1.255 127.0.0.1 255.0.0.0
6、寫一個模式,能匹配出所有的郵件地址;
[root@localhost ~]# cat /scripts/emailpreg.txt | grep -E -o "[a-zA-Z0-9_-]*@[a-zA-Z0-9_-]*\.[a-zA-Z]*$" 123@qq.com a34@163.com a43ll@gmail.com hello_world@abc.cn
7、查找/var目錄下屬主為root,且屬組為mail的所有文件和目錄;
[root@localhost ~]# find /var -user root -a -group mail -ls 134321240 4 drwxrwxr-x 2 root mail 4096 Oct 19 16:28 /var/spool/mail 135041050 408 -rw------- 1 root mail 414950 Oct 19 16:28 /var/spool/mail/root ##或者## [root@localhost ~]# find /var -user root -a -group mail -exec ls -ldh {} \; drwxrwxr-x. 2 root mail 4.0K Oct 19 16:31 /var/spool/mail -rw-------. 1 root mail 410K Oct 19 16:31 /var/spool/mail/root
8、查找當前系統上沒有屬主或屬組的文件;
[root@localhost ~]# find / \( -nouser -o -nogroup \) -exec ls -ldh {} \; drwx------. 2 1005 distro 59 Oct 3 07:58 /home/mandriva -rw-r--r--. 1 1005 distro 18 Nov 20 2015 /home/mandriva/.bash_logout -rw-r--r--. 1 1005 distro 193 Nov 20 2015 /home/mandriva/.bash_profile -rw-r--r--. 1 1005 distro 231 Nov 20 2015 /home/mandriva/.bashrc
查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;
[root@localhost ~]# find / \( -nouser -o -nogroup \) -atime -3 -exec ls -ldh {} \; drwx------. 2 1005 distro 59 Oct 3 07:58 /home/mandriva [root@localhost ~]# stat /home/mandriva | grep -i access Access: (0700/drwx------) Uid: ( 1005/ UNKNOWN) Gid: ( 2016/ distro) Access: 2016-10-19 16:36:46.077491511 -0400 #訪問時間 [root@localhost ~]# date +'%F %H:%M:%S' 2016-10-19 16:55:03 #當前時間,證明文件缺失在3天內被訪問過
9、查找/etc目錄下所有用戶都有寫權限的文件;
[root@localhost ~]# find /etc -perm -222 -ls
10、查找/etc目錄下大于1M,且目錄類型為普通文件的所有文件;
[root@localhost ~]# find /etc -size +1M -type f -exec ls -lh {} \; -r--r--r--. 1 root root 6.7M Aug 23 12:14 /etc/udev/hwdb.bin -rw-r--r--. 1 root root 3.7M Nov 20 2015 /etc/selinux/targeted/policy/policy.29
11、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件;
[root@localhost ~]# find /etc/init.d/ -perm -113 -type f -ls
12、查找/usr目錄下不屬于root、bin或hadoop的文件;
[root@localhost ~]# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls 134652726 0 drwx------ 2 polkitd root 6 Jun 9 2014 /usr/share/polkit-1/r ## 或者 ## [root@localhost ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls 134652726 0 drwx------ 2 polkitd root 6 Jun 9 2014 /usr/share/polkit-1/rules.d
13、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;
[root@localhost ~]# find /etc -not -perm -222 -type f -ls
14、查找/etc目錄下最近一周內其內容被修改過,且不屬于root或hadoop的文件;
[root@localhost ~]# find /etc -not \( -user root -a -user hadoop \) -mtime -7 -ls
原創文章,作者:luoluo,如若轉載,請注明出處:http://www.www58058.com/53570