[X-Unix] copy 101 question

Stroller MacMonster at myrealbox.com
Sun Jun 6 03:34:16 PDT 2004


On Jun 5, 2004, at 11:14 pm, James Bucanek wrote:

> Scott Warren wrote on Saturday, June 5, 2004:
>
>> I am wanting to back up just .doc files onto my firewire drive.  What
>> is the Mac/Unix version of
>>   xcopy *.doc E:\backup -r
>>
>> I have tried  cp -R *.doc \volumes\myfirewire    and this only copies
>> whats in the current directory and does not recurs the tree.
> ...
> In UNIX, the Swiss Army knife of directory recursion is the Find tool. 
>  Heres one way to use the find tool in combination with the tar (Tape 
> ARchive) tool to transfer a sparse set of hierarchically organized 
> files from one directory tree to another:
>
>     cd ~
>     find Documents -type f -name '*.doc' -print0 | tar -cf - 
> --files-from - --null | tar -xvf - -C /Volumes/firewire
>
> In English, I used the find tool to search my entire Documents folder 
> for regular files (this excludes any directories or symbolic links) 
> who's name matches the patter '*.doc'.  I piped this list to the tar 
> tool and told it to create an archive (c) outputting the results to 
> stdout (-f -).  Instead of listing the files to archive on the 
> command, I told it to read the list of files from stdin (which will be 
> the output of the find tool).  Finally, I piped the resulting archive 
> directly into a second invocation of tar that extracts (-x) the 
> archive read from stdin (-f -) into a specific directory (-C 
> /Volumes/firewire).  The -v option just outputs the names of the files 
> as they are processed so we can watch the fun.

I believe that you have tarred the files & then untarred them again in 
order to preserve the directory-tree structure of the files, so that if 
the user has ~/foo/some.doc and ~/foo/bar/some.doc then both files will 
be copied to the destination drive and appropriate subdirectories made. 
I don't know if this is the default behaviour of xcopy, but believe 
it's worth commenting on, because otherwise I'd regard this line as 
more intuitive:
     find Documents -name '*.doc' -exec cp \{\} /Volumes/firewire ;

I can't see any shorter way than yours to preserve the directories when 
copying found files, however. It seems to me a little inelegant to have 
to use `tar` twice in this way - I would have expected a single command 
to be available to do the the same thing, or a suitable flag to `cp`, 
but I can't find either.

Stroller.



More information about the X-Unix mailing list