Unix copy for each line in file

3

I have a file, file_copy, which has:

>cat file_copy
file1
file2
file3

I want to copy file1, file2, file3 from /source to /dest. One way I know how to do this is to just do:

for line in `cat file_copy` ; do cp /source/$line /dest; done

But I know that there is a better way, probably using find/xargs and/or awk, etc. What is the preferred way of doing this?

prelic

Posted 2013-09-09T14:19:54.397

Reputation: 133

Answers

8

The best way to iterate over lines in a file is using while:

while read line; do cp /source/"$line" /dest/; done < file_copy

And while the above is good enough for most cases, a more robust solution (although rarely needed) is:

while IFS= read -r line; do cp /source/"$line" /dest/; done < file_copy

The IFS= prevents whitespace from being trimmed at the start and end of a line. -r prevents backslashes from being discarded.


Regarding your original command:

  • There's a useless use of cat.

  • You shouldn't read lines with for because it's prone to errors when your lines contain whitespace. In practice, if you find yourself using for with anything else than looping over file names generated by the shell (e.g. for f in *.csv), you're probably doing something wrong.

  • You should also quote your variables to prevent file names with spaces from being broken up into multiple arguments.

slhck

Posted 2013-09-09T14:19:54.397

Reputation: 182 472

3

To improve performance you might consider do this:

tar -cf -  'cat source.txt' | (cd $dest ; tar -xf -)

Compress files inside your source and extract them will do the job faster than loop!

Ami Meidar

Posted 2013-09-09T14:19:54.397

Reputation: 31

1Interesting idea. – JakeGould – 2018-08-07T11:39:58.453