linux清某段时间内的文件,批量删除linux的文件;find方法批量删除文件;find查找某时间段内的所有文件...

1、如图所示,有大量文件夹,想批量删除它们

89dee941578ac1f54bbf208d20faefe5.png

2、使用命令 find . -maxdepth 1  -regex ".*ws.*" 可以批量找到他们。maxdepth值为1表示只在当前目录查找,不递归查找其子目录

ce643cb5ab1a1a6af97e98336a4a5f79.png

3、使用命令 find . -maxdepth 1  -regex ".*ws.*" -exec rm -rf {} ; 批量删除它们,这个世界瞬间清爽了很多

ps注意后面的分号,不要省略啊

a784b54d27858231eba20d7a2d9b062a.png

4、使用命令 find . -maxdepth 1  -regex ".*ws.*" | xargs rm -rf 同样可以批量删除

xargs是把前面的输出作为后面的参数,如果多行输出,就多次执行后面的命令

5、有的linux系统支持的regex正则表达式不一样,可以使用下面的方式替换

find . -maxdepth 1 -name "*ws*" | xargs rm -rf

6、find使用正则:

find . -regex ".*.(txt|sh)"

加参数“-regextype type”可以指定“type”类型的正则语法,find支持的正则语法有:valid types are `findutils-default', `awk', `egrep', `ed', `emacs', `gnu-awk', `grep', `posix-awk', `posix-basic', `posix-egrep', `posix-extended', `posix-minimal-basic', `sed'.

显示20分钟前的文件

find /home/prestat/bills/test -type f -mmin +20 -exec ls -l {} ;

find /home/prestat/bills/test -type f -mmin +20 -exec ls -l {} +

删除20分钟前的文件

find /home/prestat/bills/test -type f -mmin +20 -exec rm {} ;

显示20天前的目录

find /home/prestat/bills/test -type d -mtime +20 -exec ls -l {} ;

删除20天前的目录

find /home/prestat/bills/test -type d -mtime +20 -exec rm {} ;

在20-50天内修改过的文件

find ./ -mtime +20 -a -mtime -50 -type f

排除某些目录:

find ${JENKINS_HOME}/jobs -maxdepth 1  -name "*" -mtime +60! -path /var/lib/jenkins/jobs | xargs ls -ld;

排除某些文件:

find ${JENKINS_HOME}/jobs -maxdepth 1  ! -name "*.xml" -mtime +60 ! -path /var/lib/jenkins/jobs | xargs ls -ld;

理解下两种写法的区别:对结果没有影响;{}表示find找到的列表

find . -exec grep chrome {} ;

or

find . -exec grep chrome {} +

find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.

find . -type f | whileread f;doecho $f;

#dosomething

done

另外,可以使用-ok替换-exec操作:-ok相比-exec多了提示的功能,每一步都需要用户确认,这样对于rm操作,更加安全

举例:找出内容为空的文件删除

9b6c582c6264a2c90eb7475ff465e3af.png

参考:

https://javawind.net/p132

http://man.linuxde.net/find

https://unix.stackexchange.com/questions/12902/how-to-run-find-exec

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值