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

相关推荐

  • linux入门基础命令总结

    命令基础

    2017-09-18
  • Docker之~集群配置

    一、前言 Kubernetes 是Google开源的容器集群管理系统,基于Docker构建一个容器的调度服务,提供资源调度、均衡容灾、服务注册、动态扩缩容等功能套件,目前最新版本为0.6.2。 本文介绍如何基于Centos7.0构建Kubernetes平台,在正式介绍之前,大家有必要先理解Kubernetes几个核心概念及其承担的功能。以下为Kubernet…

    2015-03-23
  • 性能调优概述

    大纲: 一、概述 二、什么是性能调优?(what) 三、为什么需要性能调优?(why) 四、什么时候需要性能调优?(when) 五、什么地方需要性能调优?(where) 六、什么人来进行性能调优?(who) 七、怎么样进行性能调优?(How) 八、总结 注,硬件配置:CUP Xeon E5620 x 2 8核心, 内存 16G , 硬盘 RAID 10,操作…

    Linux干货 2015-02-10
  • Linux磁盘阵列RAID以及mdadm实现软件RAID

    一、概要 RAID(磁盘阵列):由众多价格较便宜的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘提供数据所产生加成效果来提升整个磁盘的性能。 二、RAID的级别及其优缺点 读性能提升 写性能提升 容错能力 组合后空间大小 RAID0 上升 上升 无 n*单个磁盘大小 RAID1 上升 下降 有(最多坏一块磁盘) 1*单个磁盘大小 RAID4 上升 上升 有(…

    2015-04-20
  • mysql基础概念笔记 part1

    mysql基础概念笔记     part1#wmd-preview h1 { color: #0077bb; /* 将标题改为蓝色 */} mysql基础概念笔记     part1 mysql 基础概念 基础原理,逻辑架构,事务,并发控制,读写锁 1、前言     作为一个运维…

    Linux干货 2016-09-19
  • 【译文】如何正确设定MySQL程序的选项

    MySQL手册文档版本:5.6  章节:4.2 Using MySQL Programs  略去了部分windows下使用mysql的相关的说明 译者:古二娃 时间:2015-03-15 章节目录: 4.2 Using MySQL Programs 4.2.1 Invoking MySQL Programs …

    Linux干货 2015-03-10