×

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp command, which is used to copy files locally, but scp allows you to copy files between remote hosts. The scp command uses the SSH (Secure Shell) protocol to encrypt the data being transferred, making it a secure way to transfer files over an insecure network.

The basic syntax of the scp command is:

scp [options] [source] [destination]

The "source" is the file or directory that you want to copy, and the "destination" is the location where you want to copy the file or directory.

The most basic use of the scp command is to transfer a single file from one computer to another. For example, to transfer a file called "fruits.txt" from the local computer to a remote computer with IP address 192.168.1.100, you would use the following command:

scp fruits.txt user@192.168.1.100:/home/user/

This command will transfer the "fruits.txt" file from the local computer to the remote computer's home directory for the user "user".

The scp command can also be used to transfer a directory and its contents from one computer to another. To transfer a directory called "fruits" from the local computer to a remote computer, you would use the following command:

scp -r fruits user@192.168.1.100:/home/user/

This command will transfer the "fruits" directory and its contents from the local computer to the remote computer's home directory for the user "user". The -r option is used to transfer the directory and its contents recursively.

The scp command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -P: specify the port number to connect to on the remote host.
  • -r: recursive, transfers the directory and its contents recursively.
  • -v: verbose, provides detailed information about the progress of the transfer.
  • -C: enables compression, which can speed up the transfer.
  • -p: preserves the modification and access times, and permissions of the files during the transfer.

The scp command can also be used in combination with other commands to perform more complex tasks. For example, you can use the scp command in combination with the ssh command to transfer files between computers without the need to enter a password. This can be done by setting up a ssh key pair and copying the public key to the remote computer.

It is important to note that the scp command requires that the SSH service is running on both the local and remote computers, and that the user has the necessary permissions to transfer the files.

Another important feature of the scp command is the ability to transfer files between remote servers, without the need to download them to your local machine first. This can be done by specifying the source and destination files with the format of user@host:path. For example, the following command will transfer a file called "fruits.txt" from a remote server with IP address 192.168.1.100 to another remote server with IP address 192.168.1.200:

scp user@192.168.1.100:/home/user/fruits.txt user@192.168.1.200:/home/user/

Another important feature of the scp command is the ability to limit the maximum transfer rate. This can be done by using the -l option. The -l option is followed by a number that represents the maximum transfer rate in kilobits per second. For example, the following command will transfer a file called "fruits.txt" from the local computer to a remote computer with IP address 192.168.1.100 at a maximum rate of 100 kilobits per second:

scp -l 100 fruits.txt user@192.168.1.100:/home/user/

It is also important to note that the scp command encrypts the data during the transfer, which ensures that the files are transferred securely. It also verifies the authenticity of the remote host using the SSH host key, which helps to prevent man-in-the-middle attacks.

In addition to the scp command, there are also other similar command-line utilities that can be used to transfer files securely over a network connection such as rsync and sftp. Both of these utilities also use the SSH protocol to transfer files, but they have different features and uses. The rsync command is used to synchronize files and directories between computers, and the sftp command is used for interactive file transfers over an SSH connection.

In summary, the scp command in Linux is a powerful command-line utility that is used to securely transfer files between computers using the SSH protocol. It has several options that can be used to modify its behavior, such as specifying the port number, transferring files recursively, and preserving the modification and access times, and permissions of the files during the transfer. It also allows to transfer files between remote servers, limit the maximum transfer rate, and has built-in encryption and host key verification. It is an essential command for anyone who needs to transfer files securely over a network connection. It is important to be aware of other similar command-line utilities such as rsync and sftp, which can also be used for secure file transfer, but have different features and uses.


Related Topics

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.