I have a /var/www directory which is a local copy of my web site. I want to copy only the .html files together with any sub-directories that contain .html files (retaining the directory structure), together with their files, into another directory.
I've tried various incantations involving 'find' and can only succeed in copying files from the base directory.
If someone can suggest a way to do this it would be very much appreciated. I really can't afford to lose much more hair.
Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain
On Tue, Jan 13, 2004 at 01:59:10PM +0000, Barry Samuels wrote:
I have a /var/www directory which is a local copy of my web site. I want to copy only the .html files together with any sub-directories that contain .html files (retaining the directory structure), together with their files, into another directory.
I've tried various incantations involving 'find' and can only succeed in copying files from the base directory.
Right - using find and a small bash script I can do it ;) So, here goes, this has only been half tested, so I claim no responsibility if it manages to fry your cat, toast your grandmother, or, well, generally break things... Here's what I did...
(1) Create a shell script to copy a file with directory structure as follows: --- Begin ~/bin/copyfiles.sh Script Here --- #!/bin/bash
FILENAME=$1 DIRNAME=`dirname "$FILENAME"` DESTINATION="$2"
mkdir -p $DESTINATION/$DIRNAME cp $FILENAME $DESTINATION/$FILENAME --- End Script ---
(2) change in to the structure I want to copy (in my case ~/public_html/test/)
(3) run the find command, somewhat as follows... find . -name *.html -exec ~/bin/copyfiles.sh {} /some/base/destination ;
And that should work...
there's bound to be a more elegant solution, but that *appears* to work ;)
Cheers,
Brett
Brett Parker brettp@users.sourceforge.net wrote:
Right - using find and a small bash script I can do it ;) So, here goes, this has only been half tested, so I claim no responsibility if it manages to fry your cat, toast your grandmother, or, well, generally break things... Here's what I did...
(1) Create a shell script to copy a file with directory structure as follows: --- Begin ~/bin/copyfiles.sh Script Here --- #!/bin/bash
FILENAME=$1 DIRNAME=`dirname "$FILENAME"` DESTINATION="$2" mkdir -p $DESTINATION/$DIRNAME cp $FILENAME $DESTINATION/$FILENAME --- End Script ---
(2) change in to the structure I want to copy (in my case ~/public_html/test/)
(3) run the find command, somewhat as follows... find . -name *.html -exec ~/bin/copyfiles.sh {} /some/base/destination ;
And that should work...
And it does - thanks a lot Brett.
The only side effect was that it did fry my cat - his fur is still smouldering slightly as I write this so next time I shall wait until he's out of range :-)
Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain
On 2004-01-13 13:59:10 +0000 Barry Samuels bsamuels@beenthere-donethat.org.uk wrote:
I have a /var/www directory which is a local copy of my web site. I want to copy only the .html files together with any sub-directories that contain .html files (retaining the directory structure), together with their files, into another directory.
For rc: cd /var/www && tar cvf - --files-from <{find . -name '*.html'} | tar xfC - /new/directory
I think bash is the same, but uses () instead of {}
What's going on there? Well, the cd should be obvious. If that succeeds (&&), run tar to Create Verbosely a File (cvf) and that file goes to standard output (-). Tar should read filenames from a file (--files-from) which is actually the output of that find command (that's what the <{...} does, turning command output into a file (really a type of pipe)). Then we send that (|) to another tar command that eXtracts a File (xf) from standard input (-), Changing directory (C) to /new/directory before writing files.
It's like a foreign language sometimes...
MJ Ray mjr@dsl.pipex.com wrote:
For rc: cd /var/www && tar cvf - --files-from <{find . -name '*.html'} | tar xfC - /new/directory
I think bash is the same, but uses () instead of {}
What's going on there? Well, the cd should be obvious. If that succeeds (&&), run tar to Create Verbosely a File (cvf) and that file goes to standard output (-). Tar should read filenames from a file (--files-from) which is actually the output of that find command (that's what the <{...} does, turning command output into a file (really a type of pipe)). Then we send that (|) to another tar command that eXtracts a File (xf) from standard input (-), Changing directory (C) to /new/directory before writing files.
It's like a foreign language sometimes...
What do you mean - somtimes?
I haven't tried it yet but I'll give it a go.
I could never, in a month of Sundays, dream up something like that :-)
Thanks Mark.
Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain