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 05-14-2012, 04:53 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Rep: Reputation: 34
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

Code:
cp !(/public_html/destination_directory) /public_html/ /public_html/destination_directory
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.

Thanks so much in advance!
 
Old 05-14-2012, 05:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
1 members found this post helpful.
Old 05-14-2012, 05:55 PM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by Tinkster View Post
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 05:55 PM. Reason: Thanking
 
Old 05-14-2012, 07:41 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by wh33t View Post
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.


Cheers,
Tink
 
Old 05-14-2012, 07:47 PM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by Tinkster View Post
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?

Something like:

Code:
system('shop -s extglob; cp !ignoresomestuff source_dir destination_dir';)

Last edited by wh33t; 05-14-2012 at 09:29 PM.
 
Old 05-14-2012, 08:01 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by wh33t View Post
Is there a way to run both in the same system() call?

Something like:

Code:
system('shop -s extglob; cp !ignoresomestuff source_dir destination_dir';)
Yah.

Code:
$ cat test.php
<?php
  $out=system('ls -1 *pl;echo "It is working!" ');
  print $out;
?>
Code:
php -f test.php 
iotest.pl
test.pl
It is working!
It is working!
Not that I understand why I get the echo's output twice, but I'm not a PHP guy ;}


Cheers,
Tink
 
Old 05-14-2012, 08:16 PM   #7
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by Tinkster View Post
Yah.

Code:
$ cat test.php
<?php
  $out=system('ls -1 *pl;echo "It is working!" ');
  print $out;
?>
Code:
php -f test.php 
iotest.pl
test.pl
It is working!
It is working!
Not that I understand why I get the echo's output twice, but I'm not a PHP guy ;}


Cheers,
Tink
You're awesome man.

Now, one last question. If I want to ignore multiple files do I use commas?

Example:

Code:
cp !(file1,file2,directory1,directory2) source destination
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.

Edit:

I have just tried running this:

Code:
shopt -s extglob; cp !test_file.txt /home/admin/public_html/dev/test/source_dir/* /home/admin/public_html/dev/test/destination_dir
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.

Last edited by wh33t; 05-14-2012 at 09:29 PM.
 
Old 05-14-2012, 09:49 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I think I know what you're doing wrong ;}

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
 
Old 05-14-2012, 10:01 PM   #9
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
So... if you do "cp" from inside of a dir with out specifying (*) it just assumes every file and directory in the cwd?
 
Old 05-14-2012, 10:08 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Everything that doesn't match what you have in !() ... that's the whole idea of that glob.
 
Old 05-14-2012, 10:19 PM   #11
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Code:
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:

Code:
find /home/admin/public_html/dev/test/source_dir/  ! -name "test_file.txt" | cpio -admp home/admin/public_html/dev/test/destination_dir
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:

Code:
/home/admin/public_html/test/source_dir/test_file_2.txt
Which is nearly what I want. It's ignoring the file like it should but now it's making directories.
 
Old 05-15-2012, 01:27 AM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
How about a local rsync using exclude option http://www.thegeekstuff.com/2011/01/...s-and-folders/
 
1 members found this post helpful.
Old 05-15-2012, 01:19 PM   #13
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by chrism01 View Post
How about a local rsync using exclude option http://www.thegeekstuff.com/2011/01/...s-and-folders/
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?

Last edited by wh33t; 05-15-2012 at 01:23 PM.
 
Old 05-16-2012, 08:44 PM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Is that using rsync? If so, checkout the various flags available.
I usually go with -a
Quote:
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
http://linux.die.net/man/1/rsync
Basically, those implied flags preserve just about everything ...
 
Old 05-16-2012, 09:13 PM   #15
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Ubuntu Server LTS 14.04.1
Posts: 472

Original Poster
Rep: Reputation: 34
Rsync is running from PHP on my VPS. I do a manual CHMOD from PHP to get around the issue and it works great. Thanks for your help!
 
  


Reply

Tags
cp, directories, directory, files, ignore


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
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 08:56 PM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 07:22 AM
copy specific files with directory stucture rincewind Linux - Software 1 06-22-2006 08:58 AM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 06:35 PM


All times are GMT -5. The time now is 03:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration