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

相关推荐

  • 互联网安全之iptables/netfilter入门到进阶

    随着互联网技术的方兴未艾,各种网络应用层出不穷,网络攻击、黑客入侵也成了网民畅游互联网的心头大患,互联网安全也愈加受到了人们的重视。网络防火墙,作为一种简单高效的互联网防御手段,逐渐成为了网民畅游网络世界的保护伞。下面笔者介绍下Linux系统的守卫者——iptables/netfilter。  一 兄弟齐心,其利断金  iptables/netfilter就…

    Linux干货 2017-05-06
  • 自制kickstart文件——完成CentOS系统的自动化安装

    CentOS 系统安装: ·安装程序:anaconda         bootloader–>kernel(initrd(rootfs))–>anaconda ·anaconda:<两种模式>    …

    Linux干货 2016-09-16
  • lamp+nfs搭建wordpress

    一、前言 lamp是大多上公司常用的架构,本文将针对分离式的lamp+nfs来搭建一个简单的wordpress网站。 二、架构图 三、基本实现过程 3.1:dns搭建 由于这这是一个实验,故使用yum搭建dns服务器 yum install -y bind 配置如下 dns主配置文件 dns辅助配置文件 zone文件 对所有服务…

    Linux干货 2015-10-16
  • grep文本查找和正则表达式

    一、grep grep: (Global search REgular expression and Print outthe line)全局查找正则表达式并且打印结果行。不会对输入的文本进行修改。 Unix的grep家族包括grep、egrep和fgrep,egrep是grep的扩展,支持更多的正则元字符; fgrep就是fixed grep或f…

    Linux干货 2016-08-07
  • 0811:脚本练习

    系统信息获取脚本 1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 [root@centos7 scripts]#cat systeminfo.sh  #!/usr/bin/bash  server_ip…

    Linux干货 2016-08-22
  • 马哥教育网络班22期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、vi的基本概念 基本上vi可以分为三种状态,分别是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下: 1) 命令行模式command mode) 控制屏幕光标的移…

    Linux干货 2016-09-26

评论列表(1条)

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

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