Unix Technical Forum

find and cp in one command...

This is a discussion on find and cp in one command... within the HP-UX Operating System forums, part of the Unix Operating Systems category; --> This is my find: find ./ -name "*" -mtime -1 I need to pipe that to cp to move ...


Go Back   Unix Technical Forum > Unix Operating Systems > HP-UX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-17-2008, 05:32 AM
Robert Hicks
 
Posts: n/a
Default find and cp in one command...

This is my find:

find ./ -name "*" -mtime -1

I need to pipe that to cp to move it to /tmp/20060810LK78

Help?

Robert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-17-2008, 05:32 AM
Paul Pluzhnikov
 
Posts: n/a
Default Re: find and cp in one command...

"Robert Hicks" <sigzero@gmail.com> writes:

> find ./ -name "*" -mtime -1
>
> I need to pipe that to cp to move it to /tmp/20060810LK78


mv `find . -name "*" -mtime -1` /tmp/20060810LK78

If this gives you "command line too long", then

find . -name "*" -mtime -1 | xargs -i mv {} /tmp/20060810LK78

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-17-2008, 05:32 AM
Robert Hicks
 
Posts: n/a
Default Re: find and cp in one command...


Paul Pluzhnikov wrote:
> "Robert Hicks" <sigzero@gmail.com> writes:
>
> > find ./ -name "*" -mtime -1
> >
> > I need to pipe that to cp to move it to /tmp/20060810LK78

>
> mv `find . -name "*" -mtime -1` /tmp/20060810LK78
>
> If this gives you "command line too long", then
>
> find . -name "*" -mtime -1 | xargs -i mv {} /tmp/20060810LK78
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


Thanks! I can pipe some things but others throw me.

Robert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-17-2008, 05:33 AM
OldSchool
 
Posts: n/a
Default Re: find and cp in one command...


Robert Hicks wrote:

> > > find ./ -name "*" -mtime -1
> > >
> > > I need to pipe that to cp to move it to /tmp/20060810LK78

> >



How about

find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 \;

???

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-17-2008, 05:33 AM
Paul Pluzhnikov
 
Posts: n/a
Default Re: find and cp in one command...

"OldSchool" <scott.myron@fds.com> writes:

> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 \;


This works too, but is much less efficient than the xargs solution,
because it fork()s/exec()s for each matching file.

This may matter if you want to move a couple thousands of files.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-17-2008, 05:33 AM
Alexander Skwar
 
Posts: n/a
Default Re: find and cp in one command...

· Paul Pluzhnikov <ppluzhnikov-nsp@charter.net>:

> "OldSchool" <scott.myron@fds.com> writes:
>
>> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 \;

>
> This works too, but is much less efficient than the xargs solution,
> because it fork()s/exec()s for each matching file.


Yep. Because of that, it's better to do

find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 +

xargs isn't needed.

Alexander Skwar
--
Zeichnen ist Sprache für die Augen, Sprache ist Malerei für das Ohr.
-- Joseph Joubert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-17-2008, 05:36 AM
Ulrich Windl
 
Posts: n/a
Default Re: find and cp in one command...

Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:

> "Robert Hicks" <sigzero@gmail.com> writes:
>
> > find ./ -name "*" -mtime -1
> >
> > I need to pipe that to cp to move it to /tmp/20060810LK78

>
> mv `find . -name "*" -mtime -1` /tmp/20060810LK78
>
> If this gives you "command line too long", then
>
> find . -name "*" -mtime -1 | xargs -i mv {} /tmp/20060810LK78


Q: Trying that on Linux, option "-i" caused xargs to create a command for
every input line. Same for HP-UX?

> xargs -i echo '{}' 4

1 <--- input
1 4 --> output
2 <--- input
2 4 --> output
3 <--- input
3 4 --> output
^D

Regards,
Ulrich
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-17-2008, 05:36 AM
Ulrich Windl
 
Posts: n/a
Default Re: find and cp in one command...

Alexander Skwar <alexander@skwar.name> writes:

> · Paul Pluzhnikov <ppluzhnikov-nsp@charter.net>:
>
> > "OldSchool" <scott.myron@fds.com> writes:
> >
> >> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 \;

> >
> > This works too, but is much less efficient than the xargs solution,
> > because it fork()s/exec()s for each matching file.

>
> Yep. Because of that, it's better to do
>
> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 +
>
> xargs isn't needed.


Unless "mv" is a find built-in, it's more inefficient than using xargs
(despite of the issue mentioned in my previous post on this thread).

Ulrich
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-17-2008, 05:36 AM
Alexander Skwar
 
Posts: n/a
Default Re: find and cp in one command...

Ulrich Windl <Ulrich.Windl@RZ.Uni-Regensburg.DE>:

> Alexander Skwar <alexander@skwar.name> writes:
>
>> · Paul Pluzhnikov <ppluzhnikov-nsp@charter.net>:
>>
>> > "OldSchool" <scott.myron@fds.com> writes:
>> >
>> >> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 \;
>> >
>> > This works too, but is much less efficient than the xargs solution,
>> > because it fork()s/exec()s for each matching file.

>>
>> Yep. Because of that, it's better to do
>>
>> find ./ -name "*" -mtime -1 -exec mv {} /tmp/20060810LK78 +
>>
>> xargs isn't needed.

>
> Unless "mv" is a find built-in, it's more inefficient than using xargs


Why?


-exec cmd True if the executed cmd returns a zero value
as exit status. The end of cmd must be
punctuated by a semicolon ( or a plus sign
(+) (semicolon and plus are special to the
shell and must be escaped). When + is used,
cmd aggregates a set of path names and
executes on the set. Any command arguments
between the first occurrence of {} and + are
ignored. The reason for preferring + to a ;
is vastly improved performance. Any command
argument {} is replaced by the current path
name. cmd may contain supplementary code set
characters.

However, my command won't work

But please explain why "find . -exec mv {} /foo +" is inefficient.

Alexander Skwar
--
94% of the women in America are beautiful and the rest hang out around here.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-17-2008, 05:37 AM
Ulrich Windl
 
Posts: n/a
Default Re: find and cp in one command...

Alexander Skwar <alexander@skwar.name> writes:

[...]
> But please explain why "find . -exec mv {} /foo +" is inefficient.


Simple: Consider 15000 files. Then your command will invoke "mv" 15000 times,
while an "xargs" solution will invoke "mv" more infrequently. Now if you trace
one start of "mv" you can guess how many CPU and I/O cycles you are wasting.

Regards,
Ulrich

>
> Alexander Skwar
> --
> 94% of the women in America are beautiful and the rest hang out around here.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:12 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com