How to copy files to multiple locations on Linux
Using a series of commands to copy a file to multiple locations or a series of files to a single location can be time consuming, but there are options to speed up the process. This post explains some of them.
Several commands like this can copy a single file to a number of directories on your system:
$ cp myfile dir1 $ cp myfile dir2 $ cp myfile dir3
One way to simplify the task is to type the first command and then repeat the command, specifying only the necessary changes. This method relies on whether the file or directory names are similar enough to only replace part of the names.
# single file to multiple dirs # multiple files to single dir $ cp myfile dir1 $ cp myfile1 dir $ ^1^2^ $ ^1^2^ cp myfile dir2 cp myfile2 dir $ ^2^3^ $ ^2^3^ cp myfile dir3 cp myfile3 dir
Another option is to use xargs Command to copy your file to multiple directories:
$ echo dir1 dir2 dir3 | xargs -n 1 cp -v myfile 'myfile' -> 'dir1/myfile' 'myfile' -> 'dir2/myfile' 'myfile' -> 'dir3/myfile'
In this case the xargs The command uses the information you provide (the directory names) to create each of the three commands required to copy the files to the three directories. That -v Argument ensures you see a list of copied files. That -n ensures that each command takes only one of the arguments provided by echo command in each of the commands that xargs runs. For comparison, when you run a command like this, you see three arguments per line:
$ echo Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec | xargs -n 3 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
A limitation of use xargs To run multiple commands, you must use the arguments that you send to the command echo is always appended to the end of the command being executed, so you can’t use the same technique to copy three files into a single directory. That see The command requires the last string to be the target directory and there is no way to get there xargs to reorder the commands. Obviously something like the following command won’t work.
$ echo file1 file2 file3 | xargs -n 2 cp dir1 cp: target 'file2' is not a directory cp: -r not specified; omitting directory 'dir1'
If you tried a command like the one below, you would also encounter problems. Only the last directory in the list would get a copy of the file.
$ cp xyz dir1 dir2 dir3 cp: -r not specified; omitting directory 'dir1' cp: -r not specified; omitting directory 'dir2'
Another option is to use a to the Command like one of these to iterate over the files:
$ for file in file1 file2 file3 $ for file in `ls file?` > do > do > cp -v $file dir1 > cp -v $file dir1 > done > done 'file1' -> 'dir1/file1' 'file1' -> 'dir1/file1' 'file2' -> 'dir1/file2' 'file2' -> 'dir1/file2' 'file3' -> 'dir1/file3' 'file3' -> 'dir1/file3'
again the -v is used to display the activities of the see command so you can easily see that the command is doing what you expect.
You can also use a similar command as above to copy multiple files to your current directory or to another directory.
$ for file in myfile1 myfile2 myfile3 > do > cp -v $file backups > done 'myfile1' -> 'backups/myfile1' 'myfile2' -> 'backups/myfile2' 'myfile3' -> 'backups/myfile3'
If you update important files frequently and want to save the files to a backup folder, you should probably turn this command into a script, which will make it even less likely that you’ll type a command incorrectly and will certainly make the process a lot easier.
#!/bin/bash for file in myfile1 myfile2 myfile3 do cp -v $file backups done
A script like the following would copy all the files you list as arguments to your backup folder. However, the filenames cannot contain spaces, since each character string is treated as a separate filename.
#!/bin/bash if [ $# != 0 ] # if file names provided then for file in $* do cp -v $file backups done fi
Running a script cron would allow you to backup the updated files automatically. To check whether crontab installed on your system, try a command like this:
$ which crontab $ crontab -l bash: crontab: command not found...
This command checks whether cron installed on Fedora:
$ rpm -q cronie package cronie is not installed
Cronie is the package that delivers cron and related services. To install it on Fedora use a command like this:
$ sudo dnf install cronie
After that you can check them out crontab Command like this:
$ which crontab /usr/bin/crontab
If you don’t have one crontab File not yet created, this is what you will see if you try to view it:
$ crontab -l No crontab for username
When you do this you will see something like this showing the commands set up and when to run them.
$ crontab -l 0 0 23 * * /usr/bin/cp /home/username/bin/cpfiles
In the above example the cp files Script runs every night at 11:00 p.m. provided the crontab daemon (Crond) runs.
Wrap up
There are certainly many ways to copy a bunch of files without having to type them in one at a time see command or filename separately. Use of xargs Commands, loops, and scripts can save you a lot of time and trouble.
Copyright © 2022 IDG Communications, Inc.