语法格式
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [pathname] [expression]
find [选项] [路径] [操作语句]
参数选项
pathname 命令所查找的目录路径,例如用 “.” 来表示当前目录,用 “/” 来表示系统根目录。
Options模块
-depth 从指定目录下最深层的子目录开始查找
-maxdepth levels 查找的最大目录级数,levels为自然数
-regextype type 改变正则表达式的模式。默认为emacs,还有posix-awk、posix-basic、posix-egrep、posix-extended
Tests模块
-mtime[-n|n|+n] 按照文件的修改时间来查找文件(这个参数最有用),-n 表示文件更改时间距现在n天以内;+n表示文件更改时间距现在n天以前;n是距现在第n天。
-atime[-n|n|+n] 按照文件的访问时间来查找文件,单位为天。
-ctime[-n|n|+n] 按照文件的状态改变时间来查找文件,单位为天。
-amin 按照文件的访问时间来查找文件,单位为分钟。
-cmin 按照文件的状态改变时间来查找文件,单位为分钟。
-mmin 按照文件的修改时间来查找文件,单位为分钟。
-group 按照文件所属的组来查找文件。
-name 安装文件名查找文件,只支持 *、?、[] 等特殊通配符。
-never 查找更改时间比指定文件新的文件。
-nogroup 查找没有有效用户组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找没有有效属主的文件,即该文件属猪在/etc/passwd中不存在。
-path pattern 指定路径样式,配合 -prune 参数排除指定目录。
-perm 安装文件权限来查找文件。
-regex 按正则表达式。
-iregex 按正则表达式,不区分大小写。
-size n[cwbkMG] 查找文件长度为n块的文件,带有 cwbkMG 时表示文件长度以字节计。
-user 按照文件属主来查找文件。
-type 查找某一类型的文件,如:b(块设备文件),c(字符设备文件),d(目录),p(管道文件),l(符号链接文件),f(普通文件),s(socket文件),D(door)。
Actions模块
-delete 将查找出的文件删除。
-exec 对匹配的文件执行该参数所给的shell命令。
-ok 和 -exec作用相同,但在执行每个命令之前,都会让用户下你确定是否执行。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找。
-print 将匹配的文件输出到标准输出(默认功能,使用中可省略)。
OPERATORS find支持逻辑运算符。
! 取反。
-a 取交集,全拼为 and。
-o 取并集,全拼为 or。
范例:查找指定时间内修改过的文件
在 /data 查找修改时间在5天以内的文件,-5表示文件更改时间距现在5天以内。+5表示文件更改时间距现在5天以前。5表示距现在第5天。
find /data/ -mtime -5
[root@testdb62 test]# touch 1210;touch -m -t 12101500 1210
[root@testdb62 test]# touch 1211;touch -m -t 12111500 1211
[root@testdb62 test]# touch 1212;touch -m -t 12121500 1212
[root@testdb62 test]# touch 1213;touch -m -t 12131500 1213
[root@testdb62 test]# touch 1214;touch -m -t 12141500 1214
[root@testdb62 test]# touch 1215;touch -m -t 12151500 1215
[root@testdb62 test]# ls -l --full-time
total 0
-rw-r--r-- 1 root root 0 2020-12-10 15:00:00.000000000 +0800 1210
-rw-r--r-- 1 root root 0 2020-12-11 15:00:00.000000000 +0800 1211
-rw-r--r-- 1 root root 0 2020-12-12 15:00:00.000000000 +0800 1212
-rw-r--r-- 1 root root 0 2020-12-13 15:00:00.000000000 +0800 1213
-rw-r--r-- 1 root root 0 2020-12-14 15:00:00.000000000 +0800 1214
-rw-r--r-- 1 root root 0 2020-12-15 15:00:00.000000000 +0800 1215
[root@testdb62 test]# date
Tue Dec 15 15:05:10 CST 2020
[root@testdb62 test]# find . -mtime -2
.
./1214
./1215
[root@testdb62 test]# find . -mtime +2
./1210
./1211
./1212
[root@testdb62 test]# find . -mtime 2
./1213
[root@testdb62 test]# find . -mtime -4
.
./1212
./1213
./1214
./1215
[root@testdb62 test]# find . -mtime +4
./1210
[root@testdb62 test]# find . -mtime 4
./1211
范例:用-name指定关键字查找
find /var/log/ -mtime +5 -name '*.log'
范例:利用"!" 反向查找
find . -type d #按类型查找,d代表目录,查找当前目录下的所有目录
find . ! -type d #"!"表示取反,查找不是目录的文件,注意感叹号的位置
范例:安装目录或者文件的权限来查找文件
find /data/ -perm 755
范例:按大小找文件
find . -size +10G
范例:查找时忽略某个目录
find /data -path "/data/mysql" -prune -o -print
范例:查找时忽略多个目录
find /data -path \( -path /data/dir2 -o -path /data/dir3) -prune -o -print
范例:使用user和nouser选项
find . -user mysql #查找用户为 mysql 的文件
find . -nouser #查找没有对应任何用户的文件
范例:使用group和nogroup选项
find . -group nobody # 查找用户组为nobody的文件
find . -nogroup # 查找没有对应用户组的文件
范例:查找比某个文件新或旧的文件
find . -never filex.log # 在当前目录查找更改时间比文件filex.log 新的文件
find . -never filex.log ! -never filey.log #查找更改时间比文件filex.log新,但比filey.log文件旧的文件
范例:逻辑操作符的使用
find . -maxdepth 1 -type d # maxdepth 1 查找一级目录
find . -maxdepth 1 -type d ! -name "go"
find . -maxdepth 1 -type d ! -name "go" -o -name "java" # -o 是或者的意思
find . -maxdepth 1 -type d ! -name "go" -a -name "php" # -a 是并且的意思
范例:find正则表达式
find / -regex "find"
find / -regex ".*find"
find / -regex ".*/find"
范例:ls -l 命令放在find命令的 -exec 选项中执行
find . -type f -exec ls -l {} \;
范例:在目录中查找更改时间在n天以前的文件,并删除他们
find . -type f -mtime +14 -exec rm {} \;
范例:使用 -exec选项的安全模式 -ok
#说明:-y 删除文件,-n 不删除文件
find . -name "*.log" -mtime +15 -ok rm {} \;
范例:ls -l 命令放在find命令的xargs后
find . -name "*.log"|xargs ls -l
范例:使用xargs执行mv命令
方法一:
find . -name "*.log"|xargs -i mv {} /tmp # xargs的--i参数使得{} 可代替find找到的内容
方法二:
find . -name "*.log"|xargs mv -t {} /tmp # mv 命令的 -t选项可以颠倒源和目标
范例:进入 /root 目录下的 data目录,删除 a.log 文件
find /root/data -type f -name "*a.log" | xargs rm -f
范例:在 /root/data 目录及其子目录下的所有扩展名 ".sh" 结尾的文件中,把包含 "./hostlists.txt" 的字符串全部替换成 "../idctest_iplist"
sed -i 's#./hostlists.txt#../idctest_iplist#g' `find /root/data -name "*.sh" `
范例:将 /root/data 目录下,所有以 "*.sh" 结尾的普通文件打包
方法一:
tar zcvf xxl_name.tar.gz `find /root/data -type f -name "*.sh" `
方法二:
find /root/data -type f -name "*.sh"|xargs tar zcvf xxl_name.tar.gz
范例:删除一个目录下的所有文件,但是保留一个文件
方法一:(推荐的方法)
find /root/data -type f ! -name "a.sh" | xargs rm -f
方法二:(文件多时效率低)
find /root/data -type f ! -name "a.sh" -exec rm -f {} \;