Bash scripting: Deleting the oldest directory

前端 未结 4 1131

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 -

4条回答
  •  一向
    一向 (楼主)
    2020-12-19 08:41

    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 "-".

提交回复
热议问题