Copying Files & Folders Via SCP (Linux)

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 response to “Copying Files & Folders Via SCP (Linux)”

  1. Ansible’s Copy and Fetch Modules – Geek Tech Stuff Avatar

    […] doing this by transferring the files to a physical medium (e.g. a USB pen drive), via email or via scp. However, there are other options including Ansible which has two modules to help, Copy and […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect