find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
当前目录及子目录下查找所有以 .txt
或 .pdf
结尾的文件
find . \( -name "*.txt" -o -name "*.pdf" \) 或 find . -name "*.txt" -o -name "*.pdf"
找出/home下不是以.txt结尾的文件
find /home ! -name "*.txt"
查找在指定时间曾被存取过的文件或目录,单位以分钟计算
搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录
查找在指定时间曾被存取过的文件或目录,单位以24小时计算
搜索最近七天内被访问过的所有文件
find . -type f -atime -7
搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
搜索超过七天内被访问过的所有文件
find . -type f -atime +7
查找在指定时间之时被更改过的文件或目录
查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;
查找在指定时间之时被更改的文件或目录,单位以24小时计算
从本日开始计算时间
删除当前目录下所有.txt文件
find . -type f -name "*.txt" -delete
从指定目录下最深层的子目录开始查找
寻找文件大小为0 Byte的文件,或目录下没有任何子目录或文件的空目录
要列出所有长度为零的文件
find . -empty
假设find指令的回传值为True,就执行该指令
找出当前目录下所有root的文件,并把所有权更改为用户tom
find .-type f -user root -exec chown tom {} \;
上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。
查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> all.txt
将30天前的.log文件移动到old目录中
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
因为单行命令中-exec参数中无法使用多个命令,以下方法可以实现在-exec之后接受多条命令
-exec ./text.sh {} \;
将find指令的回传值皆设为False
此参数的效果和指定“-ls”参数类似,但会把结果保存为指定的列表文件
排除符号连接
此参数的效果和指定“-print”参数类似,但会把结果保存成指定的列表文件
此参数的效果和指定“-print0”参数类似,但会把结果保存成指定的列表文件
此参数的效果和指定“-printf”参数类似,但会把结果保存成指定的列表文件
只寻找该文件系统类型下的文件或目录
查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
find . -path "./sk" -prune -o -name "*.txt" -print
查找符合指定之群组识别码的文件或目录
查找符合指定之群组名称的文件或目录
在线帮助
此参数的效果和指定“-lname”参数类似,但忽略字符大小写的差别
此参数的效果和指定“-name”参数类似,但忽略字符大小写的差别
查找符合指定的inode编号的文件或目录
此参数的效果和指定“-path”参数类似,但忽略字符大小写的差别
此参数的效果和指定“-regexe”参数类似,但忽略字符大小写的差别
忽略大小写
find . -iregex ".*\(\.txt\|\.pdf\)$"
查找符合指定的硬连接数目的文件或目录
指定字符串作为寻找符号连接的范本样式
忽略大小写
find /home -iname "*.txt"
假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出
设置最大目录层级
向下最大深度限制为3
find . -maxdepth 3 -type f
设置最小目录层级
搜索出深度距离当前目录至少2个子目录的所有文件
find . -mindepth 2 -type f
查找在指定时间曾被更改过的文件或目录,单位以分钟计算
此参数的效果和指定“-xdev”相同
查找在指定时间曾被更改过的文件或目录,单位以24小时计算
To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:
find /directory_path -mtime -1 -ls
The
-
before1
is important - it means anything changed one day or less ago. A+
before1
would instead mean anything changed at least one day ago, while having nothing before the1
would have meant it was changed exacted one day ago, no more, no less.
指定字符串作为寻找文件或目录的范本样式
在/home
目录下查找以.txt结尾的文件名
find /home -name "*.txt"
查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录
找出比file.log修改时间更长的所有文件
find . -type f -newer file.log
找出不属于本地主机群组识别码的文件或目录
不去考虑目录至少需拥有两个硬连接存在
找出不属于本地主机用户识别码的文件或目录
此参数的效果和指定“-exec”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”,则放弃执行命令
找出自己家目录下所有的.txt文件并删除
find $HOME/. -name "*.txt" -ok rm {} \;
上例中,-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
匹配文件路径或者文件
find /usr/ -path "*local*"
查找符合指定的权限数值的文件或目录
当前目录下搜索出权限为777的文件
find . -type f -perm 777
找出当前目录下权限不是644的php文件
find . -type f -name "*.php" ! -perm 644
假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串
假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为全部的名称皆在同一行
假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式可以自行指定
不寻找字符串作为寻找文件或目录的范本样式
if you want to exclude the misc
directory just add a -path ./misc -prune -o
to your find command:
find . -path ./misc -prune -o -name '*.txt' -print
Here is an example with multiple directories:
find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print
Here we exclude dir1, dir2 and dir3, since in find
expressions it is an action, that acts on the criteria -path dir1 -o -path dir2 -o -path dir3
(if dir1 or dir2 or dir3), ANDed with type -d
. Further action is -o print
, just print.
基于正则表达式匹配文件路径
find . -regex ".*\(\.txt\|\.pdf\)$"
find . -name "file-$a.sh" -o -name "file-$b.sh"
To combine it into one using
-regex
option:
On OSX
find -E . -regex ".*file-($a|$b)\.txt"
On Linux:
find . -regextype posix-extended -regex ".*file-($a|$b)\.txt"
查找符合指定的文件大小的文件
文件大小单元:
搜索大于10KB的文件
find . -type f -size +10k
搜索小于10KB的文件
find . -type f -size -10k
搜索等于10KB的文件
find . -type f -size 10k
将find指令的回传值皆设为True
只寻找符合指定的文件类型的文件
类型参数列表:
f 普通文件 l 符号连接 d 目录 c 字符设备 b 块设备 s 套接字 p Fifo
使用命令行对一个目录进行递归搜索和替换
# OSX version find . -type f -name '*.txt' -exec sed -i '' s/this/that/g {} +
查找符合指定的用户识别码的文件或目录
查找文件或目录被更改之后在指定时间曾被存取过的文件或目录,单位以日计算
查找符和指定的拥有者名称的文件或目录
找出当前目录用户tom拥有的所有文件
find . -type f -user tom
找出当前目录用户组sunk拥有的所有文件
find . -type f -group sunk
显示版本信息
将范围局限在先行的文件系统中
此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查