简体   繁体   English

删除目录中的旧文件

[英]delete old files in a directory

okay maybe this sounds simple, but it has been a bit challenging to me 好吧,这听起来很简单,但是对我来说有点挑战

I have a directory called backups and it has (backup files + other files) 我有一个名为backups的目录,它具有(备份文件+其他文件)

backups files: 备份文件:

../backups/backup-2013_03_03.zip
../backups/backup-2013_03_05.zip
../backups/backup-2013_01_01.zip
../backups/backup-2013_08_16.zip
../backups/backup-2013_02_28.zip
../backups/backup-2013_01_21.zip
../backups/backup-2013_03_29.zip
../backups/backup-2013_04_05.zip

I'm trying to delete backup files older than 90 days. 我正在尝试删除90天以上的备份文件。

 find /var/tmp/stuff -mtime +90 -print | xargs /bin/rm

seems to work, but I'm not able to limit the search to backup files only. 似乎有效,但我无法将搜索限制为仅备份文件。 "files which starts with backup*" “以备份开头的文件*”

I have tried adding "-iname backup" option to find command argument, thinking it would do the trick but it doesn't seems to work. 我尝试添加“ -iname备份”选项来查找命令参数,认为它可以解决问题,但似乎不起作用。 Any ideas? 有任何想法吗?

Thank you 谢谢

The parameter to iname matches against the full filename, so you need a trailing wildcard: iname的参数与完整文件名匹配,因此您需要结尾的通配符:

find /var/tmp/stuff -mtime +90 -iname "backup*" -print | xargs /bin/rm

You could also use find's -exec argument, but personally I find the syntax quite arcane. 您也可以使用find的-exec参数,但就我个人而言,该语法相当神秘。 I prefer xargs. 我更喜欢xargs。 find /var/tmp/stuff -mtime +90 -iname "backup*" -exec /bin/rm '{}'

Or, as damienfrancois points out, GNU find can take a -delete argument. 或者,正如damienfrancois指出的那样,GNU find可以采用-delete参数。 This is the best solution because a) it is shorter and b) it is more efficient because the deletion happens within the find process. 这是最好的解决方案,因为a)较短,b)效率较高,因为删除发生在find过程中。 exec and xargs will both spawn one new process per file to delete. execxargs都会为每个要删除的文件生成一个新进程。 Source: GNU manual However, as wildplasser points out, it could also be dangerous - -delete will remove directories by default. 来源: GNU手册然而,作为wildplasser指出,也有可能是危险的- -delete将被默认删除目录。 To only delete files, use -type f . 要仅删除文件,请使用-type f

find /var/tmp/stuff -type f -mtime +90 -iname "backup*" -delete

You can pipe through grep before calling rm. 您可以在调用rm之前通过grep进行管道传输。 Something like: 就像是:

find /var/tmp/stuff -mtime +90 -print | grep 'backup-' | xargs /bin/rm

while the find utility has all kinds of options to single handedly do this, including the deleting as noted in other answers, I can never remember any but the most basic options. 尽管find实用程序具有各种选项可以单手执行此操作,包括其他答案中提到的删除,但我只记得最基本的选项。

find "stuff" | grep "some_other_stuff" | xargs "do_stuff"

seems much easier to remember for me. 对我来说似乎更容易记住。

You could use -exec option of find along with -iname . 您可以将find -exec选项与-iname一起使用。 Since you want to delete only files, you would need to specify -type f 由于只想删除文件,因此需要指定-type f

find /var/tmp/stuff -type f -iname 'backup*' -mtime +90 -exec rm {} +

If you prefer xargs like me 如果您喜欢我的xargs

find /var/tmp/stuff -type f -iname 'backup*' -mtime +90 -print0 | xargs -0 rm

Note : It's recommended to use find -print0 with xargs -0 to avoid weird file name caveats 注意:建议使用find -print0xargs -0以避免奇怪的文件名警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM