Bash scripting: Deleting the oldest directory

本小妞迷上赌 提交于 2019-11-29 10:49:37

This is not pretty but it works:

rm -R $(ls -lt | grep '^d' | tail -1  | tr " " "\n" | tail -1)
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 "-".

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
You should remove the echo before the rm if it produces the right results
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!