dot
dot

Reputation: 2833

copying a list of files via terminal

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!

Upvotes: 6

Views: 10413

Answers (4)

user231609
user231609

Reputation: 11

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

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753495

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.

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342283

use a loop

while read -r line
do
  cp "$line" /destination
done <"file"

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 992747

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.

Upvotes: 10

Related Questions