There are several different topics on stackoverflow regarding find/removing the oldest directory/files in a directory . I have read a lot of them and seen a myriad different ways that obviously work for some people on other systems but not for me in my particular case.
The constraints are:
The closest i have gotten is something like this (not complete):
find . -maxdepth 1 -d -name "Backup Set*" -print0 | xargs -0 stat -f "%m %N" | sort -r| awk 'NR>5'
This gives me the directories that I want to delete however they now have timestamps prepended, which I am not sure that if i strip out and pipe to rm i will be back to a situation where i cannot delete directories with spaces in them.
output:
1450241540 ./Backup Set1
1450241538 ./Backup Set0
Thanks for any help here.
relevant posts that I have looked at:
https://superuser.com/questions/552600/how-can-i-find-the-oldest-file-in-a-directory-tree
Bash scripting: Deleting the oldest directory
Delete all but the most recent X files in bash
... | awk 'NR>5' | while read -r timestamp filename; do rm -rf "${filename}"; done
When there are more fields than variables specified in read, the remainder is simply lumped together in the last specified variable. In our case, we extract the timestamp and use everything else as-is for the file name. We iterate the output and run an rm for each entry.
I would recommend doing a test run with echo instead of rm so you can verify the results first.
Alternately, if you'd prefer an xargs -0 version:
... | awk 'NR>5' | while read -r timestamp filename; do printf "%s\0" "${filename}"; done | xargs -0 rm -rf
This also uses the while read but prints each filename with a null byte delimiter which can be ingested by xargs -0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With