find:查找目录下的文件

2019-12-09  本文已影响0人  August________

find:查找目录下的文件

功能说明

语法格式

find [-H] [-L] [-P] [-D debuggopts] [-Olevel] [Pathname] [expression]

选项说明

参数选项 解释说明
-depth 从指定目录的最深层目录查找
-maxdepth levels 查找最后目录级数
-regextype type 修改正则表达式的模式
参数选项 解释说明
-mtime[-n,n,+n] 按照文件的修改时间来查找文件
-atime[-n,n,+n] 按照文件的访问时间来查找文件
-ctime[-n,n,+n] 按照文件的状态改变时间来查找文件
-group 按照文件的所属组来查找文件
-name 按照文件名查找文件
-newer 查找更改时间比指定文件新的文件
-nogroup 查找没有有效用户组的文件
-nouser 查找没有有效主的文件
-perm 按照文件权限来查找文件
-regex 接正则表达式
-iregex 接正则表达式,不区分大小写
-size 查找文件为长度为n块的文件
-user 按照文件属猪来查找文件
-type b(块设备文件)
c(字符设备文件)
d(目录)
p(管道文件)
l(字符链接文件)
f(普通文件)
s(socket文件)
D(door)
-delete 将查找的文件删除
-exec 对匹配的文件执行该参数所给出的shell命令
-ok 与-exec作业相同
-prune 是find的命令不在当前指定目录查找
-print 将匹配的文件输出到标准输出

使用范例

#find /var/log/ -mtime +5 -name '*.log'
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log

#ls
1.txt  abc

[root@lhf2 17:19:05 /root/script]
#find . -type f
./1.txt

[root@lhf2 17:19:08 /root/script]
#find . ! -type f
.
./abc

#ls -ld abc/
drwxr-xr-x. 2 root root 6 Dec  9 17:18 abc/

[root@lhf2 17:20:06 /root/script]
#find . -perm 755
.
./ab
#find . -size -3k
.
./1.txt
./abc

#find . -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 4 Dec  9 17:15 ./1.txt

#find /var/log/ -name "*.log" -ok rm {} \;
< rm ... /var/log/tuned/tuned.log > ? 
< rm ... /var/log/audit/audit.log > ? 

#find /root/script/ -type f -name "*.txt" |xargs rm -f
#find /root/script/ -type f -name "*.txt" -exec rm {} \;

上一篇 下一篇

猜你喜欢

热点阅读