How To Copy A File/Directory From Local To Remote Machine In Linux / FreeBSD?

How To Copy A File/Directory From Local To Remote Machine In Linux / FreeBSD?

   

This post was last updated on August 1st, 2020 at 04:20 pm

There are various ways to copy a file or directory from remote to local or local to the remote machine. One of them is copying by using the scp command. The SCP stands for Secure copy is a remote file copy program. It copies files between hosts on a network. SCP is based on SSH, It uses the same authentication and provides the same security as SSH. SCP asks for a password if it needed for authentication. While copying a file, if the file already exists on the remote system then it will replace the content of that file.

Syntax of SCP Command

The syntax of scp is similar to cp command which is used locally for copying a file –

Copying a file to host-

scp source_file [email protected]:directory/target_file

Copying a directory from the host –

scp [email protected]:directory/source_file target_file
scp -r [email protected]:directory/source_dir target_dir

If the remote host uses a port other than 22 then it can be specified in the command-

scp -p port _no [email protected]:directory/source_file target_file

Options used with scp command

OptionsDescriptions
-PSpecifies the port to connect on the remote host
-pPreserves the modification time, access time and modes from the original file
-rRecursively copy entire directories
-vEnables verbose mode, This is helpful in debugging connections, authentication and configuration problem
-TDisable the strict filename checking
-4Forces scp to use IPv4 addresses
-6Forces scp to use IPv6 addresses only
-BSelect batch mode (It prevents asking password or passphrases)
-CUse this option to enable compression
-cSelect the cipher to use for encrypting the data transfer
-FSpecifies an alternative per-user configuration file for SSH
-iSelect the file from which the identity (private key) for public-key authentication is read
-lLimits the used bandwidth specified in Kbit/s
-oUse to pass options to SSH in the format used in SSH_config

 

Example of using scp command

Some examples of using scp command are given below-

Copying a single file from local to the remote machine

To copy a file name file.txt to remote system’s /home directory, use the following command in your terminal.

scp /home/file.txt [email protected]:/home

Copying a single file from remote to the local system

Now if you want to copy a file name file.txt from home directory of the remote system to local systems’s /home directory. Use the following command in your terminal.

scp [email protected]:/home/file.txt /home/

Copying multiple files from local to the remote machine

Instead of copying the whole directory containing multiple files. You can copy more than one file by using the following command.

scp /home/file1.txt file2.txt file3.txt [email protected]:/home

Copying multiple files from remote to the local machine

Similarly, if you want to copy more than one file without copying the whole directory from remote to the local machine. Then you should use the following command in your terminal.

scp [email protected]:/home/file1.txt file2.txt file3.txt /home/

Copying a folder

Now if you want to copy a directory name dir1 to the remote system then use the following command in your terminal.

scp -r /home/dir1 [email protected]:/home/

Copying file to a specific port

If you want to use a specific port then use option -P (in uppercase) in your command. Use the following command in your terminal.

scp -P 21 [email protected]:/home/file.txt /home/

To know more about scp and the options see the manual page of scp. To display it use the following command in your terminal.

man scp
Previous How To Install PyCharm In Linux Mint?
Next How To Install JDK And JRE In Ubuntu?

About author

You might also like

Networking 0 Comments

How To Setup Network File System (NFS) In Linux?

This post was last updated on August 1st, 2020 at 09:37 pmNFS or Network file system is a distributed filesystem protocol. It allows a remote host to mount filesystem over

Networking 0 Comments

How to Use ifconfig to set MAC address in Linux, Unix, FreeBSD etc.

This post was last updated on July 9th, 2020 at 05:08 pmLinux has a built-in command ifconfig that is used to configure wired network interfaces. In this article I am

Networking 0 Comments

DHCPD Quick Setup Instructions

This post was last updated on May 27th, 2020 at 04:44 pmSample of how to set up DHCPD server on Red Hat 7.0 Copy over the sample dhcpd.conf file cp

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply