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

The mv (move) utility is one of the most frequently used commands in Unix and Linux environments, responsible for relocating files across directories and renaming files. However, running a move command or executing an automated bash deployment script often terminates with the error: ‘mv: cannot stat ‘source_file’: No such file or directory’. In POSIX systems programming, the term stat refers to the system call (stat()) that queries the kernel for metadata about a specific file path. When stat fails, it means the operating system cannot locate any filesystem entry matching the path provided. Here is how to diagnose and resolve this error across your shell scripts.

Why Does the ‘mv: cannot stat’ Error Occur?

Understanding why stat() fails helps identify whether the breakdown is caused by syntax errors, word splitting, or missing directory paths:

  • Unquoted Filenames Containing Spaces: Passing a filename like my document.pdf without quotation marks causes bash to interpret my as the source file and document.pdf as another argument, failing when my does not exist.
  • Unexpanded Shell Variables: Referencing an uninitialised or misspelled variable (e.g., mv $SOURCE_DIR/file.txt . when $SOURCE_DIR is empty), causing the command to look for /file.txt in the root directory.
  • Unmatched Wildcard Globs: Using glob patterns like mv *.tar.gz /backup/ in an empty directory where no matching archives exist; bash passes the literal string *.tar.gz to mv.
  • Trailing Slash Misplacement on Source Files: Adding a trailing slash to a regular non-directory file (e.g., mv file.txt/ /dest/), which prompts the kernel to look for a directory named file.txt.
  • Relative Working Directory Discrepancies: Running shell scripts via cron or remote SSH sessions where the current working directory differs from the relative path assumption.

Step 1: Properly Quote Variables and File Paths

Word splitting across space characters is the most common reason mv fails with a stat error.

  1. Incorrect syntax that triggers the error:
    FILE="backup log 2026.txt"
    mv $FILE /var/backups/
    Output:
    mv: cannot stat 'backup': No such file or directory
    mv: cannot stat 'log': No such file or directory
  2. Correct syntax wrapping the variable in double quotes:
    FILE="backup log 2026.txt"
    mv "$FILE" /var/backups/
  3. Always double-quote variables in shell scripts ("$SOURCE") to ensure arguments containing spaces or tabs are parsed as a single unified token.

Step 2: Remove Trailing Slashes from Regular Files

In Linux filesystem path semantics, a trailing forward slash (/) explicitly designates a directory.

  1. Check your move command syntax:
    # Incorrect:
    mv report.pdf/ /home/user/documents/
    
    # Correct:
    mv report.pdf /home/user/documents/
  2. If the source is a regular file, appending a trailing slash forces the stat() system call to treat it as a directory, immediately returning Not a directory or No such file or directory.

Step 3: Enable nullglob in Bash to Handle Unmatched Wildcards

When you run mv *.csv /archive/ and no CSV files exist, bash passes the literal asterisk pattern string to mv.

  1. In shell scripts, enable the nullglob option so unmatched patterns expand to an empty list rather than a literal string:
    shopt -s nullglob
    files=(*.csv)
    if [ ${#files[@]} -gt 0 ]; then
        mv "${files[@]}" /archive/
    else
        echo "No CSV files found to move."
    fi
  2. This prevents mv from trying to stat a non-existent file named literally *.csv.

Step 4: Verify Source File Existence Before Moving

Implementing defensive file checks prevents scripts from executing failed operations:

  1. Wrap the move operation inside a conditional test:
    SOURCE_FILE="/var/log/nginx/access.log.1"
    DEST_DIR="/backup/logs/"
    
    if [ -f "$SOURCE_FILE" ]; then
        mv "$SOURCE_FILE" "$DEST_DIR"
        echo "File moved successfully."
    else
        echo "Warning: Source file '$SOURCE_FILE' does not exist." >&2
    fi
  2. Use -f to test for regular files or -d to test for directories before initiating the move operation.

Step 5: Verify the Destination Directory Exists

If you specify a destination path where intermediate directories do not exist, mv can fail if target syntax is ambiguous.

  1. Ensure target directories exist before moving files into them:
    mkdir -p /backup/logs/2026/
    mv "$SOURCE_FILE" /backup/logs/2026/
  2. Using mkdir -p creates missing parent directories automatically, ensuring the destination path is always valid.

Get the best tech tips delivered straight to your inbox.

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