Copy a directory into another directory while ignoring specific directories or files
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.
Copy a directory into another directory while ignoring specific directories or files
Hey LQ,
I've been racking my brains for a few hours on this one and I'm pretty fed up. I turn to the pro's now.
This is for my clients webserver. I want to be able to mirror their entire live public_html/ directory into a different directory that is also located inside the public_html/ directory.
So something like:
Code:
/public_html/ to /public_html/destination_directory/
So the problem is that I want to ignore /public_html/destination_directory/ because I don't want to copy itself into itself if that makes sense.
I've been reading up on the "cp" command but it doesn't really seem like it can do this. I've tried doing things like
But that doesn't work either. Of the few examples I've looked at I can't understand them to make them work for my specific example. So I need some help.
I want to specifically ignore multiple directories and files and I will be running this command through the PHP function system(), so I'm not sure what terminal equivalent that would be. The VPS Webhost is CentOS if that helps at all.
You're almost there ...
first, make sure extglob is on for this activity: shopt -s extglob
Then:
cd public_html
cp !(destination_directory) destination_directory
You're almost there ...
first, make sure extglob is on for this activity: shopt -s extglob
Then:
cd public_html
cp !(destination_directory) destination_directory
Remember, I'm running this from system() inside of a PHP script. I'm not sure what "shopt -s extglob" does exactly but does it permanently alter the way the server runs or is it just for the terminal session or script duration? Do I run it in the same system() exec or do I run a system('shopt -s extglob') first?
Btw, thank you for chiming in.
Last edited by wh33t; 05-14-2012 at 04:55 PM.
Reason: Thanking
Remember, I'm running this from system() inside of a PHP script. I'm not sure what "shopt -s extglob" does exactly but does it permanently alter the way the server runs or is it just for the terminal session or script duration? Do I run it in the same system() exec or do I run a system('shopt -s extglob') first?
Btw, thank you for chiming in.
That should be just for the current shell you're invoking; it's not a "system setting".
You'd have to find a way of doing this from the same session, so maybe slap shop -s extglob and your cp into a script, and run THAT from your system() call.
That should be just for the current shell you're invoking; it's not a "system setting".
You'd have to find a way of doing this from the same session, so maybe slap shop -s extglob and your cp into a script, and run THAT from your system() call.
Cheers,
Tink
Is there a way to run both in the same system() call?
You get the output twice because system() by design echo's the output. If you just wanted only the output from the commands then you would as you have done by called it by exec() or passthru() which do not output anything.
And it still copies over !test_file.txt. I've tried with brackets around test_file.txt and it didn't copy anything at all. Yet doesn't seem to error on me either.
for the glob to work you need to be in the source dir. shopt -s extglob; cd /home/admin/public_html/dev/test/source_dir/; cp !(test_file.txt) /home/admin/public_html/dev/test/destination_dir
shopt -s extglob; cd /home/admin/public_html/dev/test/source_dir/; cp !(test_file.txt) /home/admin/public_html/dev/test/destination_dir
Unfortunately doesn't work. I think it's because the terminal type must be different when running it from PHP. I'm having some luck with another solution I've googled like this:
Which does work however in the $destination_dir it creates the total directory path from the source. So in the destination directory I get the directories made like:
Holy @#$%, that was very easy to do. Thank you very much.
I only have one issue... Every time I run my back up command it changes the file permission of my destination directory to 750 when it's supposed to be set at 755... Any idea why that might be?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.