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

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

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

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

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

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 | while read f;  
do   
echo $f;  
# do something  
done  

 

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

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

 

 

参考:

https://javawind.net/p132

http://man.linuxde.net/find

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

posted @ 2017-04-21 09:52  脚本小娃子  阅读(28574)  评论(0编辑  收藏  举报