Linux - 仅保存最近的 10 个文件夹并删除其余的
我有一个包含应用程序版本的文件夹,每次上传新版本时都会为其创建一个新的子文件夹,子文件夹名称是当前时间戳,这里是所使用的主文件夹的打印输出(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
我正在寻找一个命令,该命令将保留最后 10 个版本(子文件夹)并删除其余版本。
有什么想法吗?
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):
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.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就这样吧。 (已编辑)
ls -dt */ |尾-n +11 | xargs rm -rf
首先列出最近修改的目录,然后取出除前 10 个之外的所有目录,然后将它们发送到
rm -rf
。There you go. (edited)
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
.这假设这些是唯一的目录,并且工作目录中不存在其他目录。
ls -dt1
通常只会打印最新的目录,但是/*/
会只匹配目录并打印其完整路径
1
确保一个每个匹配/列表的行
t
对时间进行排序,最新的位于顶部。sed
从第 11 行一直到底部并仅打印这些行,然后将其传递给rm
。您可以使用 xargs,但为了进行测试,您可能希望删除
| 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/*/
willonly match directories and print their full paths the
1
ensures oneline per match/listing
t
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
.You can use xargs, but for testing you may wish to remove
| rm -r
to see if the directories are listed properly first.如果目录名称包含日期,则可以按默认字母顺序删除除最后 10 个目录之外的所有目录
If the directories' names contain the date one can delete all but the last 10 directories with the default alphabetical sort
说明:
使用 awk 从剩余的 'ls -l' 输出中提取文件名
删除文件
Explanation:
use awk to extract the file names from the remaining 'ls -l' output
remove the files
编辑:
EDIT:
我建议遵循以下顺序。我在 Synology NAS 上使用类似方法来删除旧备份。它不依赖于文件夹名称,而是使用上次修改时间来决定要删除哪些文件夹。它还使用零终止来正确处理文件夹名称中的引号、空格和换行符:
重要:这将删除所有匹配的文件夹!我强烈建议首先进行测试运行,将最后一个命令 xargs -0 -r rm -rf 替换为 xargs -0 ,这将回显匹配的文件夹而不是删除它们。
每个步骤的简短说明:
find /path/to/folder -maxdepth 1 -mindepth 1 -type d -printf '%Ts\t' -print0
查找所有目录(
-type d) 直接在备份文件夹 (
-maxdepth 1
) 内,备份文件夹本身 (-mindepth 1
) 除外,打印 (-printf
) 上次修改的 Unix 时间 (%Ts
) 后跟制表符(\t
,在步骤 4 中使用)和完整文件名,后跟一个空字符 (-print0
)。sort -rnz
排序 以零结尾的项目 (
-z
)从上一步中使用数值比较(-n
)并反转顺序(-r
)。结果是按上次修改时间降序排列的所有文件夹的列表。tail -n +11 -z
打印最后几行 (
tail
) 从上一步开始,从第 11 行 (-n +11
) 开始,考虑每行都以零结尾 (-z
)。这将从剩余步骤中排除最新的 10 个文件夹(按修改时间)。cut -f2- -z
剪切从第二个字段开始的每一行,直到结尾(< code>-f2-) 将每一行视为以零结尾 (
-z
),以获取包含 10 天以上的每个文件夹的完整路径的列表。xargs -r -0 rm -rf
获取上一步中的零终止 (
-0
) 项 (xargs
),并且,如果有的话(如果没有非空白字符,-r
避免运行传递给 xargs 的命令) , 强制删除(rm -rf
) 他们。I suggest the following sequence. I use a similar approach on my Synology NAS to delete old backups. It doesn't rely on the folder names, instead it uses the last modified time to decide which folders to delete. It also uses zero-termination in order to correctly handle quotes, spaces and newline characters in the folder names:
IMPORTANT: This will delete any matching folders! I strongly recommend doing a test run first by replacing the last command
xargs -0 -r rm -rf
withxargs -0
which will echo the matching folders instead of deleting them.A short explanation of each step:
find /path/to/folder -maxdepth 1 -mindepth 1 -type d -printf '%Ts\t' -print0
Find all directories (
-type d
) directly inside the backup folder (-maxdepth 1
) except the backup folder itself (-mindepth 1
), print (-printf
) the Unix time (%Ts
) of the last modification followed by a tab character (\t
, used in step 4) and the full file name followed by a null character (-print0
).sort -rnz
Sort the zero-terminated items (
-z
) from the previous step using a numerical comparison (-n
) and reverse the order (-r
). The result is a list of all folders sorted by their last modification time in descending order.tail -n +11 -z
Print the last lines (
tail
) from the previous step starting from line 11 (-n +11
) considering each line as zero-terminated (-z
). This excludes the newest 10 folders (by modification time) from the remaining steps.cut -f2- -z
Cut each line from the second field until the end (
-f2-
) treating each line as zero-terminaded (-z
) to obtain a list containing the full path to each folder older than 10 days.xargs -r -0 rm -rf
Take the zero-terminated (
-0
) items from the previous step (xargs
), and, if there are any (-r
avoids running the command passed to xargs if there are no nonblank characters), force delete (rm -rf
) 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 个之外的所有内容。(当剩余的目录超过 10 个时,删除最旧的一个。)
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.(While there are more than 10 directories left, delete the oldest one.)