LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2005, 07:59 AM   #1
M4l3k
LQ Newbie
 
Registered: Oct 2005
Location: Denmark
Posts: 2

Rep: Reputation: 0
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
 
Old 10-30-2005, 08:06 AM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
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.
 
Old 10-30-2005, 08:11 AM   #3
M4l3k
LQ Newbie
 
Registered: Oct 2005
Location: Denmark
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 10-30-2005, 08:18 AM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
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
 
Old 02-28-2011, 01:48 PM   #5
kasapo
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
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.

Last edited by kasapo; 02-28-2011 at 01:53 PM. Reason: idea for shellscript
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile and subdirectories pycoucou Programming 4 08-18-2005 05:43 AM
How can I get make to ignore a particular error? SheldonPlankton Linux - Software 1 08-05-2004 02:44 PM
how to make Linux ignore hardware? netmanw00t Linux - Hardware 3 07-11-2004 04:33 AM
Make ls ignore symlinks Creep Linux - Software 3 01-23-2004 06:21 PM
email and subdirectories hiertjou123 Linux - Newbie 5 07-16-2003 02:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration