Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Piping output of a 'find' to the 'copy' command?

  • 12-08-2004 01:53PM
    #1
    Registered Users, Registered Users 2 Posts: 948 ✭✭✭


    Hi All,
    What's the handiest way to copy the resulting files of a 'find' to a specific location.
    e.g. I want to

    find . -name \*.log

    and copy the resulting files to a specific location.

    Any help would be great,

    Cheers,

    DC.


Comments

  • Closed Accounts Posts: 95 ✭✭krinDar


    henbane wrote:
    Is there any reason you can't use > location at the end of the command.

    e.g. find . -name \*.log > \home\filename

    That will just put the list of filenames into a file called
    filename (and your directory separators are backwards).

    What is needed is:
    find . -name \*.log -exec cp {} /other/directory/  \;
    

    Things to think of are whether there will be any filenames with
    the same name, will there be directories that will match the *.log
    glob as these could cause problems.


  • Registered Users, Registered Users 2 Posts: 678 ✭✭✭briano


    you could use a script some thing like this...

    #!/bin/bash

    FILES=$(find $1 -name $2 -print)

    for file in $FILES
    do
    $(cp $file $3)
    done

    where the command would be like "script <place-run-find-from> <name-of-file> <target-dir>"

    edit: Just saw krindar's reply, probably easier to do that.


  • Closed Accounts Posts: 95 ✭✭krinDar


    You could also look into using xargs. If your find results in a very long list of files it will be too big an argument list for cp. The xargs command builds argument lists for other commands thus avoiding this problem.

    This should work:
    find . -name \*.log -exec xargs cp {} /other/directory/  \;
    

    Xargs is fed from the standard input by default, so that will not work.

    When find gets a match it will try and execute the following command
    (assume the matching is test.log):
    xargs cp test.log /other/directory/

    But as xargs is waiting for input it will do nothing.

    If you *really* wanted to use xargs you would have to resort to something like
    this:
    find . -name \*.log | xargs -n 1 -I ARG cp ARG /other/directory
    

    the important arguments to xargs are:
    -n 1 : execute utility for each argument/field received.
    -I ARG : replace ARG in the utility string (cp ARG /other/directory/) with
    the value passed. This is used because the first argument to copy must
    be the file you want to copy (the source), the second is the destination.

    cp ARG /other/directory : The utility, with ARG being replace by the argument
    as passed to xargs.


  • Registered Users, Registered Users 2 Posts: 948 ✭✭✭dcGT


    Thanks krinDar... that's exactly what I was looking for!

    Cheers,

    DC.


  • Closed Accounts Posts: 79 ✭✭zt


    find . -name "*.log"|awk '{print "cp " $1 "/other/directory/"}' | sh


  • Advertisement
  • Closed Accounts Posts: 395 ✭✭albertw


    find |cpio -pudvm /output/dir


  • Registered Users, Registered Users 2 Posts: 1,419 ✭✭✭nadir


    I use slocate for searching


  • Registered Users, Registered Users 2 Posts: 948 ✭✭✭dcGT


    albertw wrote:
    find |cpio -pudvm /output/dir

    Thanks albertw.. I'll have to have a look at this one ;)

    DC.


Advertisement