grep命令和find命令的练习

1、显示系统上root、Fedora、或user1用户的默认shell

[root@localhost ~]# grep -E  "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,行如:hello()

[root@localhost ~]# grep -E -o  "[_[:alnum:]]+\(\)" /etc/rc.d/init.d/functions
checkpid()
__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取出基名;扩展:取出其路径名

[root@localhost ~]# echo $(which passwd)
/usr/bin/passwd
[root@localhost ~]# echo $(which passwd) | grep -E -o "[^/]+$"
passwd
[root@localhost ~]# echo $(which passwd) | grep -E -o "^/.*/" | grep -E -o "^/.*[^/]"
/usr/bin

4、找出ifconfig命令结果中的1-255之间数字

[root@localhost ~]# ifconfig | grep -E -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>" | sort -n | uniq
1
2
3
6
9
14
15
29
64
73
127
128
168
192
250
255

5、挑战题:写一个模式,能匹配合理的IP地址

[root@localhost ~]# ifconfig | grep -E -o "(\<([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][0-9]|2[0-4][0-9]|25[0-5])\>" 
192.168.2.250
255.255.255.0
192.168.2.255
127.0.0.1
255.0.0.0

6、挑战题:写一个模式,能匹配出所有的邮件地址

grep -E "^([a-zA-Z0-9_-\.+]+@[a-zA-Z0-9_.]+\.[a-zA-Z]{2,5})$"

7、查找/var目录下属主为root,且属组为mail的所有文件或目录

[root@localhost ~]# find /var -user root -group mail -ls
134321240    4 drwxrwxr-x   2 root     mail         4096 Jul 22 00:12 /var/spool/mail
115656    0 drwx------   2 root     mail            6 Jun  9  2014 /var/spool/mqueue

8、查找当前系统上没有属主或属组的文件

[root@localhost ~]# find / \( -nouser -a -nogroup \) -ls
134217856    0 drwx------   2 1001     1001           59 7月  6 20:58 /home/slackware
134217857    4 -rw-r--r--   1 1001     1001           18 11月 20  2015 /home/slackware/.bash_logout
134217858    4 -rw-r--r--   1 1001     1001          193 11月 20  2015 /home/slackware/.bash_profile
134217859    4 -rw-r--r--   1 1001     1001          231 11月 20  2015 /home/slackware/.bashrc
find: ‘/proc/5115/task/5115/fd/6’: 没有那个文件或目录
find: ‘/proc/5115/task/5115/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/5115/fd/6’: 没有那个文件或目录
find: ‘/proc/5115/fdinfo/6’: 没有那个文件或目录
429338    0 drwx------   2 4006     4006           59 7月 17 07:12 /users/www
429339    4 -rw-r--r--   1 4006     4006           18 11月 20  2015 /users/www/.bash_logout
429340    4 -rw-r--r--   1 4006     4006          193 11月 20  2015 /users/www/.bash_profile
429341    4 -rw-r--r--   1 4006     4006          231 11月 20  2015 /users/www/.bashrc

进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录

[root@localhost ~]# find / \( -nouser -a -nogroup \) -a -atime -3 -ls 
134217856    0 drwx------   2 1001     1001           59 7月  6 20:58 /home/slackware
find: ‘/proc/5113/task/5113/fd/6’: 没有那个文件或目录
find: ‘/proc/5113/task/5113/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/5113/fd/6’: 没有那个文件或目录
find: ‘/proc/5113/fdinfo/6’: 没有那个文件或目录
429338    0 drwx------   2 4006     4006           59 7月 17 07:12 /users/www

9、查找/etc目录下所有用户都有写权限的文件

[root@localhost ~]# find /etc -perm -222 -ls
134320260    0 lrwxrwxrwx   1 root     root           17 7月  6 13:10 /etc/mtab -> /proc/self/mounts
67323147    0 lrwxrwxrwx   1 root     root           49 7月  6 13:10 /etc/pki/tls/certs/ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
67323148    0 lrwxrwxrwx   1 root     root           55 7月  6 13:10 /etc/pki/tls/certs/ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
109446    0 lrwxrwxrwx   1 root     root           49 7月  6 13:10 /etc/pki/tls/cert.pem -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
109451    0 lrwxrwxrwx   1 root     root           59 7月  6 13:10 /etc/pki/ca-trust/source/ca-bundle.legacy.crt -> /usr/share/pki/ca-trust-legacy/ca-bundle.legacy.default.crt
201652199    0 lrwxrwxrwx   1 root     root           40 7月  6 13:10 /etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts
134320284    0 lrwxrwxrwx   1 root     root           14 7月  6 13:10 /etc/redhat-release -> centos-release
134320285    0 lrwxrwxrwx   1 root     root           14 7月  6 13

10、查找/etc目录下大于1M,且类型为普通文件的所有文件

[root@localhost ~]# find /etc -size +1M -a -type f -exec ls -alh {} \;
-r--r--r--. 1 root root 6.7M 7月   6 13:18 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 3.7M 11月 20 2015 /etc/selinux/targeted/policy/policy.29

原创文章,作者:N27_shijinlong,如若转载,请注明出处:http://www.178linux.com/83404

(1)
N27_shijinlongN27_shijinlong
上一篇 2017-07-30
下一篇 2017-07-30

相关推荐

  • N22-第六周博客作业

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; :%s@^[[:space:]]\+[^[:space:]]@#\1@g 或 :%s@^[[:space:]]\+[^[:space:]]@#&a…

    Linux干货 2016-09-26
  • 0809行编辑器sed

    sed :Stream EDitor, 行编辑器  sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出或者使用-i选…

    Linux干货 2016-08-10
  • linux 文件管理类命令及功能用法

    第一题:Linux上的文件管理类命令都有哪些,其常用的使用方法及相关示例演示。 文件管理类的命令有: cd,ls,touch,mldir,cp,mv,rm,rmdir,pwd,tree 使用方法 (1),cd命令用来切换工作目录至dirname。其中dirName表示法可为绝对路径或相对路径。 常用格式: cd #进入用户家目录 cd~ #进入用户家目录 c…

    Linux干货 2017-07-09
  • linux-第一周

    inux命令:(type)                  1,集成在bash中的命令,内部命令。依赖于shell类型。                  2,在文件系统路径下有…

    Linux干货 2017-05-20
  • 文件处理命令作业题

    练习题    1.找出ifconfig命令结果中的本机的所有ipv4地址4    2.查出分区空间使用率的最大百分比值    3.查找用户UID最大值的用户名、UID及shell类型    4.查出/tmp的权限。以数字方式显示    5.统计当前链接本机的每…

    Linux干货 2016-08-08
  • 计算机基础及Linux基础概述

    马哥教育网络班23期+第1周课程练习 计算机基础及Linux基础概述 一、计算机组成及其功能 1.1、概述     计算机,computer 我们在中国都称它为电脑,其实我们个人使用的计算机只是计算机家族的一部分,计算机分很多种,小型机,中型机,大型机,工作站,还有PC机,等等,其实我们家庭中使用的只能算是PC机,其实…

    Linux干货 2016-09-15

评论列表(1条)

  • 马哥教育
    马哥教育 2017-08-04 16:14

    find的功能非常强大,学有余力建议深入学习。