[英]Bash scripting: Deleting the oldest directory
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
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. ls -1t | tail -1
确实给了我最老的目录,问题是它没有删除目录,而且它也列出了文件。
How could I please fix that? 我怎么能解决这个问题?
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 $(ls -lt | grep '^d' | tail -1 | tr " " "\n" | tail -1)
rm -R $(ls -tl | grep '^d' | tail -1 | cut -d' ' -f8)
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
如果它产生正确的结果,你应该删除rm之前的回声
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.