Bash 脚本:删除最旧的目录

发布于 2024-09-16 09:54:43 字数 171 浏览 2 评论 0原文

我想查找最旧的目录(目录内),然后将其删除。我正在使用以下

rm -R $(ls -1t | tail -1)

命令: ls -1t | tail -1 确实给了我最旧的目录,问题是它没有删除该目录,而且它还列出了文件。

我该如何解决这个问题?

I want to look for the oldest directory (inside a directory), and delete it. I am using the following:

rm -R $(ls -1t | tail -1)

ls -1t | tail -1 does indeed gives me the oldest directory, the the problem is that it is not deleting the directory, and that it also list files.

How could I please fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初见 2024-09-23 09:54:43
rm -R "$(find . -maxdepth 1 -type d -printf '%T@\t%p\n' | sort -r | tail -n 1 | sed 's/[0-9]*\.[0-9]*\t//')"

这也适用于名称包含空格、制表符或以“-”开头的目录。

rm -R "$(find . -maxdepth 1 -type d -printf '%T@\t%p\n' | sort -r | tail -n 1 | sed 's/[0-9]*\.[0-9]*\t//')"

This works also with directory whose name contains spaces, tabs or starts with a "-".

错爱 2024-09-23 09:54:43

这并不漂亮,但它有效:

rm -R $(ls -lt | grep '^d' | tail -1  | tr " " "\n" | tail -1)

This is not pretty but it works:

rm -R $(ls -lt | grep '^d' | tail -1  | tr " " "\n" | tail -1)
倒带 2024-09-23 09:54:43
rm -R $(ls -tl | grep '^d' | tail -1 | cut -d' ' -f8)
rm -R $(ls -tl | grep '^d' | tail -1 | cut -d' ' -f8)
她比我温柔 2024-09-23 09:54:43

find directory_name -type d -printf "%TY%Tm%Td%TH%TM%TS %p\n" | sort -nr | tail -1 | cut -d" " -f2 | xargs -n1 echo rm -Rf

You should remove the echo before the rm if it produces the right results


find directory_name -type d -printf "%TY%Tm%Td%TH%TM%TS %p\n" | sort -nr | tail -1 | cut -d" " -f2 | xargs -n1 echo rm -Rf


You should remove the echo before the rm if it produces the right results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文