Copy bulk Files in Linux

Gabriel9999

Member
Joined
Mar 12, 2019
Messages
38
Reaction score
4
Credits
130
I have multiple files in a Linux systems where I want to copy them with a single cp command into a different path and directory. Should I write a bash script to copy one by one?
 


captain-sensible

Well-Known Member
Joined
Jun 14, 2019
Messages
2,531
Reaction score
1,672
Credits
15,089
depends, even writing a bash script might be too much work but depends on diversity , number locations and if you can group into "like" files.

For instance if i want to copy loads of tgz files from /tmp and want to copy them to a directory on my Desktop say called dir2 then i won't bother with a bash script just open a terminal cd to /tmp and



cp *.tgz /home/andrew/Desktop/dir1


then that just leaves a couple of .txz so

cp *.txz /home/andrew/Desktop/dir1


in both cases the file name prior to the * is preserved.
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,317
Reaction score
1,867
Credits
8,257
It depends on what you’re trying to copy/move.

You might need a single cp, or mv command.
Or you might need several.
Or you may need to use a different command altogether, like find (to find files to move/copy) and use finds -exec option to specify an action to perform on the found files/directories.

Or it might require a full blown script.

The cp and mv commands can take a list of several items to copy/move. So if you have several items going to one destination, that should be a one-liner.

If you have different items going to different directories, that could require several commands.

Also, you may be able to take advantage of globbing, or pattern matching using regular expressions to identify the files you want to copy/move.

But without some more specific information, it’s difficult to advise you on what to do next.
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,877
Reaction score
2,620
Credits
28,015
You can also use rsync to copy files locally from one directory to another directory. Just to files withon the source directory: rsync -av /src/foo/* /dest/backup or you could copy over the whole directory to another location. Can you give an example of what you would like to do?
 

jglen490

Well-Known Member
Joined
Mar 10, 2020
Messages
736
Reaction score
500
Credits
4,106
Use rsync.
 

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
6,609
Reaction score
5,366
Credits
19,491
Just passing through and moving this to Command Line - all contributors will be redirected.

Friday here in Oz so

Avagudweegend, all

Chris Turner
wizardfromoz
 

devguy

New Member
Joined
Jul 15, 2020
Messages
2
Reaction score
0
Credits
12
according to this article ,
bulk copy can done using rsync command

rsync -arv /home/centos/sampe-dir /opt/temp-directory/
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top