每天一个Linux命令:find(20)

find

find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)

格式

find [选项] [参数]

参数选项

参数 备注
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如: b - 块设备文件;d - 目录;c - 字符设备文件;p - 管道文件;l - 符号链接文件;f - 普通文件。
-size n: [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype: 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount: 在查找文件时不跨越文件系统mount点。
-follow: 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio: 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
-amin n 查找系统中最后N分钟访问的文件
-atime n 查找系统中最后n*24小时访问的文件
-cmin n 查找系统中最后N分钟被改变文件状态的文件
-ctime n 查找系统中最后n*24小时被改变文件状态的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-print 假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串
-exec (命令) 假设find指令的回传值为True,就执行该指令;

实例

  • 在指定目录下查找指定时间内修改过的文件

    命令: **find /root -atime -1 **

[root@VM_0_9_centos ~]# find /root -atime -1
/root/.cache/abrt/lastnotification
/root/mysql.password
/root/.mysql_history
/root/.viminfo

查找24小时内修改过的文件

  • 根据关键字查找

    命令: find . -name "*.log"

[root@VM_0_9_centos ~]# find . -name "*.log"
./myLog.log

在当前目录查找 以.log结尾的文件。 ". "代表当前目录

  • 按照目录或文件的权限来查找文件

    命令: **find . -perm 644 **

[root@VM_0_9_centos ~]# ls -al # 显示文件权限
total 161228
dr-xr-x---.  6 root root      4096 Nov  4 22:51 .
dr-xr-xr-x. 20 root root      4096 Nov  5 21:09 ..
-rw-------   1 root root     25151 Nov  5 21:09 .bash_history
-rw-r--r--.  1 root root        18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root       176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root       176 Dec 29  2013 .bashrc
drwxr-xr-x   3 root root      4096 Dec 11  2017 .cache
drwxr-xr-x   3 root root      4096 Dec 11  2017 .config
-rw-r--r--.  1 root root       100 Dec 29  2013 .cshrc
-rw-r--r--   1 root root 159019376 Jul  6 14:54 jdk-8u11-linux-x64.tar.gz
-rw-------   1 root root       122 Nov  4 14:41 .lesshst
-rw-r--r--   1 root root       229 Nov  3 20:59 myFile
-rw-r--r--   1 root root        91 Nov  2 15:43 myFile2
-rw-r--r--   1 root root    857836 Nov  4 23:15 myLog.log
-rw-------   1 root root       611 Nov  4 22:49 .mysql_history
-rw-r--r--   1 root root        31 Nov  4 22:51 mysql.password
drwxr-xr-x   2 root root      4096 Jul  6 01:39 .pip
-rw-r--r--   1 root root        73 Jul  6 01:39 .pydistutils.cfg
-rw-r--r--   1 root root   4930939 May 25  2017 rabbitmq-server-3.6.10-1.el7.noarch.rpm
drwx------   2 root root      4096 Apr 12  2018 .ssh
-rw-r--r--.  1 root root       129 Dec 29  2013 .tcshrc
-rw-------   1 root root      4434 Nov  4 22:51 .viminfo
[root@VM_0_9_centos ~]# 
[root@VM_0_9_centos ~]# find . -perm 644  # 查找权限为rw-r--r--的文件 
./.tcshrc
./.bashrc
./myLog.log
./.pip/pip.conf
./.cshrc
./jdk-8u11-linux-x64.tar.gz
./.bash_logout
./mysql.password
./rabbitmq-server-3.6.10-1.el7.noarch.rpm
./.bash_profile
./.pydistutils.cfg
./myFile2
./myFile

查找/opt/soft/test/目录下 权限为 777的文件

  • 按类型查找

    命令: find . -type f -name "*.log"

[root@VM_0_9_centos ~]# find . -type f -name "*.log"
./myLog.log

查找当目录,以.log结尾的普通文件

  • 查找当前所有目录并排序

    命令: find . -type d | sort

[root@VM_0_9_centos ~]#  find . -type d | sort
.
./.cache
./.cache/abrt
./.config
./.config/abrt
./.pip
./.ssh
[root@VM_0_9_centos ~]# 
  • 按大小查找文件

    命令: find . -size +1024c -print

[root@VM_0_9_centos ~]# find . -size +1024c -print
.
./.bash_history
./myLog.log
./.cache
./.cache/abrt
./.pip
./.config
./.config/abrt
./jdk-8u11-linux-x64.tar.gz
./.ssh
./rabbitmq-server-3.6.10-1.el7.noarch.rpm
./.viminfo

查找当前目录大于1K的文件

补充

find命令的-exec选项,可以结合其它命令用于进一步过滤或执行其它操作。-exec参数后面跟其它命令,以;为终止,考虑不同系统关于分号的含义可能有所不同,一般会加上反斜杠。{}花括号表示查询出来的文件

  • ls -l命令放在find命令的-exec选项中

    命令: find . -name "myFile*" -exec ls -l {} \;

[root@VM_0_9_centos ~]# find . -name "myFile*" -exec ls -l {} \;
-rw-r--r-- 1 root root 91 Nov  2 15:43 ./myFile2
-rw-r--r-- 1 root root 229 Nov  3 20:59 ./myFile 
  • 查询到文件并执行删除操作

    命令: find . -name "myFile2*" -exec rm {} \;

[root@VM_0_9_centos ~]# find . -name "myFile2" -exec rm {} \;
[root@VM_0_9_centos ~]# ll
total 161132
-rw-r--r-- 1 root root 159019376 Jul  6 14:54 jdk-8u11-linux-x64.tar.gz
-rw-r--r-- 1 root root       229 Nov  3 20:59 myFile #只剩下myFile,myFile2文件被删除了
-rw-r--r-- 1 root root    857836 Nov  4 23:15 myLog.log
-rw-r--r-- 1 root root        31 Nov  4 22:51 mysql.password
-rw-r--r-- 1 root root   4930939 May 25  2017 rabbitmq-server-3.6.10-1.el7.noarch.rpm
  • 配合grep命令使用

    命令: find /etc -name "passwd" -exec "root" {} \;

[root@VM_0_9_centos ~]# find /etc -name "passwd" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

find命令首先匹配所有文件名为“passwd"的文件,如passwd 、passwd.old、passwd.bak,然后执行grep命令查看这些文件中是否存在一个root用户

相关

  • 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:
    which 查看可执行文件的位置,通过环境变量PATH定义的路径下搜索可执行文件。
    whereis 查看文件的位置,包括man手册文件,源文件,二进制文件等。
    locate 配合数据库查看文件位置。
    find 实际搜寻硬盘查询文件名称。

参考

posted @ 2019-11-05 21:24  didi516  阅读(210)  评论(0编辑  收藏  举报