52

Is there a one-line command/script to copy one file to many files on Linux?

cp file1 file2 file3

copies the first two files into the third. Is there a way to copy the first file into the rest?

1
  • A question is do you want file2 and file3 to occupy independent blocks or not? Feb 21, 2019 at 21:28

13 Answers 13

83

Does

cp file1 file2 ; cp file1 file3

count as a "one-line command/script"? How about

for file in file2 file3 ; do cp file1 "$file" ; done

?

Or, for a slightly looser sense of "copy":

tee <file1 file2 file3 >/dev/null
4
  • 2
    @J.F.Sebastian: Yeah, but since the OP clearly wants this to work for more than just two destination files, I went with the form that highlights the support for that. Maybe I should have written tee <file1 >file2 file3 file4 and dispensed with the two-file parallelism?
    – ruakh
    Mar 3, 2012 at 23:02
  • 1
    Not sure if this is still relevant, but I just want to note that using the tee solution, would not preserve the initial files rights.
    – bioShark
    Dec 14, 2015 at 13:37
  • @bioShark: Right -- it only copies the contents of the files. That's why I described it as "a slightly looser sense of 'copy'".
    – ruakh
    Dec 14, 2015 at 16:39
  • 2
    just for fun, if you need a big list of files tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null
    – styks
    Feb 12, 2016 at 19:52
14

just for fun, if you need a big list of files:

tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null- Kelvin Feb 12 at 19:52

But there's a little typo. Should be:

tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null

And as mentioned above, that doesn't copy permissions.

9

You can improve/simplify the for approach (answered by @ruakh) of copying by using ranges from bash brace expansion:

for f in file{1..10}; do cp file $f; done

This copies file into file1, file2, ..., file10.

Resource to check:

4

(no loops used)

To copy the content of one file (fileA.txt) to many files (fileB.txt, fileC.txt, fileD.txt) in Linux,

use the following combination cat and tee commands:

cat fileA.txt | tee fileB.txt fileC.txt fileD.txt >/dev/null
  • applicable to any file extensions
  • only file names and extensions change, everything else remains same.
3
for FILE in "file2" "file3"; do cp file1 $FILE; done
2

You can use shift:

file=$1
shift
for dest in "$@" ; do
    cp -r $file $dest
done
1
  • 2
    What is the effect of shift? Jan 14, 2017 at 17:06
2

cat file1 | tee file2 | tee file3 | tee file4 | tee file5 >/dev/null

3
  • It probably works, but its is pretty inflexible. And it will likely be inefficient if you are copying large files.
    – Stephen C
    May 22, 2018 at 3:00
  • @StephenC it's a lot closer to the original query though May 22, 2018 at 16:41
  • If you use cat and tee, might as well: cat file1 | tee file2 file3 file4 >/dev/null Oct 26, 2018 at 7:59
1

Use something like the following. It works on zsh.

cat file > firstCopy > secondCopy > thirdCopy

or

cat file > {1..100} - for filenames with numbers.

It's good for small files.

You should use the cp script mentioned earlier for larger files.

3
  • 1
    It assumes the shell is zsh Feb 29, 2016 at 13:24
  • Why does the size of the files matter? Limited by RAM size? Jan 14, 2017 at 17:09
  • Because of the cat processing
    – Thomas
    Jan 14, 2017 at 21:09
1

I'd recommend creating a general use script and a function (empty-files), based on the script, to empty any number of target files.

Name the script copy-from-one-to-many and put it in your PATH.

#!/bin/bash -e
#  _ _____     
# | |___ /_  __  
# | | |_ \ \/ /  Lex Sheehan (l3x)
# | |___) >  <   https://github.com/l3x
# |_|____/_/\_\  
#
# Copy the contents of one file to many other files.

source=$1
shift
for dest in "$@"; do
    cp $source $dest
done

exit

NOTES

The shift above removes the first element (the source file path) from the list of arguments ("$@").

Examples of how to empty many files:

Create file1, file2, file3, file4 and file5 with content:

for f in file{1..5}; do echo $f > "$f"; done

Empty many files:

copy-from-one-to-many /dev/null file1 file2 file3 file4 file5

Empty many files easier:

# Create files with content again
for f in file{1..5}; do echo $f > "$f"; done 

copy-from-one-to-many /dev/null file{1..5}

Create empty_files function based on copy-from-one-to-many

function empty-files()
{
    copy-from-one-to-many /dev/null "$@"
}

Example usage

# Create files with content again
for f in file{1..5}; do echo $f > "$f"; done 
# Show contents of one of the files
echo -e "file3:\n $(cat file3)"

empty_files file{1..5}
# Show that the selected file no longer has contents
echo -e "file3:\n $(cat file3)"

Don't just steal code. Improve it; Document it with examples and share it. - l3x


Here's a version that will preface each cp command with sudo:

#!/bin/bash -e
# Filename: copy-from-one-to-may
#  _ _____     
# | |___ /_  __  
# | | |_ \ \/ /  Lex Sheehan (l3x)
# | |___) >  <   https://github.com/l3x
# |_|____/_/\_\  
#
# Copy the contents of one file to many other files.
# Pass --sudo if you want each cp to be perfomed with sudo
# Ex: copy-from-one-to-many $(mktemp) /tmp/a /tmp/b /tmp/c --sudo

if [[ "$*" == *--sudo* ]]; then
    maybe_use_sudo=sudo
fi

source=$1
shift
for dest in "$@"; do
    if [ $dest != '--sudo' ]; then
      $maybe_use_sudo cp $source $dest
    fi
done

exit
0

You can use standard scripting commands for that instead:

Bash:

 for i in file2 file3 ; do cp file1 $i ; done
0

The simplest/quickest solution I can think of is a for loop:

for target in file2 file3 do; cp file1 "$target"; done

A dirty hack would be the following (I strongly advise against it, and only works in bash anyway):

eval 'cp file1 '{file2,file3}';'
0

Go with the fastest cp operations

seq 1 10 | xargs -P 0 -I xxx cp file file-xxx

it means

  • seq 1 10 count from 1 to 10
  • | pipe it xargs
  • -P 0 do it in parallel - as many as needed
  • -I xxx name of each input xargs receives
  • cp file file-xxx means copy file to file-1, file-2, etc

and if name of files are different here is the other solutions.
First have the list of files which are going to be created. e.g.

one
two
three
four
five

Second save this list on disk and read the list with xargs just like before but without using seq.

xargs -P 0 -I xxx cp file xxx  < list

which means 5 copy operations in parallel:

cp file one  
cp file two  
cp file three  
cp file four  
cp file five  

and for xargs here is the behind the scene (5 forks)

 3833 pts/0    Ss     0:00 bash
15954 pts/0           0:00  \_ xargs -P 0 -I xxx cp file xxx < list
15955 pts/0           0:00      \_ cp file one
15956 pts/0           0:00      \_ cp file two
15957 pts/0           0:00      \_ cp file three
15958 pts/0           0:00      \_ cp file four
15959 pts/0           0:00      \_ cp file five
0

I don't know how correct this is but i have used something like this

echo ./file1.txt ./file2.txt ./file3.txt | xargs -n 1 cp file.txt

Where echo ./file1.txt ... is destination of a file and use it to feed xargs with one "destination" by one. Therefore command xargs -n 1. And lastly cp file.txt, which is self explanatory i think :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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