I'm looking for a generalizable solution to this problem, 'cause I'm sure it will come up again.

I have a client that does medical research. Periodically we have to remove the datasets provided by the local health authority, and attest to the fact that we have securely done so.

We use rsnapshot with a retention of

7 dailies, 
4 weeklies, 
12 monthlies, 
4 yearlies.

I recently needed to purge the rsnapshot tree of years-old data that looked a lot like this in the source location:

/path/to/studyName/data files yyyy mm 01/bunchOfFiles
/path/to/studyName/data files yyyy mm 02/bunchOfFiles
/path/to/studyName/data files yyyy mm 03/bunchOfFiles
/path/to/studyName/randomly placed and named data file 1
/path/to/studyName/randomly named admin file to keep 1
/path/to/studyName/randomly placed and named data file 2
/path/to/studyName/randomly placed and named data file 3
/path/to/studyName/randomly named admin file to keep 2
/path/to/studyName/randomly named admin file to keep 3
/path/to/studyName/randomly placed and named data file 4
/path/to/studyName/randomly named admin file to keep n
/path/to/studyName/randomly placed and named data file 5

Note that the ./data files* sub-dirs also occur in many other study locations, so they are not useful as find targets.

What I would have liked to have done would involve find with a couple of nested -exec commands. But find can't do that.

I tried nesting a -exec using bash -c within the first -exec command. After stubbing my toes on all sorts of things I needed to handle (like the spaces in the subdirectory names), my timer ran out, and it became more cost effective to just make a giant collection of rm commands to delete each instance of the file (or sub-directory containing data files).

for example, if it was possible to nest -execs, I think this would work:

find /path/to/rsnapshot_tree -type d -name studyName / 
  -exec 'find {}  -type d -iname "data files* -exec rm -rf {} \;'

OK - it can't work because of the ambiguity of the -exec termination and {}, but think of it as pseudo code


  • find each instance of the directory "studyName" in the rshapshot tree, then apply rm -rf to the 3 directories described by "data files*"
  • Then repeat for each instance of misplaced data files in the
    studyName directory.

6 commands, instead of the >150 I had to copy, paste, edit and get right in a script.

Any suggestions about a better way would be much appreciated.

I have rsnapshot trees hosted on both linux (debian/ubuntu) and freeBSD hosts. Bash is always the shell (I change the default on the freeBSD hosts)

  • 1
    Not related to your question, but why retaining 4 weeklies and not 5? Check out the calendar, and see that sometimes you may want that 5th weekly backup/snapshot. – boardrider Jan 30 '16 at 11:18

your question is not entirely clear to me, but if you have a dir structure as follows --


and you're looking for studyName/data/fileX, you could do --

find  . -path "*studyName/data*" -type f
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.