0

I want to exclude two directories while copying.

Example:

$ ls /root/tmp
a b c d e f    

I want to exclude directories a and b:

$ cp -rp /root/tmp/ /root/tmp1/
denis
  • 20,457
  • 9
  • 62
  • 82

4 Answers4

4

rsync can be used to exclude multiple directories like this:

rsync -av --exclude=/root/tmp/a --exclude=/root/tmp/b /root/tmp/ /root/tmp1/

with cp command

cp -r /root/tmp/!(a | b) /root/tmp1/

Execute shopt -s extglob before cp command to enable ! in cp

Sathiya
  • 946
  • 2
  • 10
  • 26
1

Try the below rsync it works for me on ubuntu 14.04

rsync -av --exclude='/root/tmp/a' --exclude='/root/tmp/b' 
/path/to/include /path/to/include /path/to/destination
justaguy
  • 2,640
  • 3
  • 15
  • 32
1

You could exclude the directories as part of find results before the copy, but using rsync or cp with the '!' support enabled as suggested by Sathiya is a much simpler solution.

See find example below:

find /root/tmp/ -mindepth 1 -maxdepth 1 -type d ! -regex '\(.*a\|.*b\)' -exec cp -r {} /root/tmp1/ \;
Shakiba Moshiri
  • 16,284
  • 2
  • 23
  • 38
0

You can do this in bash (does not work in sh):

shopt -s extglob
cp -r !(somefile|somefile2|somefolder) destfolder/
t7e
  • 159
  • 1
  • 6