find and cp in one command... - HP UX

This is a discussion on find and cp in one command... - HP UX ; This is my find: find ./ -name "*" -mtime -1 I need to pipe that to cp to move it to /tmp/20060810LK78 Help? Robert...

+ Reply to Thread
Results 1 to 14 of 14

Thread: find and cp in one command...

  1. 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


  2. Re: find and cp in one command...

    "Robert Hicks" 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.

  3. Re: find and cp in one command...


    Paul Pluzhnikov wrote:
    > "Robert Hicks" 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


  4. 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 \;

    ???


  5. Re: find and cp in one command...

    "OldSchool" 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.

  6. Re: find and cp in one command...

    · Paul Pluzhnikov :

    > "OldSchool" 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


  7. Re: find and cp in one command...

    Paul Pluzhnikov writes:

    > "Robert Hicks" 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

  8. Re: find and cp in one command...

    Alexander Skwar writes:

    > · Paul Pluzhnikov :
    >
    > > "OldSchool" 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

  9. Re: find and cp in one command...

    Ulrich Windl :

    > Alexander Skwar writes:
    >
    >> · Paul Pluzhnikov :
    >>
    >> > "OldSchool" 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.


  10. Re: find and cp in one command...

    Alexander Skwar 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.


  11. Re: find and cp in one command...

    Ulrich Windl :

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

    >
    > Simple: Consider 15000 files. Then your command will invoke "mv" 15000 times,


    Nope, it won't. Please note, that I used "-exec .... +" and not "-exec ... \;".

    But I'm feeling nice, so let me read the man page to you once again:

    -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.

    > while an "xargs" solution will invoke "mv" more infrequently.


    No, it won't.

    > Now if you trace
    > one start of "mv" you can guess how many CPU and I/O cycles you are wasting.


    Close to none. Actually, your suggestion is wasting CPU and I/O cycles,
    as you're calling xargs. Now, that's just a tiny program, but if we're
    splitting hairs, than please be correct

    Alexander Skwar
    --
    Asynchronous inputs are at the root of our race problems.
    -- D. Winker and F. Prosser


  12. Re: find and cp in one command...

    Alexander Skwar wrote:
    : Ulrich Windl :
    : > Alexander Skwar writes:
    : >> But please explain why "find . -exec mv {} /foo +" is inefficient.

    : Please note, that I used "-exec .... +" and not "-exec ... \;".
    : -exec cmd
    : Any command arguments
    : between the first occurrence of {} and + are ignored.

    Yes, except you can't use options after {}, with +.
    So I never use {} with +.

    But you could use a helper script: -exec move_to_foo_script +
    And the script move_to_foo_script can invoke one "mv $* /foo"

  13. Re: find and cp in one command...

    · Dennis Handly :

    > Alexander Skwar wrote:
    > : Ulrich Windl :
    > : > Alexander Skwar writes:
    > : >> But please explain why "find . -exec mv {} /foo +" is inefficient.
    >
    > : Please note, that I used "-exec .... +" and not "-exec ... \;".
    > : -exec cmd
    > : Any command arguments
    > : between the first occurrence of {} and + are ignored.
    >
    > Yes, except you can't use options after {}, with +.


    Yes, I know. I said myself that my command doesn't work. I'd
    like to know, why Ulrich thinks that find | xargs is more
    efficient than find -exec +. IMO find -exec + is extremely
    minimally more efficient, as only 1 command is executed (find),
    whereas with find | xargs, 2 commands (find, xargs) are run and
    data is piped from one process to another one; but in all practical
    purposes, I'd say that both commands are equivalently
    efficient.

    > But you could use a helper script: -exec move_to_foo_script +
    > And the script move_to_foo_script can invoke one "mv $* /foo"


    This might introduce problems, no? Eg. what happens with files named like "foo
    bar" (ie. "foo\nbar")?

    Alexander Skwar
    --
    I saw what you did and I know who you are.


  14. Re: find and cp in one command...

    Alexander Skwar writes:

    [...]
    > Yes, I know. I said myself that my command doesn't work. I'd
    > like to know, why Ulrich thinks that find | xargs is more
    > efficient than find -exec +. IMO find -exec + is extremely


    Ok, OK: Maybe because I'm too long in this business and did not hear about
    that '+' extension to -exec. Are you happy now?

    [...]

    Ulrich

+ Reply to Thread