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 "\"
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.178linux.com/53570