Linux之find命令详解

网友投稿 935 2022-08-24

Linux之find命令详解

Linux之find命令详解

find命令:

可用来在指定目录下查找文件,如果find不指定任何参数,默认查找当前目录下的所有文件,

包括子目录。参数前的字符串都被视为要查找的目录名

命令格式:find [-path……] -options [-print -exec -ok]

path:要查找的目录路径。

print:表示将结果输出到标准输出。

exec:对匹配的文件执行该参数所给出的shell命令。

形式为command {} \;,注意{}与\;之间有空格

[root@python1 tmp]# find . -type f -name "t6" -exec rm {} \;

ok:与exec作用相同,

区别在于,在执行命令之前,都会给出提示,让用户确认是否执行

[root@python1 tmp]# find . -type f -name "t5" -ok rm {} \;< rm ... ./t5 > ? y # 输入y确认删除

xargs:除了-exec和-ok后可以执行shell命令外,

find结合xargs,也可以对查询结果执行shell命令

find . -type f -name "t7" -o -name "t8" | xargs rm -rf

常用参数详解:

options常用的有下选项:

-type:按照文件类型查找

f(文件)、d(目录)、l(链接文件)、s(socket文件)等

-name:按照名字查找

-size:按文件大小查找

-perm:安装权限查找

-mtime:按修改时间查找

-atime:按访问时间查找

-ctime:按属性修改时间查找

-prune:不再当前指定的目录下查找

-user:文件属主来查找

-group:文件所属组来查找

-nogroup:查找无有效所属组的文件

-nouser:查找无有效属主的文件

-inum:根据inode号查找 (ls -lih的第一个字段信息就是文件inode)

-maxdepth:按深度查找

使用案例:

1、按名字查找

在当前目录及子目录中,查找大写字母开头的txt文件

find . -name '[A-Z]*.txt' -print

在/etc及其子目录中,查找host开头的文件

find /etc -name 'host*' -print

在HOME目录及其子目录中,查找所有文件

find ~ -name '*' -print

在当前目录及子目录中,查找不是out开头的txt文件

find . -name "out*" -prune -o -name "*.txt" -print

2、按目录查找

在当前目录除aa之外的子目录内搜索 txt文件

find . -path "./aa" -prune -o -name "*.txt" -print

在当前目录及除aa和bb之外的子目录中查找txt文件

find . \( -path "./aa" -o -path "./bb" \) -prune -o -name "*.txt" -print

在当前目录,不再子目录中,查找txt文件

find . ! -name "." -type d -prune -o -type f -name "*.txt" -print

3、按权限查找

在当前目录及子目录中,查找属主具有读写执行,其他具有读执行权限的文件

find . -perm 755 -print

4、按类型查找

在当前目录及子目录下,查找符号链接文件

find . -type l -print

5、按属主及属组

查找属主是/ -user -type f -print

查找属主被删除的文件

find / -nouser -type f -print

查找属组mysql的文件

find / -group mysql -type f -print

查找用户组被删掉的文件

find / -nogroup -type f -print

6、按时间查找

时间表示意义:

4:第四天

-4:近4天内

+4:4天前

查找2天内被更改过的文件

find . -mtime -2 -type f -print

查找2天前被更改过的文件

find . -mtime +2 -type f -print

查找一天内被访问的文件

find . -atime -1 -type f -print

查找一天前被访问的文件

find . -atime +1 -type f -print

查找一天内状态被改变的文件

find . -ctime -1 -type f -print

查找一天前状态被改变的文件

find . -ctime +1 -type f -print

查找10分钟以前状态被改变的文件

find . -cmin +10 -type f -print

7、按文件新旧

查找比aa.txt新的文件

find . -newer "aa.txt" -type f -print

查找比aa.txt旧的文件

find . ! -newer "aa.txt" -type f -print

查找比aa.txt新,比bb.txt旧的文件

find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print

8、按大小查找

查找超过1M的文件

find / -size +1M -type f -print

查找等于6字节的文件

find . -size 6c -print

查找小于32k的文件

find . -size -32k -print

9、按深度查找

find . -maxdepth 2 . ./nsswitch.conf ./pki ./pki/rpm-gpg ./pki/java ./pki/ca-trust ./pki/tls ./pki/nss-legacy

find参数还可以使用逻辑运算符

-a: and

-o: or

!: 非

find -name "t9" -o -name "t10" ./t10 ./t9 find -name "t*" -a -inum "393236" ./t10 find ! -name "t10" ./t{5-10} ./t9 ./uwsgi.ini ./t1 ./t3 ./lt1 ./test

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:iOS获取设备信息与应用信息(iphone获取设备信息)
下一篇:c语言程序设计案例教程(第2版)笔记(六)—字符串处理实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~