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

Dani AI

Generated

Short answer: in most cases a one-by-one script is unnecessary. Standard tools (cp, tar, rsync, find/xargs) handle bulk copies far more safely and efficiently. A small script is useful only when the selection logic, logging, retries, or scheduling are required — which is exactly what and were hinting at.

Practical, safe options and gotchas:

  • Preserve metadata and include hidden files: use an archive-style copy rather than plain globbing. Example that copies everything (including dotfiles) while keeping attributes:

    cp -a /path/source/. /path/destination/

    The trailing /. is important for copying the contents of the source directory (including hidden files) rather than the directory itself.

  • For complex lists or many separate paths, use an archive/transmit pipeline so permissions, symlinks and special files survive:

    tar -cf - fileA dirB otherfile | (cd /path/destination && tar -xpf -)

    This is robust when moving device files or preserving exact tree layout.

  • For large, repeated, or network-aware copies use rsync (fast, resumable, verifiable). Example workflow: first run a dry-run to preview what will change, then run for real:

    rsync -aH --progress --dry-run /path/source/ /path/destination/
  • Handling many file names, spaces, or shell limits: generate a NUL-separated list with find -print0 and feed rsync --from0 --files-from=... or use xargs -0 so filenames with whitespace/newlines are safe.

Troubleshooting tips:

  • Always check free space and permissions (ownership preservation may require sudo).
  • Use dry-runs and checksums (or rsync --checksum) when integrity matters.
  • If the command line bails out on too many arguments, switch to a files-from or xargs approach.
  • For concurrency on many small files consider xargs -P or GNU parallel, but test on a subset first.

Context: was right that cp accepts multiple sources; correctly recommends rsync for efficiency; ’s tar suggestion is also valid for full-control transfers. Choose the tool that matches the need: simple one-off copy (cp), reproducible/large/remote syncs (rsync), or exact-archive moves (tar).

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.