Linux基础知识(五)-文件查找命令find

1、显示当前系统上root、fedora或user1用户的默认shell;

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

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

[root@localhost ~]# cat /etc/rc.d/init.d/functions | grep -E -o "\<.*\>\(\)"

## 或者 ##

[root@localhost ~]# grep  -E  -o  "[_[:alnum:]]+\(\)"  /etc/rc.d/init.d/functions

3、使用echo命令输出一个路径,使用grep取出其基名;

扩展:取出其路径名;

[root@localhost ~]# echo /etc/sysconfig | grep -E -o "[^/]+$"
sysconfig   #从尾部取到非/的部分

## 或者 ##

[root@localhost ~]# echo /etc/sysconfig/ | grep -E -o "[^/]+/?$"
sysconfig/
## 扩展:取路径名 ##
[root@localhost ~]# echo /etc/sysconfig/ | grep -E -o "^/[[:alnum:]]+[^/]"
/etc
[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -E -o  "^/[[:alnum:]]*[^/]"   #多层路径用这样的匹配是没办法取到的,应该用下面的

/etc
[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -E -o "/.*/"

/etc/rc.d/init.d/

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])\>"

5、写一个模式,能匹配出合理的IP地址;

[root@localhost ~]# ifconfig | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
192.168.1.106
255.255.255.0
192.168.1.255
127.0.0.1
255.0.0.0

6、写一个模式,能匹配出所有的邮件地址;

[root@localhost ~]# cat /scripts/emailpreg.txt | grep -E -o "[a-zA-Z0-9_-]*@[a-zA-Z0-9_-]*\.[a-zA-Z]*$"
123@qq.com
a34@163.com
a43ll@gmail.com
hello_world@abc.cn

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

[root@localhost ~]# find /var -user root -a -group mail -ls
134321240    4 drwxrwxr-x   2 root     mail         4096 Oct 19 16:28 /var/spool/mail
135041050  408 -rw-------   1 root     mail       414950 Oct 19 16:28 /var/spool/mail/root
##或者##
[root@localhost ~]# find /var -user root -a -group mail -exec ls -ldh {} \;
drwxrwxr-x. 2 root mail 4.0K Oct 19 16:31 /var/spool/mail
-rw-------. 1 root mail 410K Oct 19 16:31 /var/spool/mail/root

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

[root@localhost ~]# find  /  \( -nouser -o -nogroup \) -exec ls -ldh {} \;
drwx------. 2 1005 distro 59 Oct  3 07:58 /home/mandriva
-rw-r--r--. 1 1005 distro 18 Nov 20  2015 /home/mandriva/.bash_logout
-rw-r--r--. 1 1005 distro 193 Nov 20  2015 /home/mandriva/.bash_profile
-rw-r--r--. 1 1005 distro 231 Nov 20  2015 /home/mandriva/.bashrc

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

[root@localhost ~]# find  /  \( -nouser -o -nogroup \) -atime -3 -exec ls -ldh {} \;
drwx------. 2 1005 distro 59 Oct  3 07:58 /home/mandriva
[root@localhost ~]# stat /home/mandriva | grep -i access
Access: (0700/drwx------)  Uid: ( 1005/ UNKNOWN)   Gid: ( 2016/  distro)
Access: 2016-10-19 16:36:46.077491511 -0400 #访问时间
[root@localhost ~]# date +'%F %H:%M:%S'
2016-10-19 16:55:03 #当前时间,证明文件缺失在3天内被访问过

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

[root@localhost ~]# find /etc -perm -222 -ls

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

[root@localhost ~]# find /etc -size +1M -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 6.7M Aug 23 12:14 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 3.7M Nov 20  2015 /etc/selinux/targeted/policy/policy.29

11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;

[root@localhost ~]# find /etc/init.d/ -perm -113 -type f -ls

12、查找/usr目录下不属于root、bin或hadoop的文件;

[root@localhost ~]# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
134652726    0 drwx------   2 polkitd  root            6 Jun  9  2014 /usr/share/polkit-1/r
## 或者 ##
[root@localhost ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
134652726    0 drwx------   2 polkitd  root            6 Jun  9  2014 /usr/share/polkit-1/rules.d

13、查找/etc/目录下至少有一类用户没有写权限的文件;

[root@localhost ~]# find /etc -not -perm -222 -type f -ls

14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

[root@localhost ~]# find /etc -not \( -user root -a -user hadoop  \) -mtime -7 -ls

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

(0)
luoluoluoluo
上一篇 2016-10-24 09:09
下一篇 2016-10-24 09:09

相关推荐

  • scp和rsync的使用

    通过一些简单需求了解scp和rsync的使用

    2017-09-18
  • 第十二周:httpd和lamp

    看了一下作业内容,发现所有的都在51cto上写过了。这里就不在做了。 毕竟当时几篇博客写下来,还是非常繁琐的,不想在写。 文档地址:RHEL6.8编译安装LAMP环境:httpd-2.4+mysql5.6+php5.5;基于LAMP环境部署WordPress: http://afterdawn.blog.51cto.com/7503144/1876171 配…

    Linux干货 2016-12-09
  • 包管理及源码安装Apache

    一,概述 yum 仓库的安装 在/etc/yum.repos.d/目录下创建后缀名为repo的配置文件 [CentOS7] name= baseurl= gpgcheck= enabled= 配置文件基本包含的四个要求 安装及升级本地程序包: * localinstall rpmfile1 [rpmfile2] […] (用install替代) …

    Linux干货 2016-09-01
  • 第三周博客作业

    who useradd usermod groupadd

    Linux干货 2017-12-17
  • 22期第五周课堂练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@localhost ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又…

    Linux干货 2016-09-08
  • 马哥教育网络21期+第九周练习博客

    马哥教育网络21期+第九周练习博客 1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; [root@localhost bin]# cat 1.sh  #!/bin/bash # while&nbsp…

    Linux干货 2016-09-05