Copy by extension

Tim timfrost at xtra.co.nz
Tue Oct 21 09:42:46 UTC 2008


On Tue, 2008-10-21 at 09:57 +0200, Neil wrote:
> hi
> 
> just curious: would cp -R *.foo /path/to/destination/ do the trick? Or
> would it recreate the directory structure in the destination
> directory?

No.  cp -R will copy files *.foo from the current directory, and will
recursively copy ALL files in directories matching *.foo in the current
directory.  It won't search the directory structure to match *.foo. 
The find command, as used in the examples Nils provided, does the job
of searching the directory hierarchy for matching files.

If you wanted to copy the directory structure, you would use find and cpio in tandem:
  cd /base/source/dir
  find . -name '*.foo' -print | cpio -pvdmu /base/dest/dir

The cpio options mean:
 p = pass-through  (copy between directories, and don't create an archive)
 v = verbose 
 d = create directories as needed
 m = preserve modification times of copied files
 u = unconditional - it will over-write existing files in the destination 

You could also add the l option, to link files rather than copy them, if
the source and destination are on the same filesystem. This will save
both time and disk space if there are a lot of files and they need a lot
of space.

One note: the examples from Nils will work for gnu find (which is used
for all Linux distributions), because that will use the current
directory as a default search base, but they won't work on Solaris or
BSD-based systems, or other Unix systems that don't have gnu find.  My
example explicitly sets the search base to the current directory, from
lots of years exposure to systems that don't have gnu find :(



Tim






More information about the ubuntu-users mailing list