26.6 C
Jaipur
Friday, August 6, 2021

Copy A File To Multiple Directories In Command Line On Linux

Must read

In this brief guide, we will discuss how to copy a file to multiple directories from command line using tee and find commands on Linux.

The other day I wanted to copy a movie to two different folders in my Arch Linux desktop. As you already know, We can easily do it by right-clicking on the movie file, select Copy option and paste it on the destination directory/folder.

However, I’d like to know if there are any other way to copy the file into multiple directories in one go. I thought It would help when I want to copy a single file into number of different directories at once. I did a few web search and come up with three solutions.

Copy a file to multiple directories from command line on Linux

I have text file called ostechnix.txt in my home directory, and I wanted to copy it to two different directories called Downloads and Documents in one go.

First, let us see how to copy the file to multiple different directories using tee command.

Method 1 – Using tee command

Tee is a Unix and Linux utility used to read from standard input and write to standard output and files.

Now, let us copy the ostechnix.txt file into two different directories called /home/sk/Downloads and /home/sk/Documents.

To do so, just run the following command from the Terminal:

$ tee /home/sk/Downloads/ostechnix.txt /home/sk/Documents/ostechnix.txt < /home/sk/ostechnix.txt

The above command will copy the ostechnix.txt file from home directory to ~/Downloads and ~/Documents directory.

Please note that tee command will also write the input to the standard output. If you don’t want tee command to do this, just redirect the standard output to /dev/null as shown below.

$ tee /home/sk/Downloads/ostechnix.txt /home/sk/Documents/ostechnix.txt < /home/sk/ostechnix.txt >/dev/null
Copy a file to multiple different directories using tee command in Linux

Please be mindful that if there is any file already present with the same same (i.e ostechnix.txt) in the destination, the above commands will overwrite the existing file.

Method 2: Using find and cp commands

We can copy a single file to multiple directories at once using find and cp commands. The find is a Unix and Linux command to search for files in a directory hierarchy and the cp command is used to copy files and directories.

To copy a file named ostechnix.txt to ~/Downloads and ~/Documents directories using find and cp command, run:

$ find ~/Downloads/ ~/Documents/ -maxdepth 0 -type d -exec cp ostechnix.txt {} ;
Copy a file to multiple different directories using find command in Linux
Copy a file to multiple different directories using find and cp command in Linux

The above command will copy ostechnix.txt file in the current diretory to the ~/Downloads and ~/Documents directories.

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. So, don’t forget to specify it.

Method 3: Using echo, xargs and cp commands

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.

To copy a file to multiple directries using echo, xargs and cp commands, run:

$ echo ~/Downloads ~/Documents | xargs -n 1 cp ostechnix.txt

Let us break down the above command and see how it works.

Generally, the echo command is used to print the string or text to the standard output. But in this case, we pipe the output of the echo command to xargs. Here, echo command will send the directory path to xargs.

xargs command runs the cp command two times and appends the path of each directory on to the end of the cp command.

The -n 1 option on the xargs command tells the xargs to append only use one of those arguments (i.e. directory path) at a time.

That’s it. There might be other ways to copy a file to multiple directories at once. But, I believe these commands are very simple and easy to use.

If you’re new to Linux, I always suggest you to test these commands in any test machines before start using them in the production. This way you can prevent the unnecessary loss of data.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article