How to delete empty source directories when moving folders with rsync?

Solution 1:

Using "rm -rf" has an inherent race condition, you could namely delete files that were just created between the rsync and the rm invocations.

I prefer to use:

rsync --remove-source-files -a server:incoming/ incoming/ && 
ssh server find incoming -depth -type d -delete

This will NOT remove the directories if they are not empty.

Solution 2:

I ended up in a similar situation. I wanted to avoid rm -rf, knowing that rsync failed to copy some files, and I didn't want to loose them.

To simply delete the empty directories, I found this one the most convenient (from sourcedir):

find . -type d -empty -delete

Solution 3:

I haven't found a command that does that in one go, but since rsync still performs better than rm -rf (specially if you have a very large number of directories) here are two rsync command that do a "move":

rsync -av --ignore-existing --remove-source-files source/ destination/ && \
rsync -av --delete `mktemp -d`/ source/ && rmdir source/

Solution 4:

Why not just add rm -rf source_directory after your rsync ?

rsync -axvvES --remove-source-files source_directory /destination/ && rm -rf source_directory

Each command-line program is (idealy) made to do a specified task, and it's up to you to glue several together to accomplish more complex tasks.