1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行(以/boot/grub2/grub.cfg代替);
[root@localhost ~]# grep '^[[:space:]]\+' /boot/grub2/grub.cfg load_env set default="${next_entry}" set next_entry= ...内容略
2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行(以/etc/rc.d/init.d/functions代替 );
[root@localhost ~]# cat /etc/rc.d/init.d/functions | grep '^#[[:space:]]\+[^[:space:]]\+' # -*-Shell-script-*- # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # Make sure umask is sane # Set up a default search path. # Get a sane screen width
3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
[root@localhost ~]# netstat -tan | grep 'LISTEN[[:space:]]*' tcp 0 0 192.168.122.1:53 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:25 0.0.0.0:* LISTEN tcp6 0 0 :::80 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
[root@localhost ~]# useradd bash && useradd testbash && useradd basher && useradd -d /sbin/nologin nologin [root@localhost ~]# grep '^\([^:]\+\):.*/\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 123:x:1111:1111::/123:/123 bash:x:1129:1129::/home/bash:/bin/bash nologin:x:1130:1130::/home/nologin:/sbin/nologin
5、显示当前系统上root、fedora或user1用户的默认shell;
[root@localhost ~]# cat /etc/passwd | grep '^\(root\|fedora\|user1\):' | cut -d: -f1,7 root:/bin/bash user1:/bin/bash
6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
[root@localhost ~]# cat /etc/rc.d/init.d/functions | grep '^[[:alpha:]]\+()' checkpid() { daemon() { killproc() { pidfileofproc() { pidofproc() { status() { success() { failure() { passed() { warning() { action() { strstr() {
7.1、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@localhost ~]# echo '/etc/ppp' | grep -o '^.*[^/]' | grep -o '/[^/]\+/\?$' /ppp
7.2、扩展:取出其路径名
[root@localhost ~]# echo '/etc/ppp' | grep -o '^.*[^/]' | grep -o '^.*/' /etc/
8、找出ifconfig命令结果中的1-255之间的数字
[root@localhost ~]# ifconfig | egrep '\<([1-9][1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' inet 1.1.1.117 netmask 255.255.255.0 broadcast 1.1.1.255 RX packets 1970 bytes 223843 (218.5 KiB) TX packets 1264 bytes 225028 (219.7 KiB) inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> RX packets 4 bytes 340 (340.0 B) TX packets 4 bytes 340 (340.0 B) inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
9、挑战题:写一个模式,能匹配合理的IP地址
[root@localhost ~]# ifconfig | grep -o --color '\(\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)' 1.1.1.117 255.255.255.0 1.1.1.255 127.0.0.1 255.0.0.0 192.168.122.1 255.255.255.0 192.168.122.255
10、挑战题:写一个模式,能匹配出所有的邮件地址;
[root@localhost ~]# echo '372564854@qq.com' >> 1.txt [root@localhost ~]# echo '12345678@qq.com' >> 1.txt [root@localhost ~]# echo 'lich9978@qq.com' >> 1.txt [root@localhost ~]# cat 1.txt 372564854@qq.com 12345678@qq.com lich9978@qq.com [root@localhost ~]# cat 1.txt | grep '^[a-zA-Z0-9_-]\+@[a-zA-Z0-9_-]\+\(\.[a-zA-Z0-9_-]\+\)\+$' 372564854@qq.com 12345678@qq.com lich9978@qq.com
11、查找/var目录下属主为root,且属组为mail的所有文件或目录;
[root@localhost ~]# find /var -user root -group mail /var/spool/mail
12、查找当前系统上没有属主或属组的文件;
[root@localhost ~]# find / \( -nouser -o -nogroup \) -a -atime -3 find: ‘/proc/3745/task/3745/fd/6’: 没有那个文件或目录 find: ‘/proc/3745/task/3745/fdinfo/6’: 没有那个文件或目录 ...内容略 /home/nologin/.mozilla/extensions /home/nologin/.mozilla/plugins [root@localhost ~]#
13、查找/etc目录下所有用户都有写权限的文件
[root@localhost etc]# find /etc -perm -g+w,u+w,o+w /etc/mtab /etc/pki/java/cacerts /etc/pki/tls/cert.pem ...略 /etc/httpd/run /etc/1.txt
14、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[root@localhost etc]# find /etc -size +1M -type f /etc/udev/hwdb.bin /etc/selinux/targeted/policy/policy.29 /etc/brltty/zh-tw.ctb
15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@localhost etc]# find /etc/init.d -perm -g+x,o+wx,u+x /etc/init.d
16、查找/usr目录下不属于root、bin或hadoop的文件
[root@localhost etc]# find /usr -not \( -user root -o -user bin -o -user hadoop \) /usr/share/polkit-1/rules.d /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
17、查找/etc目录下至少有一类用户没有写权限的文件
[root@localhost etc]# find /etc/ ! -perm -444 /etc/crypttab /etc/pki/CA/private ...内容略 /etc/libvirt/nwfilter /etc/gssproxy/gssproxy.conf
18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[root@localhost etc]# chown basher 1.txt [root@localhost etc]# ll 1.txt -rw-rw-rw-. 1 basher root 0 8月 7 21:11 1.txt [root@localhost etc]# find /etc ! -user root ! -user hadoop -ctime -7 /etc/1.txt
原创文章,作者:lichenhan,如若转载,请注明出处:http://www.178linux.com/30748
评论列表(1条)
写的很好,排版也很棒,加油