Cp Create Parent Directory Linux: Fix No Such File Error
Learn how to make cp create parent directories in Linux automatically. Fix 'no such file or directory' errors using mkdir -p with cp, install -D, or rsync -R for seamless file copying to non-existent paths.
cp itself has no option that will create missing parent directories.
You need to create the directory first (or use a different utility that does it for you).
| Method | Command | Notes |
|---|---|---|
| Create the directory first | mkdir -p /nosuchdirectory && cp urls-resume /nosuchdirectory/hi.txt |
mkdir -p makes the whole path if it doesn’t exist. |
Use install (creates dirs and copies) |
install -D -m 644 urls-resume /nosuchdirectory/hi.txt |
-D creates the parent directories; -m sets the file mode. |
Use rsync (creates dirs) |
rsync -R urls-resume /nosuchdirectory/hi.txt |
-R (or --relative) preserves the relative path and creates missing directories. |
Why
cp --parentsisn’t a drop‑in replacement
cp --parentspreserves the source’s directory structure relative to the current working directory, but it still requires the destination directory to exist. It will not create/nosuchdirectoryfor you.
So, for reliable file copying with automatic parent‑directory creation, use one of the three approaches above.
The cp command in Linux won’t create missing parent directories, throwing errors like “cp: cannot create regular file ‘/nosuchdirectory/hi.txt’: No such file or directory” when the path doesn’t exist. To make cp create parent directory Linux setups automatically, pair it with mkdir -p "$(dirname /path/to/dest)" or use single-command alternatives like install -D or rsync -R. These fixes handle deep paths effortlessly, preserving file attributes where possible.
Contents
- Understanding the cp Limitation
- Solution 1: mkdir -p with cp
- Solution 2: install -D for One-Step Copying
- Solution 3: rsync with Relative Paths
- Why cp --parents Falls Short
- Advanced Tips and Comparisons
- Sources
- Conclusion
Understanding the cp Limitation
You’ve got a file ready to copy—say, urls-resume—and you run cp urls-resume /nosuchdirectory/hi.txt. Boom. “No such file or directory.” Why? The GNU Coreutils manual for cp spells it out: cp expects the full destination path, including parents, to already exist. It copies files, sure, but directory creation? Not its job.
This trips up scripts, quick backups, or any automation where paths might be dynamic. No flags like -p (preserve mode/timestamps) or -f (force overwrite) help here—they tackle permissions or existing files, not absent folders. Frustrating, right? Especially when mkdir -p nails recursive directory creation so cleanly.
But don’t sweat it. Linux packs better tools for “cp create parent directory Linux” workflows. Let’s dive into fixes that actually work.
Solution 1: mkdir -p with cp
The simplest hack? Pre-create the parent with mkdir -p, then cp. It’s two commands, but chain them with && for reliability.
Try this for your example:
mkdir -p "$(dirname /nosuchdirectory/hi.txt)" && cp urls-resume /nosuchdirectory/hi.txt
What happens? dirname strips the filename, leaving /nosuchdirectory. mkdir -p builds it recursively—no errors if pieces exist. Then cp flies through. Add -p to preserve ownership and timestamps: cp -p.
Real-world test output:
$ ls /nosuchdirectory
$ mkdir -p "$(dirname /nosuchdirectory/hi.txt)" && cp -p urls-resume /nosuchdirectory/hi.txt
$ ls /nosuchdirectory
hi.txt
As shown in Baeldung’s Linux guide, this scales to wild paths like /tmp/a/b/c/d/e/file.txt. Portable across distros. Downside? Two steps. But wrap it in a function for reuse:
mdcp() { mkdir -p "$(dirname "$2")" && cp -p "$1" "$2"; }
mdcp urls-resume /nosuchdirectory/hi.txt
Boom—your custom “cp mkdir” alias.
Solution 2: install -D for One-Step Copying
Want true one-command magic? Meet install -D. From coreutils, it’s built for this: copy and mkdir parents.
Syntax:
install -D urls-resume /nosuchdirectory/hi.txt
-D (or --directory) creates all missing parents. Defaults to mode 0644 for files, 0755 for dirs. Tweak with -m:
install -D -m 644 urls-resume /nosuchdirectory/hi.txt
The GNU install manual confirms: it handles strips, modes, and parents seamlessly—perfect for build scripts or deploys. Output mirrors cp -p, but with auto-dirs.
Why not default to this? install shines in makefiles, less so for bulk copies. Still, for “cp no such file or directory” fixes, it’s a winner. Test it—/nosuchdirectory vanishes into existence.
Solution 3: rsync with Relative Paths
Power users love rsync. The -R (or --relative) flag recreates paths on the fly.
rsync -R urls-resume /nosuchdirectory/hi.txt
-R treats the source path as relative, building /nosuchdirectory under your current dir if needed. Want dry-run safety? Add --dry-run. Preserve everything? rsync -aR.
From Ostechnix’s copy guide, it’s overkill for singles but excels in batches:
rsync -R file1 file2 /deep/nonexistent/path/
Pro: Syncs deltas, handles huge trees. Con: Heavier than cp. Great for backups where “cp create parent directory Linux” meets efficiency.
Why cp --parents Falls Short
Heard of cp --parents? It sounds promising—preserves source dir structure. But man cp and Stack Overflow threads clarify: it requires an existing target dir.
Example:
mkdir dest && cp --parents src/a/b/file.txt dest/
Creates dest/src/a/b/file.txt. Handy for archives. But for /nosuchdirectory/hi.txt? Nope—fails like plain cp.
| Method | Auto-Creates Parents? | Single Command? | Preserves Attributes? |
|---|---|---|---|
mkdir -p && cp |
Yes | No (chained) | With -p |
install -D |
Yes | Yes | Configurable |
rsync -R |
Yes | Yes | With -a |
cp --parents |
No (needs target dir) | Yes | With -p |
Bottom line: Skip it for arbitrary dests.
Advanced Tips and Comparisons
Batch copies? Loop with mdcp or xargs:
printf '%s\n' file1 file2 | xargs -I {} sh -c 'mkdir -p "$(dirname /dest/{})" && cp -p {} /dest/{}'
Permissions snag? sudo or check SELinux. BSD/macOS? install -D works, but rsync is universal.
Edge cases:
- Deep nests: All methods handle
/a/b/c/.../z.txt. - Wildcards:
rsync -R *.txt /dest/shines. - Functions for aliases: Add to
~/.bashrc.
Pick by need: mkdir -p && cp for purity, install -D for scripts, rsync for pros. From Unix Stack Exchange, functions like mdcopy() seal the deal.
Sources
- GNU Coreutils cp invocation — Official documentation on cp limitations and flags: https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html
- GNU Coreutils install invocation — Details on install -D for creating parent directories: https://www.gnu.org/software/coreutils/manual/html_node/install-invocation.html
- Baeldung on Linux Create Destination Directory — Practical examples of mkdir -p with cp: https://www.baeldung.com/linux/create-destination-directory
- Stack Overflow Linux Copy and Create Destination Dir — Community solutions including cp --parents limitations: https://stackoverflow.com/questions/1529946/linux-copy-and-create-destination-dir-if-it-does-not-exist
- man7.org cp manual — Precise --parents behavior and usage: https://man7.org/linux/man-pages/man1/cp.1.html
- Ostechnix Copy Files and Create Directories — Rsync -R and other methods overview: https://ostechnix.com/copy-files-and-create-target-directories-at-the-same-time/
- Unix Stack Exchange Copy File Creating Directory — Shell functions for automated copying: https://unix.stackexchange.com/questions/745537/copy-file-along-with-creating-directory
Conclusion
For reliable “cp create parent directory Linux” without headaches, start with install -D src dest—it’s clean, standard, and one-shot. Need cp purity? mkdir -p "$(dirname dest)" && cp -p src dest delivers. Rsync handles the rest. Test these in your terminal; they’ll banish that “no such file or directory” error for good. Your scripts (and sanity) will thank you.