Let us break down the above command and see how it works. To copy a file to multiple directries using echo, xargs and cp commands, run: $ echo ~/Downloads ~/Documents | xargs -n 1 cp ostechnix.txtĬopy a file to multiple directories using cp, echo and xargs commands in Linux The another way to copy files to multiple locations is by using echo, xargs and cp commands.Īs 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. Method 2: Using echo, xargs and cp commands Now, it will not replace the target file if it already exists in the destination locations. If you do not want to overwrite the existing file, you can use -n flag with cp command like below: $ find ~/Downloads/ ~/Documents/ -maxdepth 0 -type d -exec cp -n ostechnix.txt \ So, don't forget to specify it.Īs I warned earlier, if there is any file already present with the same name (i.e ostechnix.txt) in any of the destination directories, the above command will simply overwrite the existing file without any confirmation. If you don't specify '-maxdepth 0' option, the ostechnix.txt file will be copied to ~/Downloads, ~/Documents directories and also into their sub-directories as well. The above command will copy ostechnix.txt file in the current directory to the ~/Downloads and ~/Documents directories. Copy a file to multiple different directories using find and cp command in Linux
0 Comments
Leave a Reply. |