How can I use cp to copy a directory but ignore a certain sub directory in Linux

8

3

Due to a Hard disk problem I am trying to shift a partition from one hard disk to another. I am following http://www.ibm.com/developerworks/library/l-partplan.html article to do that. In the copying part I would like to ignore one particular sub directory. How can I accomplish that keeping in mind when copying I have to preserve my owner group and time stamp. There is around 700 GB of data that needs to be copied if I do not ignore a particular subdirectory.

P Roy

Posted 2010-06-13T07:53:00.513

Reputation: 83

Answers

10

rsync -ax --exclude [relative path to directory to exclude] /path/from /path/to

You might want (or not) to use --del as well. Check the manual page.

Marco Mariani

Posted 2010-06-13T07:53:00.513

Reputation: 236

Yes that did the trick. This works.. – P Roy – 2010-06-13T09:32:50.913

2

Normally I use cpio as follows,

cd source_dir; find . -depth | cpio -pdmv dest_dir

And since this is a pipeline you can put a "subtraction filter" in the middle.

cd sourcedir; find . -depth | grep -v exclude_dir | cpio -pdmv dest_dir

or you could split this is into several steps,

cd source_dir; find . -depth > files.lst
gedit files.lst  # (take out the offending directory and files and save back to files.lst)
cpio -pdmv dest_dir < files.lst

Of course I'd test this on something smaller first but you get the idea.

hotei

Posted 2010-06-13T07:53:00.513

Reputation: 3 645

0

You could write a simple bash script with a loop to ignore the certain path you don't want copied and copy the rest. Another solution could be to us regular expressions. You can read up on bash scripting here -> http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html Regex tutorial here -> http://www.regular-expressions.info/

tapan

Posted 2010-06-13T07:53:00.513

Reputation: 918

0

Can you temporarily move (mv) the large subdirectory to some other location, do the copy, and then restore the subdirectory? I can't see a direct option in cp to do this.

Ash

Posted 2010-06-13T07:53:00.513

Reputation: 2 574

I too checked man cp there is no way to ignore using cp. I have 550 GB of data in that sub directory and mv that might take hours. – P Roy – 2010-06-13T08:14:27.190

I assumed mv would be a relatively low-cost operation - updating references to the data rather than physically moving it around on the disk. At least, I've always found moves in Windows to be way quicker than copies - the Linux file system could work entirely differently. – Ash – 2010-06-13T08:50:33.483

mv to the same hard disk is fast. But when one does a mv across hardisks I think it takes the same time as a cp. – P Roy – 2010-06-13T10:56:47.920

I meant to move it somewhere on the same hard disk to get it out of the way, then copy the remaining to the remote disk, then move the big file back. Unless you're copying the entire disk from /. – Ash – 2010-06-14T00:21:05.607

0

Rather ugly solution but... why not just cp everything in the directory non recursively, and then copy the individual directories over recursively?

Journeyman Geek

Posted 2010-06-13T07:53:00.513

Reputation: 119 122

-1

So why not just

cp -Rv [SRC] [DEST] | grep -v [EXCLUDE]

halfcab123

Posted 2010-06-13T07:53:00.513

Reputation: 7