How to copy a file to multiple directories using commands

If you have ever used the linux command console, you already know that it is very easy to copy a file or even several files thanks to the cp command, a command that we have all used at some time and that to date has served us very well to copy files using the console.

However, what happens when we want to copy a single file to several directories at the same time? The cp command does not allow to do it directly, having to repeat the cp command over and over again, something that can be cumbersome and impractical. Surely when this has happened you have wondered if there is no utility that allows you to copy a single file to several directories in a single command, without repeating.

Well I have good news for you, since the xargs command will allow you to merge several cp commands into one, saving you time and allowing you to do it in one go, with a single command. The command syntax is as follows:

xargs -n 1 cp -v archivo<<<"/carpeta1/ /carpeta2/" 

As you can see, it's about using the xarg command with the cp command as an argument, putting the name of your file where you put file and the folders where you want to copy it where you put the folder (adding the ones you want). If for example I want to copy the file test.txt in / home / azpe and / home / isaac, the command would be as follows.

xargs -n 1 cp -v prueba.txt<<<"/home/azpe/ /home/isaac/ "

If apart I want to copy it in the folder / home / joaquin and / home / willy, I would put the following command.

xargs -n 1 cp -v prueba.txt<<<"/home/azpe/ /home/isaac/ /home/joaquin/ /home/willy/ "

Thus, we will be able to copy a file in several directories at the same time, something without a doubt very practical and curious and that it never hurts to know. Remember that you can put the number of folders you want in quotes.

A good use I can think of for this command is for example for a teacher who wants to copy a file to all his students. Using this command, could copy the file in one go and in a single command, without having to go around putting a command for each student.


2 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Monica said

    I find it very interesting.

  2.   Sid ragasoom said

    Hi, my idea is to copy a file to multiple external drives using this method. Now, due to ignorance, I cannot put the "txt" file on all the MOUNTPOINT (/ media / XXX / UUID) of the disks, since there will always be new disks. Can you help me with that? From already thank you very much.