At certain times, it is necessary to duplicate a single file across various locations on our system. There is no need to use the cp command repeatedly.

echo [destination1] [destination2] [destination3]..... | xargs -n 1 cp [/location/samplefile]

The echo command is used to feed the xargs command through the pipe symbol | . The xargs command will take input three times from the echo command and copy the samplefile to the three different locations, using the cp command three times. The cp command will take one argument at a time with option -n 1. To avoid overwriting an existing file with the same name in one of the destinations we have to use n option after the cp command, the whole command becomes :

echo [destination1] [destination2] [destination3]..... | xargs -n 1 cp -n [/location/samplefile

A practical guide for cp command from Linux man pages

option description
-a, --archive Preserve the source's metadata, such as creation date, permissions, and extended attributes.
-b, --backup Create backups for destination files. The backup file has the (~) suffix unless --suffix is used.
-f, --force If the destination file/directory already exists, replace it with the source.
-i, --interactive Ask before overwriting the destination with the source.
-l, --link Create hard links instead of new files.
-n Do not overwrite the destination file if it exists.
-R, -r Copy the source directory recursively.
-u, --update control which existing files are updated; UPDATE={all,none,none-fail,older(default)}

UPDATE controls which existing files in the destination are replaced. 'all' is the default operation when an --update option is not specified, and results in all existing files in the destination being replaced.

'none' is like the --no-clobber option, in that no files in the destination are replaced, and skipped files do not induce a failure.

'none-fail' also ensures no files are replaced in the destination, but any skipped files are diagnosed and induce a failure.

'older' is the default operation when --update is specified, and results in files being replaced if they're older than the corresponding source file.

xargs

More info about this powerful command : xargs

Previous Post Next Post