Bash 命令删除除最后 5 个目录之外的所有目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4126869/
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
Bash command to delete all but last 5 directories
提问by Lito
Possible Duplicate:
Delete all but the most recent X files in bash
I have a script to create incremental backups daily and I need to delete all backups but last 5.
我有一个脚本来每天创建增量备份,我需要删除所有备份,但最后 5 个。
For example, I have this folders:
例如,我有这个文件夹:
drwxr-xr-x 4 root root 4096 Oct 29 01:10 2010-10-29 drwxr-xr-x 4 root root 4096 Oct 30 01:10 2010-10-30 drwxr-xr-x 4 root root 4096 Oct 31 01:10 2010-10-31 drwxr-xr-x 4 root root 4096 Nov 1 01:10 2010-11-01 drwxr-xr-x 4 root root 4096 Nov 2 01:10 2010-11-02 drwxr-xr-x 4 root root 4096 Nov 3 01:10 2010-11-03 drwxr-xr-x 4 root root 4096 Nov 4 01:10 2010-11-04 drwxr-xr-x 4 root root 4096 Nov 5 01:10 2010-11-05 drwxr-xr-x 4 root root 4096 Nov 6 01:10 2010-11-06 drwxr-xr-x 4 root root 4096 Nov 7 01:10 2010-11-07 drwxr-xr-x 4 root root 4096 Nov 8 01:10 2010-11-08
And I need to maintain only the last 5 directories and delete the others. After command execute, I need to have only this:
我只需要维护最后 5 个目录并删除其他目录。命令执行后,我只需要这个:
drwxr-xr-x 4 root root 4096 Nov 4 01:10 2010-11-04 drwxr-xr-x 4 root root 4096 Nov 5 01:10 2010-11-05 drwxr-xr-x 4 root root 4096 Nov 6 01:10 2010-11-06 drwxr-xr-x 4 root root 4096 Nov 7 01:10 2010-11-07 drwxr-xr-x 4 root root 4096 Nov 8 01:10 2010-11-08
I don't need to delete previous to 5 days, I need to delete all except 5 last directories :)
我不需要删除前 5 天,我需要删除除最后 5 个目录之外的所有目录:)
Now I'm using:
现在我正在使用:
find /backup/increment -maxdepth 1 -type d -mtime +5 -exec rm -rf {} \;
find /backup/increment -maxdepth 1 -type d -mtime +5 -exec rm -rf {} \;
But I need to improved not based in time :)
但我需要改进而不是基于时间:)
EDIT:This is an example for a server that do backups all days, but I need an script that delete all folders previous to last 5 because my computer do backups at 00:10 at night, but not all nights the backup is done it, because my computer isn't working all days, and I need to have always the last 5 backups :)
编辑:这是一个整天都在做备份的服务器的例子,但我需要一个脚本来删除最后 5 个之前的所有文件夹,因为我的电脑在晚上 00:10 做备份,但不是所有的晚上备份都完成了,因为我的电脑不是整天都在工作,我需要总是有最后 5 个备份:)
回答by MartinStettner
use the tailcommand to print lines starting with the nth line (Option -n +N):
使用tail命令打印从第n行开始的行(选项-n +N):
rm `ls -t | tail -n +6`
ls -toutputs the current directory sorted by time. tail -n +6takes al lines starting with the 6th line. Quoting with backticks feeds the result of the pipe into the rmcommand.
ls -t输出按时间排序的当前目录。tail -n +6从第 6 行开始取所有行。用反引号引用将管道的结果输入到rm命令中。
OLD SOLUTION, not correct ...
旧解决方案,不正确......
use the headcommand, which prints the first nlines of some output:
使用该head命令,它打印某些输出的前n行:
rm `ls -t1 | head -n 5`
ls -toutputs the current directory sorted by time. head -n 5takes the first five entries of the previous output. Quoting with backticks feeds the result of the pipe into the rmcommand.
ls -t输出按时间排序的当前目录。head -n 5获取前一个输出的前五个条目。用反引号引用将管道的结果输入到rm命令中。
Please try out first before applying to live data :) ...
在申请实时数据之前,请先试用:) ...
回答by emrea
The first thing that came to my mind. It's not elegant:
我想到的第一件事。这并不优雅:
a=0;
for i in `ls -t`;
do
a=`expr $a + 1`;
if [ $a -gt 5 ]; then
echo "removing $i";
rm -rf $i
fi;
done
回答by frankc
ls -tr | perl -ne '{@files = <>; print @files[0..$#files-5 ]}' | xargs -n1 echo rm -rf
ls -tr | perl -ne '{@files = <>; 打印@files[0..$#files-5 ]}' | xargs -n1 回声 rm -rf
You would remove the echo before the rm -rf to get it to work.
您将在 rm -rf 之前删除回声以使其工作。
回答by Max E.
The trick will be the -t option to ls, which sorts by modification time, from newest to oldest.
技巧是 ls 的 -t 选项,它按修改时间从最新到最旧排序。
A really naive solution, using a temporary file, might go this way:
一个非常幼稚的解决方案,使用临时文件,可能会这样:
ls -t > /tmp/file_list
num_files_to_keep=5
# wc -l gets the line count of a file
# first word of wc output is the actual line count, and that's all we need, so
# delete everything after the space.
num_files=`wc -l /tmp/file_list | sed "s/ .*//"`
#if appropriate you should add a check for num_files < num_files_to_keep
num_files_to_delete=$(( $num_files - $num_files_to_keep ))
rm `tail -n $num_files_to_delete /tmp/file_list`
回答by zengr
create two dummy files with the start and the end date
创建两个带有开始日期和结束日期的虚拟文件
touch -t 1010290000 before
touch -t 2011042359 after
find all the files between the 2 dummy files and "rm -rf" the result
找到 2 个虚拟文件之间的所有文件,然后“rm -rf”结果
find . -newer before \! -newer after -exec rm -rf {} \;

