Results 1 to 6 of 6

Thread: Copy files from a list

  1. #1
    Join Date
    May 2007
    Location
    London UK
    Beans
    107
    Distro
    Ubuntu 12.04 Precise Pangolin

    Copy files from a list

    I'm having trouble with this. Using locate and egrep I've created a text file listing path/fileneme of multiple files located in various directories. I now want to copy them to a single directory without their original tree structure. I've tried rsync and cpio, but I can't arrive at a satisfactory way of doing it. I'd be grateful for any suggestions.

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Copy files from a list

    unless i misinterpreted your requirement, you can loop through the lines and just copy the files to the destination
    Code:
    while read line
    do
    cp " line" destination
    done < myfile.txt

  3. #3
    Join Date
    May 2007
    Location
    London UK
    Beans
    107
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Copy files from a list

    Quote Originally Posted by ghostdog74 View Post
    unless i misinterpreted your requirement, you can loop through the lines and just copy the files to the destination
    Code:
    while read line
    do
    cp " line" destination
    done < myfile.txt
    Thanks for the quick reply. Forgive me, but I'm entirely new to scripting. My script is:

    Code:
    #!/bin/sh -vx
    while read line
    do
    cp " line" home/mydir/output_directory
    done < /home/mydir/input_file_listing.txt

    But that gives the error:

    + read line
    + cp line home/mydir/output_directory
    cp: cannot stat ` line': No such file or directory

    Input_file_listing.txt contain a single line reading:
    /home/mydir/testfile
    and testfile exists in mydir.
    Have I misunderstood the code you supplied?
    Last edited by pmorton; December 17th, 2007 at 12:45 AM. Reason: clarity

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Copy files from a list

    Quote Originally Posted by pmorton View Post
    Thanks for the quick reply. Forgive me, but I'm entirely new to scripting. My script is:

    Code:
    #!/bin/sh -vx
    while read line
    do
    cp " line" home/mydir/output_directory
    done < /home/mydir/input_file_listing.txt

    But that gives the error:

    + read line
    + cp line home/mydir/output_directory
    cp: cannot stat ` line': No such file or directory

    Input_file_listing.txt contain a single line reading:
    /home/mydir/testfile
    and testfile exists in mydir.
    Have I misunderstood the code you supplied?
    Code:
    while read line
    do
    cp "$line" home/mydir/output_directory
    done < /home/mydir/input_file_listing.txt

  5. #5
    Join Date
    May 2007
    Location
    London UK
    Beans
    107
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Copy files from a list

    Thanks a million. I think you've just kick-started my scripting!
    Regards
    P Morton

  6. #6
    Join Date
    Jul 2006
    Beans
    1

    Re: Copy files from a list

    Hi,

    You can also copy files like this:

    # find / -name "*something*" -type f | awk '{print "cp " $1 " /path/to/directory/."}' | bash

    Br,
    -miha
    Last edited by miha; February 1st, 2013 at 08:41 AM. Reason: On this example there is no need to create file list first

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •