Linux - 仅保存最近的 10 个文件夹并删除其余文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6024088/
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
Linux - Save only recent 10 folders and delete the rest
提问by Ran
I have a folder that contains versions of my application, each time I upload a new version a new sub-folder is created for it, the sub-folder name is the current timestamp, here is a printout of the main folder used (ls -l |grep ^d):
我有一个包含我的应用程序版本的文件夹,每次上传新版本时都会为其创建一个新的子文件夹,子文件夹名称是当前时间戳,这里是使用的主文件夹的打印输出(ls - l |grep ^d):
drwxrwxr-x 7 root root 4096 2011-03-31 16:18 20110331161649
drwxrwxr-x 7 root root 4096 2011-03-31 16:21 20110331161914
drwxrwxr-x 7 root root 4096 2011-03-31 16:53 20110331165035
drwxrwxr-x 7 root root 4096 2011-03-31 16:59 20110331165712
drwxrwxr-x 7 root root 4096 2011-04-03 20:18 20110403201607
drwxrwxr-x 7 root root 4096 2011-04-03 20:38 20110403203613
drwxrwxr-x 7 root root 4096 2011-04-04 14:39 20110405143725
drwxrwxr-x 7 root root 4096 2011-04-06 15:24 20110406151805
drwxrwxr-x 7 root root 4096 2011-04-06 15:36 20110406153157
drwxrwxr-x 7 root root 4096 2011-04-06 16:02 20110406155913
drwxrwxr-x 7 root root 4096 2011-04-10 21:10 20110410210928
drwxrwxr-x 7 root root 4096 2011-04-10 21:50 20110410214939
drwxrwxr-x 7 root root 4096 2011-04-10 22:15 20110410221414
drwxrwxr-x 7 root root 4096 2011-04-11 22:19 20110411221810
drwxrwxr-x 7 root root 4096 2011-05-01 21:30 20110501212953
drwxrwxr-x 7 root root 4096 2011-05-01 23:02 20110501230121
drwxrwxr-x 7 root root 4096 2011-05-03 21:57 20110503215252
drwxrwxr-x 7 root root 4096 2011-05-06 16:17 20110506161546
drwxrwxr-x 7 root root 4096 2011-05-11 10:00 20110511095709
drwxrwxr-x 7 root root 4096 2011-05-11 10:13 20110511100938
drwxrwxr-x 7 root root 4096 2011-05-12 14:34 20110512143143
drwxrwxr-x 7 root root 4096 2011-05-13 22:13 20110513220824
drwxrwxr-x 7 root root 4096 2011-05-14 22:26 20110514222548
drwxrwxr-x 7 root root 4096 2011-05-14 23:03 20110514230258
I'm looking for a command that will leave the last 10 versions (sub-folders) and deletes the rest.
我正在寻找一个将保留最后 10 个版本(子文件夹)并删除其余版本的命令。
Any thoughts?
有什么想法吗?
采纳答案by AhmetB - Google
There you go. (edited)
你去吧。(已编辑)
ls -dt */ | tail -n +11 | xargs rm -rf
ls -dt */ | tail -n +11 | xargs rm -rf
First list directories recently modified then take all of them except first 10, then send them to rm -rf
.
首先列出最近修改过的目录,然后获取除前 10 个之外的所有目录,然后将它们发送到rm -rf
.
回答by linuts
EDIT:
编辑:
find . -maxdepth 1 -type d ! -name \.| sort | tac | sed -e '1,10d' | xargs rm -rf
回答by Gilles 'SO- stop being evil'
Your directory names are sorted in chronological order, which makes this easy. The list of directories in chronological order is just *
, or [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
to be more precise. So you want to delete all but the last 10 of them.
您的目录名称按时间顺序排序,这很容易。按时间顺序排列的目录列表只是*
,或者[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
更准确地说。所以你想删除除最后 10 个之外的所有内容。
set [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
while [ $# -gt 10 ]; do
rm -rf ""
shift
fi
(While there are more than 10 directories left, delete the oldest one.)
(虽然还剩 10 个以上的目录,请删除最旧的一个。)
回答by Tilo
ls -lt | grep ^d | sed -e '1,10d' | awk '{sub(/.* /, ""); print }' | xargs rm -rf
Explanation:
解释:
- list all contents of current directory in chronological order (most recent files first)
- filter out all the directories
- ignore the 10 first lines / directories
use awk to extract the file names from the remaining 'ls -l' output
remove the files
- 按时间顺序列出当前目录的所有内容(最近的文件在前)
- 过滤掉所有目录
- 忽略前 10 行/目录
使用 awk 从剩余的 'ls -l' 输出中提取文件名
删除文件
回答by Thme
ls -dt1 /path/to/folder/*/ | sed '11,$p' | rm -r
this assumes those are the only directories and no others are present in the working directory.
这假设这些是唯一的目录,工作目录中不存在其他目录。
ls -dt1
will normally only print the newest directory however the/*/
will only match directories and print their full paths the1
ensures one line per match/listingt
sorts time with newest at the top.sed
takes the 11th line on down to the bottom and prints only those lines, which are then passed torm
.
ls -dt1
通常只会打印最新的目录,但是/*/
只会匹配目录并打印它们的完整路径,以1
确保每个匹配/列表中的一行t
排序时间与最新的在顶部。sed
将第 11 行放到底部并仅打印那些行,然后将这些行传递给rm
.
You can use xargs, but for testing you may wish to remove | rm -r
to see if the directories are listed properly first.
您可以使用 xargs,但为了进行测试,您可能希望先删除| rm -r
以查看目录是否首先正确列出。
回答by arx-e
If the directories' names contain the date one can delete all but the last 10 directories with the default alphabetical sort
如果目录的名称包含日期,则可以删除除最后 10 个目录之外的所有目录,并使用默认字母排序
ls -d */ | head -n -10 | xargs rm -rf