Page 1 of 1

Linux Command to Copy Multiple Files?

Posted: 2020/02/07 12:11:49
by emacsx
I want to copy a rubric.docx file to multiple files with names such as F20161368.docx, F20161981.docx, F20161983.docx... from a list of names in a MS Excel or text file.

Please help with a command or script to do that.

Re: Linux Command to Copy Multiple Files?

Posted: 2020/02/07 12:18:05
by MartinR
Here's your starter:

Code: Select all

for i in F20161368.docx F20161981.docx F20161983.docx
do
  cp rubric.docx $i
done

Re: Linux Command to Copy Multiple Files?

Posted: 2020/02/07 14:35:33
by emacsx
MartinR wrote:
2020/02/07 12:18:05
Here's your starter:

Code: Select all

for i in F20161368.docx F20161981.docx F20161983.docx
do
  cp rubric.docx $i
done
Thanks, but the F20161368.docx F20161981.docx F20161983.docx files do not exist and need to be created as a copy of rubric.docx

Re: Linux Command to Copy Multiple Files?

Posted: 2020/02/07 14:51:49
by MartinR
That's exactly what the script does. You may find this link https://www.gnu.org/software/bash/manual/bash.pdf useful.

* Line 1: Variable i will take the values F20161368.docx, F20161981.docx and F20161983.docx in turn for three passes around the loop.
* Line 2: The start of the loop
* Line 3: The value held in i is substituted for $i, giving for instance cp rubric.docx F20161368.docx, the the line is executed.
* Line 4: The end of the loop.

Re: Linux Command to Copy Multiple Files?

Posted: 2020/02/08 18:20:26
by Whoever
emacsx wrote:
2020/02/07 12:11:49
I want to copy a rubric.docx file to multiple files with names such as F20161368.docx, F20161981.docx, F20161983.docx... from a list of names in a MS Excel or text file.

Please help with a command or script to do that.

Code: Select all

for file in $(cat <text file with list of file names in it>); do cp rubric.docx $file; done
Replace "<text file with list of file names in it>" with the filename of your text file with the list of file names, one per line.

Re: Linux Command to Copy Multiple Files?

Posted: 2020/02/08 20:17:43
by MartinR
That will work with a text file but I doubt it would with Excel - that's why I gave the literal approach.

To emacsx: Whoever's script ib built up as follows:
* $(cat <text file with list of file names in it>) - this runs first and lists the file into a single line.
* for file in <list of names> ; - Just as my annotated example, but the variable is called "file", not "i". The semicolon marks the logical end of line.
* do cp rubric.docx $file; done - My lines 2, 3 and 4 written as a single line.

How many file names are there to copy to? There is a limit to the above expansion and there is a way around it, but it gets a little more complex .