Linux find 命令详解

简介:
Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

语法:

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} ;

参数说明:

-name: 按照文件名查找。
-iname:按照文件名查找,忽略大小写。
-perm: 按照文件权限来查找文件。
-user: 按照文件属主来查找文件。
-group:按照文件所属的组来查找文件。
-mtime -n +n:按照文件更改时间来查找。【-7:7天之内;+7:7天前】
-nogroup:查找无效属组的文件。
-nouser: 查找无效属主的文件。
-newer file1 !file2 查找更改时间比文件 file1 新,但比 file2 旧的文件。
-type:查找某一类型的文件,如:
      b:块设备文件
      d:目录
      f:普通文件
      c:字符设备文件
      p:管道文件
      l:符号链接文件
      s:socket
-size n:[c] 查找文件长度为n块的文件,带有c表示文件长度以字节计算。
-depth: 查找文件时,首先查找当前目录中文件,然后再查找子目录中文件。
-follow:查找find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件。
-amin n: 在过去 n 分钟内被读取过。
-anewer file: 比文件 file 更晚被读取过的文件。
-atime n : 在过去n天内被读取过的文件。
-cmin n :  在过去 n 分钟内被修改过。
-cnewer file :比文件 file 更新的文件。
-ctime n : 在过去n天内被修改过的文件。
-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写。
-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name。
你可以使用 ( ) 将运算式分隔,并使用下列运算。
exp1 -and exp2
! expr
-not expr
exp1 -or exp2
exp1, exp2

查找普通文件和目录:

find /home/yoon -type f  (普通文件)

find /home/yoon -type d  (普通目录)

只显示1级目录文件且过滤自身:

find ./ -maxdepth 1  -type d  ! -name "hankyoon"

查找一天内被访问过的文件:

find /home/yoon -atime -1 -type f

除了某个文件以为,其余的均删除:

find /home/yoon -type f ! -name 1.sql -and -type f ! -name 1.sql | xarges rm -rf

ls | grep -v "yoon.txt" | xarges rm -rf

删除目录下所有文件:

find /home/yoon -type f -exec rm -rf {} ;

find /home/yoon -type f | xarges rm -rf

删除7天以前的普通文件:

find /home/yoon -type f -mtime +7 -exec rm -rf {} ;

删除7天以前的普通文件,在删除之前询问它们:

find /home/yoon -type f mtime +7 -ok rm -rf {} ;

查找系统中所有文件长度为0的普通文件,并列出完整路径:

find / -type f -size 0 -exec ls -l {} ;

按照目录或者文件权限来查找:

find /home/yoon -perm 777

按照时间来查找:

find / -ctime  +20  最近修改文件时间20分钟以前

find / -mtime  +7   修改文件为7天之前的(最重要)

find / -mtime  7    修改文件为第7天,就是往前推7天

find / -mtime  -7   修改文件为7天之内的
原文地址:https://www.cnblogs.com/hankyoon/p/13650847.html