1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;
2、显示/etc/passwd中以sh结尾的行;
3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;
4、查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行;
5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;
6、查找ifconfig命令结果中的1-255之间的整数;
7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行
8、在/etc/passwd中取出默认shell为bash
9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息
10、高亮显示passwd文件中冒号,及其两侧的字符
11、匹配/etc/services中开头结尾字母一样的单词
1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;
[root@node rc.d]# vim rc.sysinit
hello
no
yes
world
haha
Hello
#方法一
[root@node rc.d]# grep -i '^h' /etc/rc.d/rc.sysinit
hello
haha
Hello
#方法二
[root@node rc.d]# egrep '^[hH]' /etc/rc.d/rc.sysinit
hello
haha
Hello
#方法三
[root@node rc.d]# egrep -i '^h' /etc/rc.d/rc.sysinit
hello
haha
Hello
2、显示/etc/passwd中以sh结尾的行;
[root@node ~]# grep "sh$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mhn:x:1000:1000::/home/mhn:/bin/bash
tom:x:1001:1001::/home/tom:/bin/bash
haha:x:1002:1002::/home/haha:/bin/bash
3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行;
方法一
[root@node ~]# grep "^#\ " /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Sep 6 13:27:55 2023
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
方法二
[root@node ~]# grep -E '^#[[:space:]]+[^[:space:]]*' /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Sep 6 13:27:55 2023
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
方法三
[root@node ~]# grep -E '^#\s.{2,}' /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Sep 6 13:27:55 2023
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
4、查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行;
[root@node ~]# grep "^to" /etc/rc.d/rc.local
touch /var/lock/subsys/local
[root@node ~]# egrep -w "(to).*\1" /etc/rc.d/rc.local
# In contrast to previous versions due to parallel execution during boot
to to
[root@node ~]# egrep '\b(to).*\1\b' /etc/rc.d/rc.local
# In contrast to previous versions due to parallel execution during boot
5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;
[root@node ~]# grep -w "\" /etc/inittab
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
####grep -w 's.*d' /etc/inittab
#精确匹配:\<匹配内容\>(以**开头,以**结尾)
6、查找ifconfig命令结果中的1-255之间的整数;
[root@node ~]# ifconfig | egrep -o -w "[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]"
192
168
75
131
255
255
255
192
168
75
255
#egrep -w -o '[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]|2[0-4][0-9]'
7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行
[root@node ~]# grep -i -w "^F\" /var/log/secure
8、在/etc/passwd中取出默认shell为bash
[root@node ~]# grep -w "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mhn:x:1000:1000::/home/mhn:/bin/bash
tom:x:1001:1001::/home/tom:/bin/bash
haha:x:1002:1002::/home/haha:/bin/bash
9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息
第一种方法:
[root@node ~]# find /etc/ -name "ns*.conf" | xargs ls -l
-rw-r--r--. 1 root root 2980 9月 6 21:32 /etc/authselect/nsswitch.conf
lrwxrwxrwx. 1 root root 29 9月 6 21:32 /etc/nsswitch.conf -> /etc/authselect/nsswitch.conf
第二种方法:
[root@node ~]# ls -l /etc/ns*.conf
lrwxrwxrwx. 1 root root 29 9月 6 21:32 /etc/nsswitch.conf -> /etc/authselect/nsswitch.conf
第三种方法:
ls /etc | grep '\bns.*.conf\b'
10、高亮显示passwd文件中冒号,及其两侧的字符
[root@node ~]# grep --color ".:." /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11、匹配/etc/services中行开头结尾字母一样的单词
#将一行中开头单词与行尾单词相同的输出
[root@node ~]# egrep -o '\b([a-zA-Z]+)\b.*\b\1$' /etc/services