Thursday, September 8, 2022
HomeNetworkingHow one can copy information to a number of areas on Linux

How one can copy information to a number of areas on Linux


Utilizing a sequence of instructions to repeat a file to a number of areas or quite a lot of information to a single location might be time-consuming, however there are alternatives to hurry up the method. This submit explains a few of them.

A number of instructions like these can to repeat a single file to a sequence of directories in your system:

$ cp myfile dir1
$ cp myfile dir2
$ cp myfile dir3

One option to make the duty simpler is typing the primary command after which repeat the command by solely specifying the wanted adjustments. This methodology depends on whether or not the file or listing names are comparable sufficient to substitute just some portion of the names.

# single file to a number of dirs        # a number of information 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

An alternative choice is to make use of the xargs command to repeat your file to a number of directories:

$ echo dir1 dir2 dir3 | xargs -n 1 cp -v myfile
'myfile' -> 'dir1/myfile'
'myfile' -> 'dir2/myfile'
'myfile' -> 'dir3/myfile'

On this case, the xargs command makes use of the data you present (the listing names) to compose every of the three instructions required to repeat the information to the three directories. The -v argument ensures that you will note a listing of the information being copied. The -n ensures that every command will use solely one of many arguments supplied by the echo command in every of the instructions that xargs runs. As a comparability, in case you ran a command like this, you’ll see three arguments per line:

$ echo Jan Feb Mar Apr Might Jun Jul Aug Sep Oct Nov Dec | xargs -n 3
Jan Feb Mar
Apr Might Jun
Jul Aug Sep
Oct Nov Dec

One limitation of utilizing xargs to run a number of instructions is that the arguments you ship to the command utilizing echo will all the time be tacked to the top of the command that’s run, so you may’t use the identical approach to repeat three information to a single listing. The cp command requires that the ultimate string be the goal listing and there’s no option to get xargs to rearrange the instructions. Clearly one thing just like the command beneath won’t work.

$ echo file1 file2 file3 | xargs -n 2 cp dir1
cp: goal 'file2' will not be a listing
cp: -r not specified; omitting listing 'dir1'

If you happen to tried a command just like the one beneath, you’d additionally run into issues. Solely the final listing within the listing would get a duplicate of the file.

$ cp xyz dir1 dir2 dir3
cp: -r not specified; omitting listing 'dir1'
cp: -r not specified; omitting listing 'dir2'

An alternative choice is to make use of a for command like considered one of these to loop by way of the information:

$ for file in file1 file2 file3        $ for file in `ls file?`
> do                                   > do
>   cp -v $file dir1                   >   cp -v $file dir1
> achieved                                 > achieved
'file1' -> 'dir1/file1'                'file1' -> 'dir1/file1'
'file2' -> 'dir1/file2'                'file2' -> 'dir1/file2'
'file3' -> 'dir1/file3'                'file3' -> 'dir1/file3'

Once more, the -v is used to show the actions of the cp command so as to simply see that the command is doing what you count on.

You would additionally use a command just like what you see above to repeat a number of information into your present or into one other listing.

$ for file in myfile1 myfile2 myfile3
> do
>   cp -v $file backups
> achieved
'myfile1' -> 'backups/myfile1'
'myfile2' -> 'backups/myfile2'
'myfile3' -> 'backups/myfile3'

If you happen to replace necessary information steadily and wish to save the information right into a backups folder, you must in all probability flip this command right into a script, making it even much less probably that you simply’ll mistype a command and positively making the method quite a bit less complicated.

#!/bin/bash

for file in myfile1 myfile2 myfile3
do
  cp -v $file backups
achieved

A script just like the one beneath would take any information you listing as arguments and replica them to your backups folder. The file names, nevertheless, can’t comprise blanks since every string is handled as a separate filename.

#!/bin/bash

if [ $# != 0 ]	# if file names supplied
then
  for file in $*
  do
    cp -v $file backups
  achieved
fi

Working a script by way of cron would let you robotically again up the information which might be up to date. To verify if crontab is put in in your system, attempt a command like this:

$ which crontab
$ crontab -l
bash: crontab: command not discovered...

This command will verify if cron is put in on Fedora:

$ rpm -q cronie
package deal cronie will not be put in

Cronie is the package deal that provides cron and associated providers. To put in it on Fedora, us a command like this:

$ sudo dnf set up cronie

After that, you may verify on the crontab command like this:

$ which crontab
/usr/bin/crontab

If you happen to don’t have a crontab file but established, you will note this if you attempt to have a look at it:

$ crontab -l
No crontab for username

If you happen to do, you will note one thing like this exhibiting the instructions which have been arrange and when they’re to be run.

$ crontab -l
0 0 23 * * /usr/bin/cp /residence/username/bin/cpfiles

Within the above instance the cpfiles script will likely be run at 11 p.m.  each night supplied the crontab daemon (crond) is operating.

Wrap-Up

There are actually some ways to repeat a sequence of information with out having to sort every cp command or filename individually. Utilizing the xargs command, loops, and scripts can prevent numerous time and bother.

Be a part of the Community World communities on Fb and LinkedIn to touch upon matters which might be prime of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments