1

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?

terdon
  • 242,166

4 Answers4

2

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.

slowko
  • 321
1

Just loop over the file and copy:

while read file; do cp "$file" /path/to/target/dir; done < list.txt
terdon
  • 242,166
0

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.

kenorb
  • 20,988
-1

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.

jhilmer
  • 554
  • 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