The cp (copy) utility is a fundamental GNU/Linux command-line tool used to duplicate files and directory structures across local partitions, mounted network shares, and remote filesystems. However, executing a copy command or running a bash automation script frequently fails with the error message: ‘cp: cannot stat ‘source_file’: No such file or directory’. In Unix operating systems, the kernel’s stat() system call queries the filesystem inode to retrieve metadata before copying file contents. When stat fails, it means the operating system cannot locate any file or folder matching the specified path string. Here is how to diagnose and resolve copy path errors in Linux.
What Causes the ‘cp: cannot stat’ Error?
Understanding how bash processes tokens before passing them to the cp binary helps isolate the problem quickly:
- Unquoted Filenames Containing Spaces: Passing a source path like
cp financial report.pdf /backup/causes bash to split the arguments, promptingcpto search for a file namedfinancialand a file namedreport.pdf. - Uninitialised or Misspelled Variables: In shell scripts, writing
cp "$SRC_DIR/config.json" .when$SRC_DIRis empty causes the command to query/config.jsonin the root filesystem. - Trailing Slashes on Regular Non-Directory Files: Adding a slash to a standard file (e.g.,
cp data.csv/ /dest/) instructs the kernel to treatdata.csvas a directory, failing when it is a regular file. - Globbing Expansion Failures (Null Glob): Running
cp *.png /images/inside a folder containing no PNG images; bash passes the literal asterisk string*.pngtocp. - Relative Working Directory Confusion: Executing scripts from a different working directory where relative paths (such as
../config/app.conf) fail to resolve correctly.
Step 1: Quote All Variables and File Paths
Preventing word splitting is the primary defence against stat errors in bash.
- Incorrect command syntax that fails:
Output:FILE="server backup 2026.tar.gz" cp $FILE /var/backups/cp: cannot stat 'server': No such file or directory cp: cannot stat 'backup': No such file or directory - Correct syntax wrapping the argument in double quotes:
FILE="server backup 2026.tar.gz" cp "$FILE" /var/backups/ - Always wrap shell variable references in double quotes (e.g.,
"$SOURCE_PATH") throughout bash scripts.
Step 2: Remove Trailing Slashes from Regular Source Files
A trailing slash in Unix path syntax explicitly indicates a directory.
- Inspect your copy command syntax:
# Incorrect (fails with stat error): cp access.log/ /var/log/backup/ # Correct: cp access.log /var/log/backup/ - If the source is a file, remove the trailing forward slash to allow
stat()to evaluate it as a regular file inode.
Step 3: Enable nullglob in Bash for Wildcard Copies
When using wildcards in shell scripts, enable nullglob to prevent literal asterisks from being passed to cp.
- Add the following configuration to your shell script:
shopt -s nullglob files=(*.log) if [ ${#files[@]} -gt 0 ]; then cp "${files[@]}" /backup/logs/ else echo "No log files found to copy." fi - With
nullglobenabled, unmatched globs expand to an empty array rather than failing with an unhandled stat error.
Step 4: Use Defensive Path Verification in Scripts
Always verify file existence before attempting copy operations in production scripts.
- Implement conditional verification:
SOURCE_PATH="/opt/application/settings.conf" DEST_DIR="/backup/config/" if [ -f "$SOURCE_PATH" ]; then mkdir -p "$DEST_DIR" cp "$SOURCE_PATH" "$DEST_DIR" echo "Copy operation successful." else echo "Error: Source file '$SOURCE_PATH' does not exist." >&2 exit 1 fi - Using
mkdir -pguarantees the destination folder exists, preventing ambiguous multi-argument copy errors.