grep 正则表达式 find

1.显示当前系统上root,fedora 或user1用户的默认shell
[root@centos7 ~]# grep -E  “^(root|fedora|user1)” /etc/passwd
root:x:0:0:root:/root:/bin/bash
fedora:x:2003:2003::/home/fedora:/bin/bash
user1:x:2004:2004::/home/user1:/bin/bash
2.找出 /etc/rc.d/init.d/functions文件中某个单词后面跟一组小括号的行,形如:hello();
[root@centos7 ~]# cat /etc/rc.d/init.d/functions |grep -E “[[:alpha:]]{1,}\(\) “
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
3.使用echo命令输出一个绝对路径,使用grep取出莫个基名。
[root@centos7 fedora]# echo /home/fedora/|grep -E “[[:alpha:]]{1,}/$”
/home/fedora/
4.找出ifconfig命令结果中的1-255之间的数字。
[root@centos7 ~]# ifconfig|grep -E -o  “\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>”
5挑战题:写一个模式,能匹配合理的ip地址;
[root@centos7 ~]# ifconfig|grep -E -o  “\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>”
192.168.119.137
255.255.255.0
192.168.119.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
6.挑战题:写一个模式,能匹配出所有的邮件地址;
[root@centos7 ~]# cat email |grep -E “^([0-9]|[a-zA-Z])([[:alnum:]]|[[:punct:]])+@[[:alnum:]]*\.(com|cn)$”
7.查找/var目录下属主为root,且属组为mail的所有文件或目录;
[root@centos7 var]# find /var/ -user root -a -group mail -ls
33701767    0 drwxrwxr-x   2 root     mail          105 Sep 17 07:19 /var/spool/mail
8.查找当前系统上没有属主或属组的文件;
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的目录或文件;
[root@centos7 var]# find / \( -nouser -o -nogroup \) -atime +3 -ls
find: ‘/proc/7176/task/7176/fd/6’: No such file or directory
find: ‘/proc/7176/task/7176/fdinfo/6’: No such file or directory
find: ‘/proc/7176/fd/6’: No such file or directory
find: ‘/proc/7176/fdinfo/6’: No such file or directory
34754918    0 -rw-rw—-   1 1005     mail            0 Sep  2 11:28 /var/spool/mail/mandriva
18360951    4 -rw-r–r–   1 1005     distro         18 Aug  2  2016 /home/mandriva/.bash_logout
18360952    4 -rw-r–r–   1 1005     distro        193 Aug  2  2016 /home/mandriva/.bash_profile
18360953    4 -rw-r–r–   1 1005     distro        231 Aug  2  2016 /home/mandriva/.bashrc
34754923    4 -rw——-   1 1005     distro         11 Sep  2 11:30 /home/mandriva/.cache/abrt/lastnotification
18360956    4 -rw——-   1 1005     distro         24 Sep  2 12:13 /home/mandriva/.bash_history
9.查找/etc目录下所有用户都有写权限的文件;
[root@centos7 etc]# find /etc/ -perm /222 -type f -ls
10. 查找/etc目录下大于1M,且类型为普通文件的所有文件;
find /etc/ -size 1M  -type f -ls
 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/87385

(0)
469008940469008940
上一篇 2017-09-18 16:09
下一篇 2017-09-18 19:58

相关推荐

  • N25第5周作业(grep和find使用)

    显示当前系统上root, fedora 或user1的默认shell 找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); 使用echo命令输出一个绝对路径,使用grep取出基名,扩展取出其路径名 找出IFCONIFG命令结果的1-255之间的数字 挑战题:写一个模式,能匹配出合理的IP地址 挑战题…

    Linux干货 2016-12-30
  • Shell脚本编程入门

    什么是Shell      操作系统最外层的程序,shell通过提示符让用户输入,向操作系统解释该输入,然后处理来自操作系统的任何结果输出来,管理用户与操作系统之间的交互。      Shell是一个用户跟操作系统之间的一个命令解释器。Shell是用户与Linux操作系统之间沟通…

    Linux干货 2016-08-15
  • Hive深入浅出

    1.  Hive是什么 1) Hive是什么? 这里引用 Hive wiki 上的介绍: Hive is a data warehouse infrastructure built on top of Hadoop. It provides tools to enable easy data ETL, a mechanism to put stru…

    Linux干货 2016-03-22
  • shell编程基础一(多种方法求值1+2+..+100)

    #SHELL编程基础一(多种方法求值1+2+..+100)##为什么要学好shell shell脚本语言是实现linux系统管理及自动化运维所必备的重要工具,linux系统的底层及基础应用软件的核心大都涉及shell脚本的内容。 每一个合格的linux系统管理员或运维工程师,都需要能够熟练地编写shell脚本语言,并能够阅读系统及各类软件…

    Linux干货 2017-08-26
  • sed初步认识

     ◆ sed基础概念 ◆ sed用法 一.sed 基础概念: Stream EDitor (流/行编辑器) sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕…

    2017-03-16
  • 非对称密钥加密解密

    两台计算机一台用公钥加密
    另一台解密

    2018-01-08