Linux Command to Copy Multiple Files?

General support questions
Post Reply
emacsx
Posts: 5
Joined: 2016/02/05 04:33:23

Linux Command to Copy Multiple Files?

Post by emacsx » 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.

MartinR
Posts: 566
Joined: 2015/05/11 07:53:27
Location: UK

Re: Linux Command to Copy Multiple Files?

Post by MartinR » 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

emacsx
Posts: 5
Joined: 2016/02/05 04:33:23

Re: Linux Command to Copy Multiple Files?

Post by emacsx » 2020/02/07 14:35:33

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

MartinR
Posts: 566
Joined: 2015/05/11 07:53:27
Location: UK

Re: Linux Command to Copy Multiple Files?

Post by MartinR » 2020/02/07 14:51:49

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.

Whoever
Posts: 1114
Joined: 2013/09/06 03:12:10

Re: Linux Command to Copy Multiple Files?

Post by Whoever » 2020/02/08 18:20:26

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.

MartinR
Posts: 566
Joined: 2015/05/11 07:53:27
Location: UK

Re: Linux Command to Copy Multiple Files?

Post by MartinR » 2020/02/08 20:17:43

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 .

Post Reply

Return to “CentOS 6 - General Support”