I use rsync to copy directories including files, with --remove-source-files I let rsync delete source files. Unfortunately it doesn't delete directories so I would like to delete all empty directories under SOURCE1 and SOURCE2. The find -exec rmdir command does this but unfortunately it also deletes the SOURCE directories itself



sudo rsync --remove-source-files --progress --ignore-existing -vr  /mnt/download/transmission/complete/ /mnt/dune/DuneHDD_1234
sudo rsync --remove-source-files --progress --ignore-existing -vr /mnt/download/sabnzbd/completed/ /mnt/dune/DuneHDD_1234
find $SOURCE1 -not -name "complete" -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;
find $SOURCE2 -not -name "completed" -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;

I also tried the following code

find $SOURCE1 -mindepth 2 -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;
find $SOURCE2 -mindepth 2 -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;

And without

find $SOURCE1 -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;
find $SOURCE2 -type d -empty -prune -exec rmdir --ignore-fail-on-non-empty -p \{\} \;

I could add a mkdir test and change SOURCE1 to "/mnt/download/transmission/complete/test" and this way it always deletes the directory that I just created but I would like to do it the proper way

Example: I created 6 directories:









After running copy.sh I end up with perfectly copyed directories and files (test1/test2/test3/test and test10/test11/test12/test) on destination and deleted directories and files on source INCLUDING source ($SOURCE1 and $SOURCE2) itself.

Is there a way to tell find to exclude the source directory itself? In other words: Everything UNDER the folowing directories shoud be deleted but not the directories themselves:


marked as duplicate by Michael Hampton, HalosGhost, sam, GAD3R, techraf Dec 3 '16 at 21:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from serverfault.com Dec 3 '16 at 18:59

This question came from our site for system and network administrators.


If you want to use find, you should add the flag -mindepth 1, so that the level below your source directories is the first being found.

However, you could also use a bunch of different tools, e.g. ls -d */ in order to print the directory names.

  • mindepth 2 is still deleting the source directory, mindepth 1 did give me an error and also deletes the source directory. Why?? Could I try to add * after / in my source directories? So that $SOURCE1="/mnt/download/transmission/complete/*" or is that not correct? – Maikel Dec 3 '16 at 7:19
  • Sorry, I ofcourse mean: SOURCE1="/mnt/download/transmission/complete/*" – Maikel Dec 3 '16 at 9:54

I found an more elegant way;

SOURCE1="/mnt/download/transmission/complete/"  # server1
SOURCE2="/mnt/download/sabnzbd/completed/"      # server1
DESTINATION="/mnt/dune/DuneHDD_1234/Transfer"   # server2

# move downloads to server2
sudo rsync --remove-source-files --progress --ignore-existing -vr $SOURCE1 $DESTINATION
sudo rsync --remove-source-files --progress --ignore-existing -vr $SOURCE2 $DESTINATION
# delete (only) empty directories left behind by rsync
find $SOURCE1 -mindepth 1 -type d -empty -delete # -mindepth:dont delete parent dir,-type d -empty -delete:delete only empty directories
find $SOURCE2 -mindepth 1 -type d -empty -delete

Source: Remove empty directory trees (removing as many directories as possible but no files) If not allowed please let me know