Copying files into several Linux directories calls for a way to handle each destination in turn. The cp command copies files, and a Bash loop can repeat that command for each directory.
I’ll show the loop with sample paths and explain how xargs can handle a destination list generated by find.
TL;DR
To copy one file into several directories, run cp once for each destination in a Bash loop.
- Use cp SOURCE DESTINATION when both paths are known.
- Quote each destination variable so spaces stay inside one path.
- Use xargs -0 for a generated destination list that can contain unusual characters.
What Does cp Copy?
GNU cp copies one or more source paths into a destination directory. The final command block shows multiple source names going into one archive.
A path such as archive/ tells cp to place the copy inside that directory. Use mkdir -p for intended target folders before copying, and remember that cp cannot create missing parent directories itself.
cp SOURCE DESTINATION
cp file-a.txt file-b.txt archive/
How to Copy Files to Several Directories Step by Step
A short script makes the path variables visible before you adapt the example.
Step 1: Choose the source and destination paths
Start with one source file and list each destination directory after in. The script creates sample folders beneath demo-data for a repeatable example.
#!/usr/bin/env bash
set -eu
base="$(dirname "$0")/demo-data"
mkdir -p "$base/source" "$base/dir one" "$base/dir-two"
printf 'sample payload\n' > "$base/source/report.txt"
for destination in "$base/dir one" "$base/dir-two"; do
cp "$base/source/report.txt" "$destination/"
done
printf '%s\n' 'Copied files:'
for destination in "$base/dir one" "$base/dir-two"; do
printf '%s: ' "${destination##*/}"
cat "$destination/report.txt"
done
Save the code as copy_to_dirs_demo.sh and run bash copy_to_dirs_demo.sh from its directory. I ran that command and got the output shown here. The destination containing a space stayed one path.
Copied files:
dir one: sample payload
dir-two: sample payload

The quotes around both path expansions keep “dir one” together as a single argument. set -e also stops the example if a command fails, while set -u reports an unset variable instead of silently treating it as empty.
The script creates its demo folders with mkdir -p. Check each destination variable before the loop so a typo does not create an unintended folder.
Step 2: Replace the sample paths with your own
Assign the source once, then use a destination variable in the loop.
source="$HOME/report.txt"
for destination in "$HOME/Archive A" "$HOME/ArchiveB"; do
cp "$source" "$destination/"
done
The trailing slash signals a directory target, and that directory must exist. To rename the copy, replace “$destination/” with “$destination/report-copy.txt”.
Check Paths, Overwrites, and Filename Handling
Review the directory list and existing filenames before a copy that could replace valued data. The table contrasts path handling with overwrite behavior, so choose the relevant row before running the loop.
| Situation | What happens | What to do |
|---|---|---|
| Several destinations appended to cp | cp treats the final path as its one target | Run one cp call per destination |
| Destination directory is absent | cp reports that it cannot create the target file | Create the directory first or correct the path |
| Destination file already exists | cp normally replaces it | Use cp -i to ask before replacement |
| Destination name contains spaces | Unquoted expansion may split it into arguments | Quote the variable in the loop |
I tried cp with a source and two destination-looking paths and got “cp: target ‘demo-data/missing-two’: No such file or directory”. cp took the last operand as its target and exited with an error. Use the loop above, and reserve -R for copying source directories and their contents.
The table’s -i option asks before replacing an existing file when used interactively, so it can wait indefinitely for input in an unattended run. GNU cp also has -n for skipping existing files. Check the local cp manual when portability matters.
Use xargs when the destination list is generated
Use xargs when another program supplies destination paths. The NUL-safe form below carries each name as a single argument even when a directory contains spaces or line breaks.
find /srv/destinations -mindepth 1 -maxdepth 1 -type d -print0 |
xargs -0 -n 1 cp -- "$HOME/report.txt"
The depth flags limit find to immediate child directories rather than their descendants. I checked the same xargs form with the sample folders and both cmp checks passed.
-print0 and -0 form a matched pair: find emits NUL-separated names, and xargs reads each name without splitting spaces or line breaks. The -n 1 flag starts a separate cp call for each discovered directory, which fits one source copied to many targets. GNU xargs can add -r so an empty search result does not invoke cp at all.
For many source files going to one existing directory, GNU find can batch them with -exec cp -t /backup {} +. The -t flag puts the target first, and + passes multiple matches per cp call.
When the destination operand is a directory, cp uses the source basename. If two sources share that name, copy them to separate targets or preserve their parent paths to avoid a collision.
GNU cp accepts -p to request file-metadata preservation when the operating system permits it. Check the resulting access before relying on another account or service to read the copy.
| Approach | Best fit | Boundary |
|---|---|---|
| Bash loop | A short list of known destinations | Runs one copy after another |
| find with xargs -0 | Destinations selected from a directory tree | Needs a NUL-safe path producer |
| find -exec cp -t | Many sources sent to one directory | GNU cp option and existing target |
| tee | One input stream written to several named files | Also writes to standard output |
Bash expands a brace expression such as report-{a,b}.txt into two source paths. A glob like “$HOME”/*.log selects matching names from a directory. Review the matches before copying when a wildcard could select unrelated files.
Bash does not match hidden names such as .settings with an ordinary * glob. If those files belong in the copy, enable dotglob for that shell or name hidden files explicitly, then inspect the matches before running a large batch.
A nested loop lets each selected source reach each destination. Put one loop around the other and call cp for each source-destination pair.
for source in "$HOME"/report-*.txt; do
for destination in "$HOME/Archive A" "$HOME/ArchiveB"; do
cp "$source" "$destination/"
done
done
If no source name matches, Bash leaves the glob text unchanged unless nullglob is enabled. cp then reports a missing source path.
The NUL byte cannot appear in a pathname, so it marks an unambiguous boundary between entries. Newlines can appear in filenames, which is why line-based lists are not safe for every generated path.
| Path source | Boundary | Safer handling |
|---|---|---|
| Literal destination list | Spaces divide shell words unless quoted | Quote each path |
| Bash pathname glob | Unmatched glob may remain literal text | Review matches before copy |
| find output through a pipe | Whitespace parsing can split pathnames | Pair -print0 with xargs -0 |
Common Errors When Copying to Several Directories
A common mistake is to pass several destination paths to a single cp command.
| Case | Observed symptom | Fix |
|---|---|---|
| Several destination-looking operands after one source | cp: target ‘demo-data/missing-two’: No such file or directory | Loop over destinations and call cp once for each. |
| Destination directory does not exist | cp cannot create the target file in the missing directory | Create intended folders with mkdir -p before copying, or correct the path. |
| A destination contains spaces | An unquoted variable can split into multiple arguments | Quote the destination variable in the loop. |
| The destination file already exists | The copy can replace the existing file | Use cp -i for an interactive prompt or inspect the target first. |
The first error came from the command shown in the receipt. A final slash marks an intended directory, but it does not create that directory, so verify the paths before running the loop.
Conclusion: Choose the Method That Matches the Paths
Use a quoted loop for a small destination list, or find with xargs when the list comes from directory discovery.
- cp command guide: overwrite and directory options.
- Bash for-loop guide: list expansion and quoting.
- Copying a large number of files: the bulk-source task.
FAQ
These answers distinguish multiple source files from repeated destinations.
Can cp copy one file to multiple directories in one call?
No. cp accepts one destination per invocation. Use a loop or a tool such as xargs to run one copy for each directory.
How do I copy several files to one directory?
List the source filenames first and put the destination directory last, for example cp file-a.txt file-b.txt archive/.
Does cp overwrite a file in the destination?
Yes, cp normally replaces an existing destination file. Add -i to prompt before replacement, or inspect the destination before copying.
