5

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

Community
  • 1
  • 1
skimon
  • 1,149
  • 2
  • 13
  • 26
  • does the `stat` output let you put in extra dbl-quotes, i.e.`stat -f "%m \"%N\%""` ? Good Q, Good luck. – shellter Dec 17 '15 at 22:59

2 Answers2

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

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • Thanks for this, I tried the former and it works - with the caveat that you have to run it from a script , I was not able to run it directly from the command line as i had been testing my commands this way. – skimon Dec 18 '15 at 02:31
0

piping this cut command cut -d ' ' -f 2- will take out the timestamp

simple example

echo '1450241538 ./Backup Set0'|cut -d ' ' -f 2-

will get rid of the timestamps

results will be

./Backup Set0
repzero
  • 8,254
  • 2
  • 18
  • 40