This article will illustrate how to copy a local file to a remote system with the SCP command.
Follow the steps:
- Execute the following command to copy a file from a local to a remote system-
scp file.txt [email protected]:/remote/directory - If you want to copy a file named file.txt to a remote server, follow these steps-
- Replace “file.txt” with the name of your file.
- Use “remote_username” as the user on the remote server.
- “10.10.0.2” is the server’s IP address.
- “/remote/directory” is the path to where you want to copy the file.
- If no remote directory is specified, the file will be copied to the user’s home directory. After entering the command, you will be asked for the user password, and then the file transfer will begin.
[email protected]’s password:
file.txt 100% 0 0.0KB/s 00:00
- If you do not include the filename in the destination, the file will be copied with its original name. To save it with a different name, you must specify the new filename.
scp file.txt [email protected]:/remote/directory/newfilename.txt - If the SSH on the remote host uses a port other than the default 22, you can indicate the port by using the -P argument.
scp -P 2322 file.txt [email protected]:/remote/directory - To copy a directory and all its files, simply use the scp command with the -r flag, which recursively copies the entire directory along with its contents.
scp -r /local/directory [email protected]:/remote/directory - When utilising wildcards like * or? to match particular files or directories, it’s important to prevent shell expansion by enclosing the path containing wildcard characters in quotes (“ ”) or single quotes (‘ ’). In the following instance, we are copying all .txt files from the local Projects directory to the Projects directory on the remote server.
scp “~Projects/*.txt” [email protected]:/home/user/Projects/ - If you wish to maintain the file metadata, which includes details like ownership and creation date, pass the -p option. For instance, to copy file.txt to the remote directory while preserving the metadata, use the following command-
scp -p file.txt [email protected]:/remote/directory/
In this manner, you can copy a local file to a remote system with the SCP command. If you run into any difficulty, our support experts will help you out. They are available for you 24X7.
Want more tips on file transfers? Check out our guide on How to Copy Files with Command Line Interface