(1) 显示当前系统上root、fedora或user1用户的默认shell
“`
~]# grep -E “^(root|fedora|user1)” /etc/passwd | cut -d: -f7
/bin/bash
/bin/bash
/bin/bash
“`
(2) 找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
“`
~]# grep -E “[[:alpha:]]+\(\)” /etc/rc.d/init.d/functions
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
“`
(3) 使用echo命令输出一个绝对路径,使用grep取出其基名;扩展:取出其路径名
“`
~]# echo “/etc/sysconfig/network-scripts/ifcfg-ens33” | grep -Eo “[^/]+/?$” | cut -d’/’ -f1
ifcfg-ens33
~]# echo “/etc/sysconfig/network-scripts/ifcfg-ens33” | grep -Eo “^/.*/”
/etc/sysconfig/network-scripts/
“`
(4) 找出ifconfig命令结果中的1-255之间的数字
“`
~]# ifconfig | grep -E “(\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-5]\>)”
“`
(5) 挑战题:写一个模式,能匹配合理的IP地址
“`
~]# ifconfig | grep -E “(\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-4]\>).(\<[0-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-4]\>).(\<[0-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-4]\>).(\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-4]\>)”
inet 10.6.9.148 netmask 255.255.255.0 broadcast 10.6.9.255
inet 127.0.0.1 netmask 255.0.0.0
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
“`
(6) 挑战题:写一个模式,能匹配出所有的邮件地址
“`
~]# vim mail.test
tom@cartoon.com hello
13810773388@139.com —o9fkdjfi#
sssjji4f4e@126.com jfeifj389j
hhd8-12dd@hh99-jd.net jjjjllllll
~]# grep -Eo “\<[[:alnum:][:punct:]]+@[[:alnum:][:punct:]]+.[[:alpha:]]\>” mail.test
tom@cartoon.com
13810773388@139.com
sssjji4f4e@126.com
hhd8-12dd@hh99-jd.net
“`
(7) 查找/var目录下属主为root,且属组为mail的所有文件或目录
“`
~]# find /var -user root -a -group mail
/var/spool/mail
/var/spool/mail/root
“`
(8) 查找当前系统上没有属主或属组的文件;扩展:查找当前系统上没有属主或属组,且最近三天内曾被访问过的文件或目录;
“`
~]# find / -nouser -o -nogroup -a -type f
扩展:
~]# find / -nouser -o -nogroup -a atime -3
“`
(9) 查找/etc目录下所有用户都有写权限的文件
“`
~]# find /etc -perm -222 -a -type f
“`
(10) 查找/etc目录下大于1M,且文件类型为普通文件的所有文件
“`
~]# find /etc -size +1M -type f -exec ls -lh {} \;
-rw-r–r–. 1 root root 3.6M Nov 12 2016 /etc/selinux/targeted/active/policy.kern
-rw-r–r–. 1 root root 1.4M Nov 12 2016 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r–r–. 1 root root 3.6M Nov 12 2016 /etc/selinux/targeted/policy/policy.30
-r–r–r–. 1 root root 7.0M Aug 29 14:42 /etc/udev/hwdb.bin
-rw-r–r–. 1 root root 1.4M Nov 6 2016 /etc/brltty/zh-tw.ctb
“`
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/87471