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?
cp can copy a single file to a different filename (i.e. "rename" the destination), but there is no way to specify different filenames for multiple files. So the answer is no, cp can not rename when copying multiple files. -t is partcicularly useful when used with, e.g., ... | xargs cp -t destdir/ or find ...
The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.
The another way to copy files to multiple locations is by using echo , xargs and cp commands. As you already know, the cp command is used to copy the files and directories, and xargs command is used to build and execute command lines from standard input.
cp (copy) - The cp command is used to copy files or directories in the Linux system. If we want to copy a file from a source directory and paste it into another destination directory then we have to use the cp command.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With