views:

96

answers:

4

I have a list of filenames called list.txt.

I would like to copy the files in this list to a new folder.

How should I go about doing this?

Thanks!

+2  A: 

You can do this:

cp `cat list.txt` new-folder/

Note that you should avoid having spaces in your filenames when using this (another solution would be needed if this is the case).

The backticks execute the cat list.txt command and use the output of that command as part of the command line to cp. Newlines in the output of cat are collapsed to spaces before command line substitution.

Greg Hewgill
Note that this flattens out any directory hierarchy in the files - they will all be in a single directory.
Jonathan Leffler
That's true. The question stated a "list of filenames" instead of a "list of pathnames".
Greg Hewgill
I agree - it is not a problem, unless the questioner had not thought about the issue. Conversely, my answer may not be what the questioner wanted - precisely because it does preserve the directory structure. And it would be far from unheard of to want to trim some parts of the path off the names while preserving some of the directory structure - which none of the answers deals with.
Jonathan Leffler
+2  A: 

use a loop

while read -r line
do
  cp "$line" /destination
done <"file"
ghostdog74
Note that this flattens out any directory hierarchy in the files - they will all be in a single directory.
Jonathan Leffler
yes, but oP doesn't say he needs to have same structure. he specifically indicates "copy ..... to a new folder".
ghostdog74
+2  A: 

If you want to preserve directory structure, use one of these (assuming GNU tar):

cpio -pvdm /destination <list.txt

tar -cf - -F list.txt | tar -C /destination -x

In both cases, the net result is that /destination is prefixed to the names of the files in list.txt.

Jonathan Leffler
A: 

loop:

dest="<destination folder>" #keep the quotes
for i in `cat < list.txt` #loop each line of list.txt
do
cp $i $dest #in each loop copy $i, e/a line of list.txt to $dest
echo "Done."
done

Tested. Make sure "./list.txt" exists, otherwise manually fill in the absolute path. :)

The original poster doesn't want to move the *file* list.txt into the new folder; rather, list.txt contains a list of files to move.
Ian Clelland
my bad! lol I fixed it