Prevent rmdir -p from traversing above a certain directory

Posted by thepurplepixel on Super User See other posts from Super User or by thepurplepixel
Published on 2010-04-22T15:23:03Z Indexed on 2010/04/22 15:34 UTC
Read the original article Hit count: 177

Filed under:
|
|
|
|

I hacked together this script to rsync some files over ssh. The --remove-source-files option of rsync seems to remove the files it transfers, which is what I want. However, I also want the directories those files are placed in to be gone as well.

The current part of the find command, -exec rmdir -p {} ; tries to remove the parent directory (in this case, /srv/torrents), but fails because it doesn't have the right permissions. What I'd like to do is stop rmdir from traversing above the directory find is run in, or find another solution to get rid of all the empty folders. I've thought of using some kind of loop with find and running rmdir without the -p switch, but I thought it wouldn't work out.

Essentially, is there an alternative way to remove all the empty directories under the parent directory? Thanks in advance!

#!/bin/bash

HOST='<hostname>'
USER='<username>'
DIR='<destination directory>'
SOURCE='/srv/torrents/'

rsync -e "ssh -l $USER" --remove-source-files -h -4 -r --stats -m --progress -i $SOURCE $HOST:$DIR
find $SOURCE -mindepth 1 -type d -empty -prune -exec rmdir -p \{\} \;

© Super User or respective owner

Related posts about linux

Related posts about bash