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.pdfwithout quotation marks causes bash to interpretmyas the source file anddocument.pdfas another argument, failing whenmydoes not exist. - Unexpanded Shell Variables: Referencing an uninitialised or misspelled variable (e.g.,
mv $SOURCE_DIR/file.txt .when$SOURCE_DIRis empty), causing the command to look for/file.txtin 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.gztomv. - 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 namedfile.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.
- Incorrect syntax that triggers the error:
Output:FILE="backup log 2026.txt" mv $FILE /var/backups/mv: cannot stat 'backup': No such file or directory mv: cannot stat 'log': No such file or directory - Correct syntax wrapping the variable in double quotes:
FILE="backup log 2026.txt" mv "$FILE" /var/backups/ - 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.
- Check your move command syntax:
# Incorrect: mv report.pdf/ /home/user/documents/ # Correct: mv report.pdf /home/user/documents/ - 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.
- In shell scripts, enable the
nullgloboption 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 - This prevents
mvfrom 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:
- 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 - Use
-fto test for regular files or-dto 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.
- Ensure target directories exist before moving files into them:
mkdir -p /backup/logs/2026/ mv "$SOURCE_FILE" /backup/logs/2026/ - Using
mkdir -pcreates missing parent directories automatically, ensuring the destination path is always valid.