D
D
Dr. Robert Ford2019-08-25 21:35:37
linux
Dr. Robert Ford, 2019-08-25 21:35:37

How to remove the difference between two directories?

There are 2 directories: dir1 and dir2. How can I recursively remove all subdirectories and files from dir2 that are not in dir1 ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2019-08-25
@drford

Maybe rsync will help?

rsync \
  -av \
  --delete \
  --existing \
  dir1 dir2

V
Vitaly Karasik, 2019-08-25
@vitaly_il1

diff -r dir1/ dir2/ |grep Only | sed -e 's/^.*Only in //' -e '[email protected]: @/@' | xargs rm
Upd: but Sergey Sokolov 's answer is certainly prettier

V
vreitech, 2019-08-25
@fzfx

dir1=/path/to/dir1 dir2=/path/to/dir2; cd $dir2; for i in $(find . -mindepth 1 -type d -or -type f | tac); do if ; then if ; then rm -f $i; elif ; then rmdir $i; fi; fi; done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question