[X-Unix] copy 101 question

David Ledger dledger at ivdcs.demon.co.uk
Sun Jun 6 10:45:37 PDT 2004


>From: Stroller <MacMonster at myrealbox.com>
>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.

The tar cf - | (cd otherdir; tar xvf -) method of copying file 
hierarchies is well established.  It does use three processes, but 
one is the shell for the ( ... ) and the other two are both tar. 
All are in memory so the text (the invariant part) of the program 
will be re-used.  The GNU-tar (?) variant we get with OSX allows the 
( cd ... ) to be skipped.

At least one version of cp does take a -R flag for recursive copying, 
but, because it's not universal, I never use it.

If the purpose is backup, the hierarchy really does need to be 
maintained.  Have a look at 'pax' (man pax).  Pax is tar, ustar, and 
several version of cpio all rolled into one.  It takes a list of 
files to copy/archive on stdin like cpio does, or on the command line 
like tar.  Pax is also supported on commercial Unix systems other 
than OSX.

David




-- 
David Ledger - Freelance Unix Sysadmin in the UK.
Chair of HPUX SysAdmin SIG of hpUG technical user group (www.hpug.org.uk)
dledger at ivdcs.co.uk (also dledger at ivdcs.demon.co.uk)
www.ivdcs.co.uk



More information about the X-Unix mailing list