1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;
方法一:
[root@CentOS6 ~]# grep '^[[:space:]]\{1,\}.*' /boot/grub/grub.conf root (hd0,0) kernel /vmlinuz-2.6.32-642.3.1.el6.x86_64 ro root=/dev/mapper/vg_centos6-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=vg_centos6/lv_swap.UTF-8 rd _LVM_LV=vg_centos6/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto initrd /initramfs-2.6.32-642.3.1.el6.x86_64.img ...
方法二:
[root@CentOS6 ~]# grep '^[[:space:]]\+.*' /boot/grub/grub.conf root (hd0,0) kernel /vmlinuz-2.6.32-642.3.1.el6.x86_64 ro root=/dev/mapper/vg_centos6-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=vg_centos6/lv_swap.UTF-8 rd _LVM_LV=vg_centos6/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto initrd /initramfs-2.6.32-642.3.1.el6.x86_64.img ...
2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
[root@CentOS6 ~]# grep -E '^#[[:space:]]+[[:graph:]]+' /etc/rc.d/rc.sysinit # /etc/rc.d/rc.sysinit - run once at boot time # Taken in part from Miquel van Smoorenburg's bcheckrc. # Check SELinux status # Print a text banner. ...
3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@CentOS6 ~]# netstat -tan|grep 'LISTEN[[:space:]]*$' 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:* LISTEN ...
4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@CentOS6 ~]# grep -o '^\([[: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:3005:3005::/home/bash:/bin/bash nologin:x:3008:3008::/home/nologin:/sbin/nologin
5、显示当前系统上root、fedora或user1用户的默认shell;
[root@CentOS6 ~]# egrep '^(root|fedora|user1)' /etc/passwd|awk -F':' '{print $7}' /bin/bash /bin/bash /bin/bash
6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
[root@CentOS6 ~]# grep '\<[[:alpha:]].*\>()' /etc/rc.d/init.d/functions fstab_decode_str() { checkpid() { daemon() { killproc() {
7、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@CentOS6 ~]# echo "/etc/rc.d/rc" |grep -o '[^/]\+$' rc
扩展:取出其路径名
[root@CentOS6 ~]# echo "/etc/rc.d/rc" |grep -o '^/.*/' /etc/rc.d/
8、找出ifconfig命令结果中的1-255之间数字;
[root@CentOS6 ~]# ifconfig |grep --color=auto -E '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' eth0 Link encap:Ethernet HWaddr 00:0C:29:56:9F:C1 inet addr:192.168.31.249 Bcast:192.168.31.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe56:9fc1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:125943 errors:0 dropped:0 overruns:0 frame:0 TX packets:76554 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:153321348 (146.2 MiB) TX bytes:9617405 (9.1 MiB) inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:841 errors:0 dropped:0 overruns:0 frame:0 TX packets:841 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:939130 (917.1 KiB) TX bytes:939130 (917.1 KiB)
9、挑战题:写一个模式,能匹配合理的IP地址;
[root@CentOS6 ~]# ifconfig |grep --color -E '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' inet addr:192.168.31.249 Bcast:192.168.31.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0
10、挑战题:写一个模式,能匹配出所有的邮件地址;
[root@CentOS6 ~]# grep --color -E '^[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+$' mailtest long@mail.com santi@gmail.com wangdachui@hotmail.org
11、查找/var目录下属主为root,且属组为mail的所有文件或目录;
[root@CentOS6 ~]# find /var -user root -a -group mail -ls 1571276 4 drwxrwxr-x 2 root mail 4096 8月 7 21:23 /var/spool/mail 1570144 4 -rw------- 1 root mail 2949 8月 6 22:04 /var/spool/mail/root
12、查找当前系统上没有属主或属组的文件;
[root@CentOS6 ~]# find / -nouser -o -nogroup -ls
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
[root@CentOS6 ~]# find / \( -nouser -o -nogroup \) -a -atime -3 -ls
13、查找/etc目录下所有用户都有写权限的文件;
[root@CentOS6 ~]# find /etc -perm -222 -ls |tail -3 1321779 0 lrwxrwxrwx 1 root root 46 7月 8 23:30 /etc/alternatives/java_sdk_1.6.0_exports -> /usr/lib/jvm-exports/java-1.6.0-openjdk.x86_6 41321467 0 lrwxrwxrwx 1 root root 37 7月 30 15:13 /etc/alternatives/jre -> /usr/lib/jvm/jre-1.7.0-openjdk.x86_64 1321066 0 lrwxrwxrwx 1 root root 7 7月 15 22:36 /etc/rc -> rc.d/rc
14、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[root@CentOS6 ~]# find /etc/ -size +1M -type f -ls 1329408 8228 -rw-r--r-- 1 root root 8424092 7月 8 23:33 /etc/selinux/targeted/modules/active/policy.kern 1329416 8228 -rw-r--r-- 1 root root 8424092 7月 8 23:33 /etc/selinux/targeted/policy/policy.24 1325355 2144 -rw-r--r-- 1 root root 2194776 7月 8 23:33 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@CentOS6 ~]# find /etc/init.d/ -perm -113 -ls
16、查找/usr目录下不属于root、bin或hadoop的文件;
[root@CentOS6 ~]# find /usr ! -user root ! -user bin ! -user hadoop -ls 539527 12 -rwsr-xr-x 1 abrt abrt 10296 5月 12 04:43 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
17、查找/etc/目录下至少有一类用户没有写权限的文件;
[root@CentOS6 ~]# find /etc ! -perm -222 -ls
18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[root@CentOS6 ~]# find /etc -mtime -7 ! -user root ! -user hadoop -ls
原创文章,作者:sandiegoit,如若转载,请注明出处:http://www.178linux.com/30779
评论列表(1条)
写的很好,排版也很棒,加油