How to copy a directory excluding some of its contents in UNIX?

Hi,
In UNIX, how can I copy a directory excluding some of its contents?

Example:

MyDir has some files and directories underneath.

I would like to copy MyDir and its contents except the files under the dir1 and dir2 directories.

But I want the dir1 and dir2 directories as empty directories.

So my directory structure like this:

MyDir has these files and directories:

dir_A
dir_B
dir_C
dir_D

and it continues...

each of these directories has these files and directories underneath:
dir1
dir2
config.xml

As an overall example for it is like this:

MyDir
       dir_A
               dir1
               dir2
               config.xml
       dir_B
               dir1
               dir2
               config.xml
       dir_C
               dir1
               dir2
               config.xml
       dir_D
               dir1
               dir2
               config.xml

Open in new window



Now I want to copy MyDir excluding the files under dir1 and dir2 for each of them.

I would prefer to do it in one line in stead of writing a script.

How can I do it?

Thanks,

TolgarAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
What is your OS?
Seems that you're running a rather recent version of find.

Try this:

find . -maxdepth 2 -type f | xargs -I{} cp -rp {} ../Mydir_new/{}
0
 
saimazzCommented:
Try using GNU tar e.g.

gtar cvf - --exclude=/home/dir1 /home/me/someProject | (cd /home/you; tar -xvf - )
0
 
TolgarAuthor Commented:
Can you please rewrite this command with my directory structure?

What does each argument do in this line?

Thanks,

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
saimazzCommented:
maybe better is to use rsync :

rsync -av --exclude='/home/dir1' --exclude='/home/dir2' --exclude='/home/config.xml' source_destination
0
 
TolgarAuthor Commented:
@saimazz: No. It is does not include dir1 and dir2. It basically copies only files under dir_A. By the way, I don't wanna exclude config.xml. It is directly under dir_A, dir_B, dir_C and dir_D.

Thanks,
0
 
woolmilkporcCommented:
cd MyDir
find . -type d | xargs -I{} mkdir -p ../MyDirNew/{}
find . -type f -maxdepth 2 | xargs -I{} cp -rp {} ../Mydir_new/{}

0
 
TolgarAuthor Commented:
@woolmilkporc:it says this when I run your command:


find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

Open in new window



What should I do?

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.