views:

69

answers:

4

Hello,

I have a simple Bash question which I do not seem to be able to get right.

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?

Thank you very much,

Jary

A: 
rm -R $(ls -tl | grep '^d' | tail -1 | cut -d' ' -f8)
dave
sorry, it's a bit ugly...
dave
A: 

This is not pretty but it works:

rm -R $(ls -lt | grep '^d' | tail -1  | tr " " "\n" | tail -1)
codaddict
Thanks a lot! Looks good to me.
Jary
Be careful: it does not work if the directory name contains spaces.
Giuseppe Cardone
A: 
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 "-".

Giuseppe Cardone
A: 
frankc