bash 删除旧的备份文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1426434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove old backup files
提问by shantanuo
# find /home/shantanu -name 'my_stops*' | xargs ls -lt | head -2
The command mentioned above will list the latest 2 files having my_stops in it's name. I want to keep these 2 files. But I want to delete all other files starting with "my_stops" from the current directory.
上面提到的命令将列出名称中包含 my_stops 的最新 2 个文件。我想保留这两个文件。但我想从当前目录中删除以“my_stops”开头的所有其他文件。
回答by pavium
If you create backups on a regular basis, it may be useful to use the -atime option of find so only files older than your last two backups can be selected for deletion.
如果您定期创建备份,则使用 find 的 -atime 选项可能会很有用,这样只能选择比最近两次备份更旧的文件进行删除。
For daily backups you might use
对于您可能使用的每日备份
$ find /home/shantanu -atime +2 -name 'my_stops*' -exec rm {} \;
but a different expression (other than -atime) may suit you better.
但不同的表达方式(除 -atime 外)可能更适合您。
In the example I used +2 to mean more than 2 days.
在示例中,我使用 +2 表示超过 2 天。
回答by Hai Vu
Here is a non-recursive solution:
这是一个非递归解决方案:
ls -t my_stops* | awk 'NR>2 {system("rm \"" find /home/folder/ -maxdepth 1 -name "*.jpg" -mtime +2
"\"")}'
Explanation:
解释:
- The ls command lists files with the latest 2 on top
- The awk command states that for those lines (NR = number of records, i.e. lines) greater than 2, delete them
- The quote characters are needed just in case the file names have embedded spaces
- ls 命令列出最新的 2 个文件
- awk 命令声明对于那些大于 2 的行(NR = 记录数,即行数),删除它们
- 仅在文件名包含嵌入空格的情况下才需要引号字符
回答by Cexar Augusto Landazabal Cuerv
Without recursive approach:
没有递归方法:
##代码##回答by beggs
回答by Carlos Tasada
That will show you from the second line forward ;)
这将从第二行开始向您展示;)
find /home/shantanu -name 'my_stops*' | xargs ls -lt | tail -n +2
find /home/shantanu -name 'my_stops*' | xargs ls -lt | 尾 -n +2
Just keep in mind that find is recursive ;)
请记住, find 是递归的;)

