How to Fix ‘cp: cannot stat: No such file or directory’ in Linux

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, prompting cp to search for a file named financial and a file named report.pdf.
  • Uninitialised or Misspelled Variables: In shell scripts, writing cp "$SRC_DIR/config.json" . when $SRC_DIR is empty causes the command to query /config.json in 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 treat data.csv as 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 *.png to cp.
  • 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.

  1. Incorrect command syntax that fails:
    FILE="server backup 2026.tar.gz"
    cp $FILE /var/backups/
    Output:
    cp: cannot stat 'server': No such file or directory
    cp: cannot stat 'backup': No such file or directory
  2. Correct syntax wrapping the argument in double quotes:
    FILE="server backup 2026.tar.gz"
    cp "$FILE" /var/backups/
  3. 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.

  1. Inspect your copy command syntax:
    # Incorrect (fails with stat error):
    cp access.log/ /var/log/backup/
    
    # Correct:
    cp access.log /var/log/backup/
  2. 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.

  1. 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
  2. With nullglob enabled, 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.

  1. 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
  2. Using mkdir -p guarantees the destination folder exists, preventing ambiguous multi-argument copy errors.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.