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

16,155

Solution 1

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

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

Share:
16,155

Related videos on Youtube

ylluminate
Author by

ylluminate

Updated on September 18, 2022

Comments

  • ylluminate
    ylluminate over 1 year

    rsync can be coaxed into moving folders very similarly and superiorly to traditional mv with the following options:

    rsync -axvvES --remove-source-files source_directory /destination/
    

    However one thing I cannot yet seem to get it to do is to remove original directories. The --remove-source-files flag does just that, removes the source files, but not also source directories. I wish there were a --remove-source-directories flag as well, but there's not.

    How can I deal with this? I suppose I can just issue an rm -fr after the move, however I'd prefer to keep the procedure all in one process vs introducing a potential for mistakes.

    • Nils
      Nils about 12 years
      What is your reason for not using mv?
    • ylluminate
      ylluminate about 12 years
      Different volumes and I need to preserve all attributes, including times.
    • dathor
      dathor almost 11 years
    • Sridhar Sarnobat
      Sridhar Sarnobat about 5 years
      I'm very surprised rsync has no option for this, especially when it does at the destination side. I end up doing find . -type d -empty | xargs rmdir -p
    • ylluminate
      ylluminate about 5 years
      You know @Sridhar-Sarnobat I can't help but wonder if there's a way to pass results from an rsync session into a list that is then processed following the process that then cleans it up... Hmm.
  • ylluminate
    ylluminate about 12 years
    I think you're right. I believe I ultimately have to drill down to the bottom as well first as I cannot get caught just moving the way rsync normally does. Probably need to use find -depth. Would you have any suggestion as to a quick algorithmic approach to getting down to the bottom of the dir stack, mv'ing with rsync, rm'ing as you note, and then stepping back up the tree?
  • Charlie Gorichanaz
    Charlie Gorichanaz about 7 years
    Note you apparently may need to add -depth on OSX or others.
  • Sridhar Sarnobat
    Sridhar Sarnobat over 4 years
    Yep, thank goodness for -empty, which I only found out recently and it's become very common for me to use.
  • Raúl Salinas-Monteagudo
    Raúl Salinas-Monteagudo over 3 years
    Thanks @CharlieGorichanaz, done
  • juanesarango
    juanesarango about 3 years
    Thank @lp_ This one should be updated as the best answer