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

Due to a Hard disk problem I am trying to shift a partition from one hard disk to another. I am following 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.

0

6 Answers

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.

1

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.

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 -> Regex tutorial here ->

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.

4

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

So why not just

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like