Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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!
--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).
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:
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.
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 12:53 PM.
Reason: idea for shellscript
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.