Index

Subject : Re: LUG: How to copy full directory structure and only select

From : Brian Cottingham <spiffytech@gmail.[redacted]>

Date : Thu, 05 Aug 2010 07:11:01 -0400

Parent


===
mkdir stuff
cd data
cp --parents `find . -name 'file.dat' | xargs` ../stuff/
===

The --parents flag tells cp to preserve the directory structure when
copying. Since cp is preserving the directory structure (relative to the
dir we're searching from) we have to cd into data/ to prevent the file
structure from being stuff/data/1.

If you're using zsh or Bash 4.x, file globbing makes the copy command
simpler:

===
cp --parents **/file.dat ../stuff/
===

--Brian


On Thu, Aug 05, 2010 at 04:37:00AM -0400, Daniel Underwood wrote:
> Suppose I have the following:
>
> data
> |-- 1
> | |-- 0index.html
> | |-- 0index2.html
> | |-- 1.txt
> | |-- 2.txt
> | |-- 3.txt
> | |-- 4.txt
> | |-- 5.txt
> | |-- 6.txt
> | `-- file.dat
> `-- 2
> |-- 1.txt
> |-- 2.txt
> |-- 3.txt
> |-- 4.txt
> `-- file.dat
>
> And I want to copy this entire directory (data/) to another location,
> except for the fact that I only want to copy: (1) all directories and
> (2) each "file.dat" file. So, the result copied to another location
> should be the following:
>
> data
> |-- 1
> | `-- file.dat
> `-- 2
> `-- file.dat
>
> Without writing a script to walk the entire directory and check each
> item before deciding whether to copy it to the destination directory,
> how can I easily do this? Could I somehow recreate only the directory
> structure and then find the file.dat's and pipe them to an xargs copy
> command?
>
> TIA guys...
> --
> Daniel Underwood
> North Carolina State University
> PhD Student - Industrial Engineering
> email: daniel.underwood@ncsu.[redacted]
> phone: XXX.302.3291
> fax: XXX.515.5281
> web: http://www4.ncsu.edu/~djunderw/
>


Replies :