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 {} \;
删除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
参考:
https://javawind.net/p132
http://man.linuxde.net/find