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

相关推荐

  • bash脚本作业

    1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。 2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出。 1、求100以内所有正整数之和 2、通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,…

    Linux干货 2016-08-19
  • linux命令 kill命令详则

    kill命令 kill用来删除执行中的程序或工作。kill可将指定的信息送至程序。预设的信息(默认)为SIGTERM(15),可经指定程序终止。若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除程序。程序或工作的编号可利用ps指令或job指令查看。 语法 kill(选项)(参数) 选项 -a:当处理当前进程时,不限制命令名和进程号的对应关系; &…

    2017-08-21
  • 一个PHP程序员学习运维的转型

         我是一位PHP开发工程师,平时负责前端、后端以及服务器端的工作,但是称不上是个牛逼的程序员。网上热烈讨论一则传闻,“全栈工程师”(Full Stack Engineer),要求应征者对开发堆栈的每个方面都有所掌握。那究竟何为 “全栈工程师”呢?从字面上来理解,全栈工程师必须熟悉开发堆栈的每一个层次,或者至少熟悉绝大多数…

    Linux干货 2017-04-02
  • 基于mysql的数据库分析系统(rsyslog)

    rsyslog:日志收集和存储系统 1.事件在电脑中的日志记录格式为:     日期时间 主机 进程[pid]:事件内容 2.rsyslog的特性:     多线程;     UDP,TCP,SSL/TLS,RELP; &nbsp…

    Linux干货 2016-10-23
  • HTTP协议

    HTTP协议   HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。HTTP协议工作于客户端-服务端架构之上。浏览器作为HT…

    Linux干货 2017-02-15
  • vsftpd虚拟用户搭建

    首选的FTP服务器搭建方式 安装vsftpd      yum install -y vsftpd      systemctl enable vsftpd     #设置开机启动      systemctl is-enabled vsf…

    Linux干货 2017-04-27

评论列表(1条)

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

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