I need to copy 150 file from a directory containing 900 files. I have the name of all 150 files in a text file, list.txt. How can I do this in Linux?


You can try this with rsync

rsync -av --files-from=list_of_filenames.txt SOURCE_DIR DESTINATION_DIR/

notice the trailing space on the destination dir. rsync works from your current working directory so the file paths in your file list must be relative to that.

| improve this answer | |

Just loop over the file and copy:

while read file; do cp "$file" /path/to/target/dir; done < list.txt
| improve this answer | |

If list with your file paths are already escaped, you can use the following command:

cp -v $(<list.txt) dest/

If your list is too long, then use a while solution as suggested in other answer.

| improve this answer | |

I will do like this:

for f in `cat filenames.txt`; do cp $f destination; done

Where you replace destination with the destination for you files. Often I insert a echo after the do to verify my command is correct by making a dry run.

| improve this answer | |
  • 1
    This will fail if any of the file names contain whitespace. You could fix that by quoting the $f, but then it would still fail for file names with newlines (although, admittedly, that won't be an issue if the file names are in a list). As a general rule, never use a for loop to iterate over things like this. This is bash pitfall number 1. – terdon Dec 22 '16 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.