Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

What I have tried:

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?
Posted
Updated 4-May-20 4:44am
Comments
Richard MacCutchan 4-May-20 9:28am    
It largely depends on the names of the files (do they all have some part of their name in common), and where you want to copy them to. you may just need a single command, or a more complex bash script.

Some options for you
If you just want to copy the directories from one place to another then use cp
cp -rp dir1 dir2 dir3 ... dest

The -rp flags tell cp to copy recursively and keep permissions/timestamps on the copies.

If you want to move the directories, permanently to a new location the use mv
mv dir1 dir2 dir3 ... dest


If you just need to provide access from your destination directory to your data, then perhaps you can just set up soft links
ln -s dir1 dir2 dir3 ... dest
This is fast, as it does not actually copy data anywhere, and if you modify any data in dir1 (e.g.), then it is available instantly to anyone looking at it from dest.

If you want to copy your data to dest, and you need to update regularly, but only a subset of files will be changed, then consider using rsync
rsync -ar dir1 dir2 dir3 ... dest
After the initial copy, this will copy only those files that are different between the source and destination directories, which will dramatically cut down the data transfer on copies after the first one.

All these commands support a -v option to be verbose about what its doing, so you can see what the command is doing.
 
Share this answer
 
Gabriel9999 wrote:
Should I write a bash script to copy one by one?
Yes; if there are several destinations, you cannot write a single command for all of them.
 
Share this answer
 
What about the documentation: cp(1): copy files/directories - Linux man page[^] ?
If a single instance of cp doesn't fit your needs (not very clear to me) then, yes, you have to write the shell script.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900