Unix Shell - recursive cp that will only pick up certain extensions

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > recursive cp that will only pick up certain extensions





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author recursive cp that will only pick up certain extensions
zip184@gmail.com

2007-12-20, 7:21 pm

I'm writing a shell script that needs to copy files with a certain
extension out of a large directory structure. Is there a single
command that can do this? I tried many combinations of find, cp -r,
exec, piping. Nothing I can think of works, but I am an armature at
best. I'm using cygwin. Can anyone show me a single command that can
do this? I want to avoid writing a recursive script or something of
that nature. -Thanks

Ex. cp -r ( copy only *.pbl ) newdir
Lew Pitcher

2007-12-20, 7:21 pm

On Dec 20, 3:19 pm, zip...@gmail.com wrote:
> I'm writing a shell script that needs to copy files with a certain
> extension out of a large directory structure. Is there a single
> command that can do this? I tried many combinations of find, cp -r,
> exec, piping. Nothing I can think of works, but I am an armature at
> best. I'm using cygwin. Can anyone show me a single command that can
> do this? I want to avoid writing a recursive script or something of
> that nature. -Thanks
>
> Ex. cp -r ( copy only *.pbl ) newdir


find . -name '*.pbl' -exec cp {} newdir \;

Bill Marcum

2007-12-20, 7:21 pm

On 2007-12-20, zip184@gmail.com <zip184@gmail.com> wrote:
>
>
> I'm writing a shell script that needs to copy files with a certain
> extension out of a large directory structure. Is there a single
> command that can do this? I tried many combinations of find, cp -r,
> exec, piping. Nothing I can think of works, but I am an armature at
> best. I'm using cygwin. Can anyone show me a single command that can
> do this? I want to avoid writing a recursive script or something of
> that nature. -Thanks
>
> Ex. cp -r ( copy only *.pbl ) newdir


tar cf - $( find . -name '*.pbl' ) | {cd newdir; tar xf -;}
Stephane Chazelas

2007-12-21, 7:33 am

On Thu, 20 Dec 2007 12:19:57 -0800 (PST), zip184@gmail.com wrote:
> I'm writing a shell script that needs to copy files with a certain
> extension out of a large directory structure. Is there a single
> command that can do this? I tried many combinations of find, cp -r,
> exec, piping. Nothing I can think of works, but I am an armature at
> best. I'm using cygwin. Can anyone show me a single command that can
> do this? I want to avoid writing a recursive script or something of
> that nature. -Thanks
>
> Ex. cp -r ( copy only *.pbl ) newdir


cd olddir && find . -name '*.pbl' -print | pax -rwdpe ../newdir

pax will create directories in ../newdir as needed but not
necessarily with the same permissions and owners as they were in
olddir.

Note that the above assumes that no file name contains any
newline character.

--
Stephane
Icarus Sparry

2007-12-21, 7:33 am

On Fri, 21 Dec 2007 07:56:47 +0000, Stephane Chazelas wrote:

> On Thu, 20 Dec 2007 12:19:57 -0800 (PST), zip184@gmail.com wrote:
>
> cd olddir && find . -name '*.pbl' -print | pax -rwdpe ../newdir
>
> pax will create directories in ../newdir as needed but not necessarily
> with the same permissions and owners as they were in olddir.
>
> Note that the above assumes that no file name contains any newline
> character.


This is parsed as
cd olddir && { find ... | pax ... }
which is probably not what you want, in particular if "newdir" is
something like ~- or an absolute path.

Better to use a subshell

( cd olddir && find ... ) | pax -rwdpe newdir
Stephane Chazelas

2007-12-21, 7:33 am

On 21 Dec 2007 09:00:17 GMT, Icarus Sparry wrote:
[...]
>
> This is parsed as
> cd olddir && { find ... | pax ... }
> which is probably not what you want, in particular if "newdir" is
> something like ~- or an absolute path.
>
> Better to use a subshell
>
> ( cd olddir && find ... ) | pax -rwdpe newdir


No, the paths output by find must be accessible by pax, so pax'
current directory must be the same as find's which wouldn't be
the case in the syntax you suggest.

Note my use of "../newdir" above. That assumes "oldir" and
"newdir" are subdirectories of a same directory.

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com