I want to find some dirs and delete all but the 10 latest dirs.
I can find . -maxdepth 1 -type d -name "XXX*" | xargs rm -rf
but that would remove all of them.
I can't use head / tail
because I don't know the length of the list. I thought of piping the result to wc -l
and then doing subtracting 10 from it and then rm -rf
'ing it, but I don't know how...
What I came up till now is this:
find . -maxdepth 1 -type d -name "XXX*" | # get the dirs list
wc -l | # count number
xargs -I{} expr {} - $(find . -maxdepth 1 -type d -name "XXX*") # send number to xargs and try to use expr to subtract from it the tail of the find
I'm stuck in the expr
part and how to cut the list where I want
Here you are:
I have tested it, working fine. Note current directory
"."
also counts if-name "*"
From
man head
: