I've been using rsync to synchronise folders and it works well. The problem is that more recently I've started syncing folders with larger files in them, and it takes much longer than I'd like it to (due to its hashing comparison). I've noticed that the cp commands can do one part of rsyncs job much quicker, by invoking the -u option. This means newer files in the source can be added to the destination easily using this method.

But what I need to figure out is the second part of the rsync job which I find useful. This is a command which can recursively compare the list of files in all folders, and delete those which no longer feature in the source, but which are still in the destination (but without performing a hash on all the files, a simple comparison using the ls command, for example, is good enough for what I want).

Is this possible?

  • Do you have a sample of your rsync line available? – Zachary Brady Jun 29 '16 at 2:35
  • i just use this line which updates newer files and deletes missing ones: rsync -avzh sourcefolder destinationfolder --delete – tambungant Jun 29 '16 at 2:40

This will (pretend to) delete any differences between the folders:

diff -awr folderA folderB | sed 's/Only in //;s/: /\//' | while read f; do echo "removing ${f}"; done;

If you want to remove differences in A but not B, you can add in a grep like so:

diff -awr folderA folderB | sed 's/Only in //;s/: /\//' | grep "^folderA/" | while read f; do echo "removing ${f}"; done;

note that you have to type folderA into the command twice for this one

To run it for real, just replace echo "removing ${f}"; with rm -f "${f}";

  • this is fantastic and does exactly what i want, except for one more thing, how can i also remove folders in addition to files please? – tambungant Jun 29 '16 at 11:54
  • i've realised i can just invoke the -rf option on the rm command, thanks for your help! – tambungant Jun 29 '16 at 12:18
  • yep rm -rf "${f}"; will do it :) Glad I could help! – ajlowndes Jun 29 '16 at 12:20
  • hey take note of the edit I just made - I added a ^ into the grep string to make sure to match the start of the line, instead of anywhere in it. – ajlowndes Jun 29 '16 at 12:58

You can refer this link this link. rsync is having the feature of deleting the folder which doesn't exits on the live ENV.

rsync -av --delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.