×

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

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.

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.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

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.

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...

4 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 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 ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

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.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 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.

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.

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.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.