Copy Files Between Remote Systems in the Linux Command Line

Here are couple of methods for transferring files between remote system and local machine over SSH.
Warp Terminal

Here's the scenario. You can connect to a remote Linux system over SSH and you find yourself in a situation where you have to copy some files from the remote server to your local system.

How do you do that? You can use the good old scp command in the following manner:

scp remote_username@remote_server_IP:/dir/location/filename local_dir_path

If you want to copy some files from local to remote system:

scp local_dir_path/filename remote_username@remote_server_IP:/dir/location 

Pay attention to the use of colon (:) between the remote server details and the path.

You can also use the rsync command here. Let me go over these steps in detail in this tutorial.

βœ‹
This tutorial assumes you can connect to the remote system using SSH. You need to know the password for the remote user and the remote user should have read/write access to the folder where you want to copy the file.

My environmental setup consists of a Raspberry Pi working as the remote server. I can SSH into the Pi easily from my TUXEDO laptop.

Using scp command

The scp command, short for secure copy, uses the SSH connection to transfer files between remote systems. I like it because its syntax is similar to the cp command.

πŸ’‘
I keep a separate terminal session open with SSH connection into the remote server. This gives me the ability to view and copy the file locations on the remote server. This is important because you won't get tab completion over SCP.

Copy files from local machine to remote server

I have a sample.txt file in the Documents directory. I want to send this file to the remote server in its Template directory.

Do you remember the syntax:

scp local_dir_path/filename remote_username@remote_server_IP:/dir/location 

It will ask for a password which is the password for the user on the remote server.

Let's use it in the example here:

scp Documents/sample.txt [email protected]:~/Templates

And here is the output for the command:

scp copy files from local to remote over SSH

I used the ~ notation for the home directory of the user as it is shorter to write than /home/username.

You can verify it by connecting to the remote server via SSH. This is why I keep a separate terminal session open.

Verify copying of files from local to remote system

Copying multiple files

Use the same logic as the cp command:

scp file1 file2 fileN remote_username@remote_server_IP:/dir/location 

Copy a folder to remote system

Like you do with the cp command, use the recursive option -r:

scp -r Dir1 remote_username@remote_server_IP:/dir/location 

Copy files from remote server to local machine

Let's turn the tables now. Let's copy files from the remote server to the local machine. Here's the syntax:

scp remote_username@remote_server_IP:/dir/location/filename local_dir_path

Let's say I have a few screenshots on the Pi and I want them on my laptop.

scp [email protected]:~/Pictures/sd-card-copy.png .

I copied the remote file to my current working directory.

scp copy remote file to local

Copying multiple files?

This gets tricky as copying multiple files will mean providing full path (including user name and IP address) for all the files. If you can use wild card matching, go for it otherwise, copy the desired files in a new temp directory and copy this temp directory to the local system.

scp -r remote_username@remote_server_IP:/dir_location local_dir_path

Using rsync command

rsync is another powerful command that gives you the ability to copy files between remote systems. Unlike scp, rsync is more than just a simple transfer command, it has more powerful features that make it a good backup tool when used in combination with cron jobs.

Here, I'll only show you how to use it for simple file transfers.

Copy files/directories from local to remote

To copy a file from local system to the remote server, you can use rsync command in this fashion:

rsync local_file_path remote_username@remote_server_IP:/dir/location 

Let's take an example where I want to copy a local directory NewDir into the Documents directory of the remote Raspberry Pi:

rsync -r Documents/NewDir [email protected]:~/Documents

Copy files/directories from remote to local

To copy a file or directory from the remote machine to the local, use rsync in this fashion:

rsync remote_username@remote_server_IP:/file_location local_dir_path

Let's say I want to copy rpdiags.txt from the home directory of the remote system to my current working directory on the local:

rsync [email protected]:~/rpdiags.txt .
use rsync to copy files from remote to local

Conclusion

If you have an SFTP server setup, you can use a GUI tool like FileZilla.

Install and Use FileZilla on Ubuntu to Connect to SFTP Server
FileZilla is a popular open-source GUI tool that lets you transfer files between remote systems using SFTP. Here’s how to use it.

I prefer using the scp command for quick file transfer over SSH connection. I use rsync when I have to make a backup of a folder that consists of numerous files. More on that in some other article. Enjoy πŸ˜„

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries πŸ•΅οΈβ€β™‚οΈ

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.