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?
Asked
Active
Viewed 1,587 times
1
terdon
- 234,489
- 66
- 447
- 667
user143252
- 29
- 9
4 Answers
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
- 7
1
Just loop over the file and copy:
while read file; do cp "$file" /path/to/target/dir; done < list.txt
terdon
- 234,489
- 66
- 447
- 667
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.
-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
- 544
- 3
- 5
-
1This 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.](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) – terdon Dec 22 '16 at 14:06