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?

Mike_danvers commented: Yes linux follows a tree structure +3

Recommended Answers

All 6 Replies

To this I really want to write the one word answer of "Yes."

So yes to what you asked to try as well as consider using tar. The tar command is older than a lot of users yet I find Linux users ignoring tar. I can't explain why.

No. You shouldn’t write a bash script. Just use the cp command bulk copy feature and provide all files you want to copy and add the path which is the destination.
cp file1 file2 file3 /mnt/backup

Still probably better to put it in a script file even if you only have to do it once. That way you can check for typos. The more file names you type the higher the chances of typing one incorrectly. Type - proof read - execute.

What about a script that assembles all the file names and concatenates then together before executing the single cp command.

commented: That sounds like tar with extra steps (nod to Rick and Morty memes.) +15

It's ok to use rsync to copy files, even if the target is on your own filesystem.

rsync is superior to cp as it only copies the diffs of the files, so if the target file only exists, but with minor changes, then it'll only copy those parts. It can also compress the files, in case you're copying over a network, which is very very common especially at work. Also if you're copying over the internet, it can copy using the secure shell.

rsync -azv --progress path/to/a.c path/to/b.c /tmp/

This would copy your files in archive mode (keeps the timestamps) with z compression, in vobose mode while showing you the progress.

Do not need write script for this. Actually its a single line command e.g.
You have directory dir1 which contains multiple files and dir2 which is target to copy cp dir1/* dir2/ copy all files

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.