Home › Linux & shell

cp: cannot stat 'file': No such file or directory (Linux) — how to fix

cp: cannot stat 'config/*.yml': No such file or directory

Quick fix: Check the source exists from where you run cp: pwd; ls -la path/to/file. Use an absolute path or cd first, don't quote globs (cp config/*.yml dest/, not 'config/*.yml'), and use cp -r dir/. dest/ to copy a directory's contents.

What it means

Before copying, cp calls stat() on each source to see what it is. "No such file or directory" (ENOENT) means that path doesn't exist as written, from the current working directory. The quoted name in the message is the exact path cp received, which is the best clue: if it still contains * or ~, the shell didn't expand it.

mv, rsync and install print similar messages for the same reasons.

Cause 1: wrong working directory

Relative paths are relative to where the command runs, not where the script lives:

pwd
ls -la config/app.yml

In scripts, resolve paths from the script's own location:

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cp "$SCRIPT_DIR/config/app.yml" /etc/myapp/

Cron jobs, systemd services and CI steps often start in a different directory (cron starts in the user's home). Use absolute paths there.

Cause 2: the glob matched nothing

If no file matches *.yml, bash leaves the pattern as-is and cp looks for a file literally named *.yml:

cp build/*.js dist/
# cp: cannot stat 'build/*.js': No such file or directory

Check what the glob matches with ls build/*.js. Maybe the build step didn't run, or files have another extension. In scripts you can skip safely when nothing matches:

shopt -s nullglob
files=(build/*.js)
(( ${#files[@]} )) && cp "${files[@]}" dist/

Cause 3: quoting stopped expansion

Globs and ~ are not expanded inside quotes:

cp '~/notes.txt' /tmp/      # looks for a dir literally named "~"
cp "src/*.c" out/           # looks for a file named "*.c"

Leave the wildcard and tilde outside quotes, and quote only the parts with spaces:

cp ~/notes.txt /tmp/
cp "My Files"/*.c out/

Cause 4: spaces or hidden characters in the name

An unquoted path with spaces is split into several arguments, each "not found":

cp My Report.pdf /tmp/          # cannot stat 'My' and 'Report.pdf'
cp "My Report.pdf" /tmp/

Scripts edited on Windows can carry a trailing carriage return (\r) in a filename variable, so file.txt\r doesn't exist. cat -A script.sh shows ^M at line ends; fix with sed -i 's/\r$//' script.sh. The line ending inspector shows the same thing in the browser.

Cause 5: copying a directory's contents

cp -r src/* dest/      # skips dotfiles; fails if src is empty
cp -r src/. dest/      # copies everything, including hidden files

src/. works even when src has only hidden files or is empty.

Cause 6: case and typos

Linux filesystems are case-sensitive: Config.yml and config.yml are different files. Use tab completion to build the path, or find . -iname 'config.yml' to locate it.

Inside Docker builds

In a Dockerfile, RUN cp runs inside the image, where only files from earlier COPY steps exist. If the source should come from your machine, use COPY instead, and see the page on COPY failed: file not found.

Last updated 2026-12-03.