Linux scp Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux scp (secure copy) command to securely copy files and directories between local and remote hosts. The scp command is a part of the SSH (Secure Shell) suite of tools and provides a secure way to transfer data over a network. You will explore the basic syntax of the scp command, learn how to copy files between local and remote hosts, and discover how to recursively copy directories using scp. This lab covers essential networking and communication skills for Linux users.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") linux/RemoteAccessandNetworkingGroup -.-> linux/scp("`Secure Copying`") subgraph Lab Skills linux/mkdir -.-> lab-422901{{"`Linux scp Command with Practical Examples`"}} linux/cp -.-> lab-422901{{"`Linux scp Command with Practical Examples`"}} linux/touch -.-> lab-422901{{"`Linux scp Command with Practical Examples`"}} linux/ssh -.-> lab-422901{{"`Linux scp Command with Practical Examples`"}} linux/scp -.-> lab-422901{{"`Linux scp Command with Practical Examples`"}} end

Introduction to scp Command

In this step, you will learn about the scp (secure copy) command, which is used to securely copy files and directories between local and remote hosts in a Linux environment.

The scp command is a part of the SSH (Secure Shell) suite of tools and provides a secure way to transfer data over a network. It uses the SSH protocol to encrypt the data during the transfer, ensuring the confidentiality and integrity of the transferred files.

To begin, let's explore the basic syntax of the scp command:

scp [options] source_file_or_directory destination_file_or_directory

Here, the source_file_or_directory can be a local file or directory, or a remote file or directory specified in the format user@host:path. The destination_file_or_directory can also be a local or remote location.

For example, to copy a file from the local host to a remote host, you can use the following command:

scp ~/project/file.txt labex@remote_host:/home/labex/project/

This command will securely copy the file.txt file from the local ~/project directory to the /home/labex/project/ directory on the remote host.

Example output:

file.txt                                    100%  123     0.1KB/s   00:00

The output shows that the file was successfully copied to the remote host.

Similarly, to copy a file from a remote host to the local host, you can use the following command:

scp labex@remote_host:/home/labex/project/file.txt ~/project/

This command will securely copy the file.txt file from the /home/labex/project/ directory on the remote host to the local ~/project/ directory.

Example output:

file.txt                                    100%  123     0.1KB/s   00:00

In the next step, you will learn how to use the scp command to copy files and directories between local and remote hosts with more advanced options.

Copying Files Between Local and Remote Hosts

In this step, you will learn how to use the scp command to copy files between the local and remote hosts.

Let's start by copying a file from the local host to the remote host. Ensure that you are in the ~/project directory on the local host:

scp file.txt labex@remote_host:/home/labex/project/

This command will securely copy the file.txt file from the local ~/project directory to the /home/labex/project/ directory on the remote host.

Example output:

file.txt                                    100%  123     0.1KB/s   00:00

Now, let's copy a file from the remote host to the local host:

scp labex@remote_host:/home/labex/project/file.txt ~/project/

This command will securely copy the file.txt file from the /home/labex/project/ directory on the remote host to the local ~/project/ directory.

Example output:

file.txt                                    100%  123     0.1KB/s   00:00

You can also copy multiple files at once using the scp command. For example, to copy two files from the local host to the remote host:

scp file1.txt file2.txt labex@remote_host:/home/labex/project/

This command will securely copy both file1.txt and file2.txt from the local ~/project directory to the /home/labex/project/ directory on the remote host.

Example output:

file1.txt                                   100%  123     0.1KB/s   00:00
file2.txt                                   100%  456     0.4KB/s   00:00

In the next step, you will learn how to use the scp command to recursively copy directories between the local and remote hosts.

Recursive Copying of Directories with scp

In this step, you will learn how to use the scp command to recursively copy directories between the local and remote hosts.

To copy a directory recursively, you need to use the -r (recursive) option with the scp command. Let's start by creating a directory on the local host and adding some files to it:

mkdir ~/project/directory1
touch ~/project/directory1/file1.txt
touch ~/project/directory1/file2.txt

Now, let's copy the entire directory1 directory from the local host to the remote host:

scp -r ~/project/directory1 labex@remote_host:/home/labex/project/

This command will securely copy the directory1 directory, including all its contents, from the local ~/project directory to the /home/labex/project/ directory on the remote host.

Example output:

directory1/                                 100%    0     0.0KB/s   00:00
directory1/file1.txt                         100%  123     0.1KB/s   00:00
directory1/file2.txt                         100%  123     0.1KB/s   00:00

Now, let's copy the directory1 directory from the remote host to the local host:

scp -r labex@remote_host:/home/labex/project/directory1 ~/project/

This command will securely copy the directory1 directory, including all its contents, from the /home/labex/project/ directory on the remote host to the local ~/project/ directory.

Example output:

directory1/                                 100%    0     0.0KB/s   00:00
directory1/file1.txt                         100%  123     0.1KB/s   00:00
directory1/file2.txt                         100%  123     0.1KB/s   00:00

You have now learned how to use the scp command to recursively copy directories between the local and remote hosts.

Summary

In this lab, you will learn about the scp (secure copy) command, which is used to securely copy files and directories between local and remote hosts in a Linux environment. You will explore the basic syntax of the scp command and learn how to copy files from the local host to a remote host, as well as from a remote host to the local host. Additionally, you will learn how to use the scp command with more advanced options to copy files and directories between local and remote hosts.

The lab covers the following key points:

  1. Introduction to the scp command and its usage
  2. Copying files between local and remote hosts using the scp command
  3. Recursive copying of directories with the scp command

Linux Commands Cheat Sheet

Other Linux Tutorials you may like