[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-19
7
Medium Priority
?
304 Views
Last Modified: 2012-05-12
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,

0
Comment
Question by:Tolgar
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:saimazz
ID: 36993301
Try using GNU tar e.g.

gtar cvf - --exclude=/home/dir1 /home/me/someProject | (cd /home/you; tar -xvf - )
0
 

Author Comment

by:Tolgar
ID: 36993333
Can you please rewrite this command with my directory structure?

What does each argument do in this line?

Thanks,

0
 
LVL 3

Expert Comment

by:saimazz
ID: 36993453
maybe better is to use rsync :

rsync -av --exclude='/home/dir1' --exclude='/home/dir2' --exclude='/home/config.xml' source_destination
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Tolgar
ID: 36993573
@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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36993747
cd MyDir
find . -type d | xargs -I{} mkdir -p ../MyDirNew/{}
find . -type f -maxdepth 2 | xargs -I{} cp -rp {} ../Mydir_new/{}

0
 

Author Comment

by:Tolgar
ID: 36993878
@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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 36994099
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question