LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make cp ignore subdirectories? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-cp-ignore-subdirectories-378295/)

M4l3k 10-30-2005 06:59 AM

How to make cp ignore subdirectories?
 
Hi all,

Let's say I have Dir1 containing Dir11, Dir12, Dir13... to Dir19 as well as some files.
How do I copy Dir1 into let's say Dir2 omitting Dir12 for example?

I need to perform this action for dozens of folders so that doing manually would be too time consuming.

Thanks
M4l3k

XavierP 10-30-2005 07:06 AM

Quote:

From 'man cp'
GNU DETAILS
Generally, files are written just as they are read. For exceptions, see the
--sparse option below.

By default, `cp' does not copy directories (see -r below).
The man pages are your friend.

M4l3k 10-30-2005 07:11 AM

How to ignore subdirectories?
 
Hi,

Thanks for the quick answer but I meant that I want to copy all the content of Dir1 except from Dir14 (or whatever number I chose in the example above).

Using the -r flag with cp will cause the copy of all the subdirectories => That's not what I want!
Not using the -r flag will ignore the subdirectories => That's not what I want!

I want to be able to specify somewhere the subdirectory I want to ignore. Using one command line would be great!

Thanks

M4l3k

XavierP 10-30-2005 07:18 AM

You could try:
Quote:

--parents (in file-utils 4.0 also -P)
Form the name of each destination file by appending to the target directory a
slash and the specified name of the source file. The last argument given to
cp must be the name of an existing directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file `a/b/c' to `existing_dir/a/b/c', creating any missing inter-
mediate directories.
So you could try
Code:

cp --parents dir2....dir13 dir15 dir1
(i'd try it with just a couple of directories to start off with, if it doesn't work you won't have spent a lot of time typing).

Forgot to say in post#2 - welcome to LQ :D

kasapo 02-28-2011 12:48 PM

Just go into the parent of dir1..dir12, and do a directory listing, then pipe the output to a series of grep -v "dir-i-want-to-ignore" for the offending directories.

Then also grep -v for "total ##" with the last grep -v in there, then that output gets piped to the sed command which if you use my little regex will trim off everything before the file/foldername. You could modify this slightly to ignore files as well:

cp -rp `ls -l | grep -v "errors" | grep -v "sessions" | grep -v "databases" | grep -v "private" | grep -v "total" | sed s/.*:...//` ~/desination-for-copy/

So just replace "errors", "sessions" "databases", "private" etc with whatever. The grep -v "total" is required!

I did this as such to ignore logs/errors/session data and deployment settings when copying my web applicaiton, and I use it so much I made an alias for it. You can even add env variables after the cp and ls which holds the paths for the source/destination (just have to make sure you set it before you copy each time). Oh, and you could always replace one of the vars with a dot and just be sure to be in either the source or destination folder when you do the backup.

alias bu='cp -rp $BU_SRC/`ls -l $BU_SRC | grep -v "errors" | grep -v "sessions" | grep -v "databases" | grep -v "private" | grep -v "total" | sed s/.*:...//` $BU_DEST'


I realize you posted this a long time ago, but maybe it'll help someone?


EDIT: One more note -- if you don't know the names of the files you want to ignore ahead of time, this command could easily be used in a shell script that takes args for src dest ignore1..ignoreN

like:

./cpignore . ~/backup sessions private databases

It wouldn't be that much effort even if you're not a shell scripter, and it'd be a good opportunity to learn how to do something simple like this.


All times are GMT -5. The time now is 04:54 AM.