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 |
Quote:
|
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 |
You could try:
Quote:
Code:
cp --parents dir2....dir13 dir15 dir1 Forgot to say in post#2 - welcome to LQ :D |
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. |