This error occurs when scp cannot locate the file or directory you're trying to transfer. Common causes include incorrect file paths, typos, relative path confusion, or the file not existing at the specified location.
The 'scp: No such file or directory' error means the source file or destination directory cannot be found on the system where the transfer is being initiated. When using scp to copy a file locally to a remote server (scp localfile user@host:/path), scp looks for 'localfile' on your local machine. When copying from remote (scp user@host:/path /local), scp looks for the file on the remote server. If the path is wrong, contains typos, uses unexpanded shell variables, or the file doesn't exist, scp cannot proceed and displays this error.
First, check if the file actually exists at the path you're trying to transfer:
# For local files
ls -la /path/to/your/file.txt
# For remote files, SSH in first
ssh user@host
ls -la /path/to/your/file.txtIf the file is not listed, verify the correct path.
Replace relative paths with absolute paths to avoid ambiguity about the current working directory:
# Avoid this (relative path)
scp myfile.txt user@host:uploads/
# Use this instead (absolute path)
scp /home/user/myfile.txt user@host:/home/user/uploads/Absolute paths start with / and don't depend on which directory your terminal is currently in.
If your file path contains spaces or special characters, wrap it in quotes:
# This will fail if 'my file.txt' has a space
scp my file.txt user@host:/remote/path/
# Quote the path to preserve spaces
scp "my file.txt" user@host:/remote/path/
# For paths with special characters
scp "/path/with/$variables/file.txt" user@host:/remote/path/Ensure the destination directory exists on the target system and you have write permissions:
# SSH into the remote host
ssh user@host
# Check if directory exists
ls -ld /remote/destination/path/
# Create it if it doesn't exist
mkdir -p /remote/destination/path/
# Verify write permissions
touch /remote/destination/path/test.txt && rm /remote/destination/path/test.txtAdd the -v flag to see detailed output about where scp is looking for the file:
scp -v /local/file.txt user@host:/remote/path/The verbose output will show:
- Exact paths being checked
- Connection attempts
- Where the error occurs
- Directory listings as scp tries to locate files
This often reveals typos or path issues you might have missed.
If you're using environment variables in the remote path, ensure they're properly handled:
# This may NOT expand $HOME on the remote system
scp localfile user@host:$HOME/uploads/
# Use literal path instead
scp localfile user@host:/home/username/uploads/
# Or use -O flag to enable OpenSSH's path globbing
scp -O localfile user@host:~/uploads/When you specify a remote path with a variable like $HOME, the local shell may not expand it before sending to the remote system.
Check that you can read the source file. Missing read permissions can cause scp to report the file as not found:
# Check permissions
ls -l /path/to/file.txt
# If you don't have read permission, add it
chmod +r /path/to/file.txt
# Test that you can read it
cat /path/to/file.txtOn some systems, scp may behave differently when you're logged into a server and trying to reference local files. If you run scp while SSH'd into a remote system, the local file path may be interpreted as a remote path on the current system. To avoid this, always run scp from your local machine, not from within an SSH session. For Windows OpenSSH users, path handling can be problematic. Using absolute paths and proper quoting usually resolves these issues. Some systems support the -O flag (use OpenSSH protocol) or -S flag (specify ssh program) for additional control over path handling and globbing behavior. The error 'realpath: No such file' is related but indicates path canonicalization failure when dealing with directory transfers.
Load key "/home/user/.ssh/id_rsa": invalid format
How to fix 'Load key invalid format' in SSH
Bad owner or permissions on /home/user/.ssh/config
How to fix "Bad owner or permissions on .ssh/config" in SSH
Error connecting to agent: Connection refused
How to fix "Error connecting to agent: Connection refused" in SSH
Connection closed by UNKNOWN port 65535
How to fix 'Connection closed by UNKNOWN port 65535' in SSH
Offending ECDSA key in /home/user/.ssh/known_hosts:line
How to fix "Offending ECDSA key in known_hosts" in SSH