delete all folders/files and keep only the last 10 in a folder

Hi,

I want to write a script that deletes all folders and keep the last 10 recent folders.

I know the following:

ls -ltr will sort the folders from old to recent.
ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning)
I want to get the 10th folder from the bottom. HOW?
Once I get it, I can run: find . ! -newer <this_object> -exec rm {} \;

Thanks,

PS: Any other suggestion is appreciated :wink:

Do a man on tail

thanks

ls -ltr | awk '{print $9}' | tail -10 | head -1

will give the file. How to put it in the next command:
find . ! -newer <HERE> -exec rm {} \;

thx.

cd test
FILE0=`ls -ltr | awk '{print $9}' | tail -11 | head -1`
find . ! -newer $FILE0 -exec rm -r {} \;