Copying Files & Folders Via SCP (Linux)

scp man page

I’m working on a few projects at the moment and using SCP. I thought I had covered SCP in a post before, but my site’s search engine says otherwise. So here is a post on SCP.

Secure Copy (SCP) can be used, as the name suggests, to securely copy files from one destination to another. I use it fairly regularly to transfer files from my Macbook to my Raspberry Pis. SCP works similar to SSH .

Transferring File From Local Host To Remote Host

scp filename username@remotehost:location_to_save_file

For example, to save log.txt from my local device to my Pi 4 (which is on IP 192.168.0.23), using the user Pi and placing the file into the user’s home folder,  I would use:

scp log.txt pi@192.168.0.23:~/

Transferring A Folder From Local Host To Remote Host

scp -r folder_name username@remotehost:location_to_save_folder

The -r is used to make scp recursively move the folder and all of it’s contents.

For example,to save a folder called “awesome_folder” from my local device to my Pi 4 (which is on IP 192.168.0.23), using the user Pi and placing the folder into the user’s home folder,  I would use:

scp -r awesome_folder pi@192.168.0.23:~/

Changed Ports and SSH Keys

If, like me, you’ve changed the default settings for SSH so that it doesn’t ask for passwords, wants an SSH key and uses a different port then there are few more options that are needed. NOTE: These are case-sensitive, so -p and -P are not the same.

scp -P portnumber -i ssh_key_name filename username@remotehost:location_to_save_file

-i is used to tell SCP to use a particular identity (SSH key)

-P is used to tell SCP to use a particular port for the connection.

For example, to save log.txt from my local device to my Pi 4 (which is on IP 192.168.0.23), using the user Pi and placing the file into the user’s home folder, where the Pi 4 runs SSH over port 5050 and the SSH key is called secret, I would use:

scp -P 5050 -i secret log.txt pi@192.168.0.23:~/

Maintaining File Information

If you are moving files where the access and modification timestamps are important, consider using the -p option as this should preserve the files access and modification details.

Copying Files / Folders From Remote Host To Local Host

To copy files or folders from the remote host to the local host involves switching the command options;

scp username@remotehost:path_to_file local_host_path_to_save_file

For example, to save log.txt from my Pi 4 (which is on IP 192.168.0.23), using the user Pi to my home directory on my MacBook,  I would use:

scp pi@192.168.0.23:~/log.txt ~/

One thought on “Copying Files & Folders Via SCP (Linux)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.