Linux commands to copy one file to many files
Solution 1:
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
Solution 2:
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.
Solution 3:
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:
- http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
Solution 4:
for FILE in "file2" "file3"; do cp file1 $FILE; done