Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
This is a shell scripting question - it's about programming. –  Jonathan Leffler Nov 11 '09 at 1:18

4 Answers 4

up vote 6 down vote accepted

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.

share|improve this answer
    
Note that this flattens out any directory hierarchy in the files - they will all be in a single directory. –  Jonathan Leffler Nov 11 '09 at 0:01
    
That's true. The question stated a "list of filenames" instead of a "list of pathnames". –  Greg Hewgill Nov 11 '09 at 0:08
    
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 Nov 11 '09 at 1:17

use a loop

while read -r line
do
  cp "$line" /destination
done <"file"
share|improve this answer
    
Note that this flattens out any directory hierarchy in the files - they will all be in a single directory. –  Jonathan Leffler Nov 11 '09 at 0:04
    
yes, but oP doesn't say he needs to have same structure. he specifically indicates "copy ..... to a new folder". –  ghostdog74 Nov 11 '09 at 0:57

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.

share|improve this answer

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. :)

share|improve this answer
    
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 Dec 14 '09 at 21:08
    
my bad! lol I fixed it –  user231609 Jan 4 '10 at 19:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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