0

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

Copy.sh

SOURCE1="/mnt/download/transmission/complete/"
SOURCE2="/mnt/download/sabnzbd/completed/"

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:

test10/

test10/test11/

test10/test11/test12/

test10/test11/test12/test

test1/

test1/test2/

test1/test2/test3/

test1/test2/test3/test

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:

SOURCE1="/mnt/download/transmission/complete/"
SOURCE2="/mnt/download/sabnzbd/completed/"
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

0

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? –  Dec 03 '16 at 07:19
  • Sorry, I ofcourse mean: SOURCE1="/mnt/download/transmission/complete/*" –  Dec 03 '16 at 09:54
0

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