scp (Secure Copy Protocol) allows you to securely copy files between your computer and a remote server, or between two remote servers. It’s similar to cp, but works over SSH.
This is the most common case. The command copies a file from your machine to a specific folder on the remote server:
scp file.txt username@server_ip:/path/to/the/folder/
- file.txt: The name of the file you want to copy from your computer.
- user@server.com: Your username and the domain or IP address of the remote server.
- :/path/to/the/folder/: The path where you want to save the file on the server. The colon (:) is required.
To download a file from the server to your local machine, the syntax is reversed:
scp username@server_ip:/path/to/the/file.txt /path/local/
- username@server_ip:/path/to/the/file.txt: The full path to the file on the server.
- /path/local/: The directory on your computer where you will save the file.
If you need to copy a folder with all its contents, you must add the -r option to make the copy recursive.
scp -r directory/ username@server_ip:/path/to/the/folder/
- -r: Tells scp to copy the directory and all its subdirectories and files recursively.
- directory/: The folder you want to copy.
specify a port
-p is not the correct option, the correct option to specify a port is -P (with a capital P).
scp -r -P nnnnn folder username@server_ip:/path/directory
If scp ignores port nnnnn, it will try to connect to the default SSH port, which is 22, and so the server will respond with an “Operation timed out” on that port.
error: “scp: stat remote: No such file or directory”
scp: stat remote: No such file or directory often doesn’t mean the folder doesn’t exist, but rather that your user doesn’t have the necessary permissions to view or write to that remote directory.
There are directories /random-name which in Linux is usually owned by the root user or a web server user, and is not accessible to a normal user for security reasons.
To fix this, you should change the destination to a folder where your user actually has write permissions. Your home directory is the safest and most common place to do this(~/).
scp -r -P nnnnn folder username@server_ip:~/
The ~ symbol is a shortcut in Linux that always represents your user’s home directory.
You will see something similar to the image:

Once the folder is in your home directory on the server (/home/username/), you can move it to /random-name-system using sudo
sudo mv ~/folder /random-name-system
When you run this command, you’ll be prompted for your server user password (the same one you use to log in). Once you enter it, the folder will be moved to the new location.