当前位置: 首页 > Python编程 > Python编程实战技能 > Python编程基础入门 > python文件查找之find命令

python文件查找之find命令

发布时间:2020年09月27日 10:08:50 来源: 点击量:469

【摘要】shell编程-文件查找之find命令1 语法格式find [路劲][选项][操作]选项参数对照表2 -name 查找 etc 目录下以 conf结尾的文件find etc

shell编程-文件查找之find命令

1.语法格式

find [路劲][选项][操作]

选项参数对照表

2.-name   

查找/etc/目录下以.conf结尾的文件

find /etc/ -name "*.conf"

-iname   不区分大小写

find /etc/ -iname "*.conf"

-user      查找当前目录为root用户的文件

find ./ -user root

3.-type   

文件的类型

f     文件

d    目录

c    字符设备文件

b    块设备文件

l     链接文件

p    管道文件 

find . -type f

find . -type d

4.-size

文件大小

-n    小与n的文件

+n   大于n的文件

 查找/etc目录下小与100k的文件

find /etc -size -100k

查找/etc目录下大于1M的文件

find /etc -size +1M

相关推荐:《Python视频教程》

5.-mtime

修改时间

-n    n天以内修改的文件

+n   n天以外修改的文件

n     正好n天修改的文件

 查找/etc目录下5天之内修改并且以conf结尾的文件

find /etc -mtime -5 -name '*.conf'

查找/etc目录下10天之前修改并且属主为root的文件

find /etc -mtime +10 -user root

6.-mmin

-n    n分钟以内修改的文件

+n   n分钟以外修改的文件

修改/etc目录下30分钟以内修改的目录

find /etc -mmin -30 -type d

7.-mindepth 

表示从n级子目录开始搜索

find /etc -mindepth 3 -type -f

-madepth n

表示最多搜索到n-1级子目录

8.操作-exec

对搜索的文件常用操作

-print   打印输出

-exec    对文件执行特定的操作

-ok        和exec功能意义,只是每次操作都会给用户提示

-exec的格式为

-exec 'command' {}

例子一:

搜索/home/shell_learn/下的文件,文件名以.sh结尾,且修改时间在一个星期之内的,然后将其删除

#打印
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -print
#复制
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -exec cp {} /home/shell_learn/test/ ;
#删除
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -exec rm -rf {} ;

9.locate命令

locate不同于find命令是在整块磁盘中搜索,locate命令是在数据库文件中查找

find是默认全局匹配,locate则是默认部分匹配

updatedb命令

用户更新/var/lib/mlocate/mlocate.db

所使用的配置文件/etc/updatedb.conf

 实例:updatedb命令把文件更新到数据库(默认是第二天系统才会自动更新到数据库),否则locate查找不到

[root@VM_0_9_centos shell_learn]# touch 789.txt
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# locate 789.txt
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# updatedb
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# locate 789.txt
/home/shell_learn/789.txt
[root@VM_0_9_centos shell_learn]#

10 .whereis命令

实例

[root@VM_0_9_centos shell_learn]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# whereis -b mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz
[root@VM_0_9_centos shell_learn]#

11.which

作用:仅查找二进制程序文件

[root@VM_0_9_centos shell_learn]# which mysql

/usr/bin/mysql

[root@VM_0_9_centos shell_learn]# 

12.各查找命令总结

分享到: 编辑:wangmin

就业培训申请领取
您的姓名
您的电话
意向课程
点击领取

环球青藤

官方QQ

扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群

绑定手机号

应《中华人民共和国网络安全法》加强实名认证机制要求,同时为更加全面的体验产品服务,烦请您绑定手机号.

预约成功

本直播为付费学员的直播课节

请您购买课程后再预约

环球青藤移动课堂APP 直播、听课。职达未来!

安卓版

下载

iPhone版

下载
环球青藤官方微信服务平台

刷题看课 APP下载

免费直播 一键购课

代报名等人工服务

课程咨询 学员服务 公众号

扫描关注微信公众号

APP

扫描下载APP

返回顶部