Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cp dir recursivly excluding 2 subdirs

Tags:

linux

bash

unix

cp

I have 1 directory with 9 subdirectories and 10 files. Subdirectory have next level subdirectories and files.

/home/directory/
/home/directory/subdirectory1
/home/directory/subdirectory2
...
/home/directory/subdirectory9
/home/directory/file1
...
/home/directory/file10

I want to copy all subdirectories and files recursivly excluding:

/home/directory/subdirectory5
/home/directory/subdirectory7

What is the best way for it?

like image 457
dr0zd Avatar asked Feb 27 '12 12:02

dr0zd


3 Answers

rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path
like image 195
kjohri Avatar answered Oct 24 '22 09:10

kjohri


I don't know a good way of doing it with cp, but it's fairly easy using rsync and the --exclude switch.

like image 22
aioobe Avatar answered Oct 24 '22 07:10

aioobe


Maybe the find command will help you:

$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;
like image 43
kev Avatar answered Oct 24 '22 09:10

kev