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.
- 1 Syntax of SCP Command
- 2 Options used with scp command
- 3 Example of using scp command
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
|-P||Specifies the port to connect on the remote host|
|-p||Preserves the modification time, access time and modes from the original file|
|-r||Recursively copy entire directories|
|-v||Enables verbose mode, This is helpful in debugging connections, authentication and configuration problem|
|-T||Disable the strict filename checking|
|-4||Forces scp to use IPv4 addresses|
|-6||Forces scp to use IPv6 addresses only|
|-B||Select batch mode (It prevents asking password or passphrases)|
|-C||Use this option to enable compression|
|-c||Select the cipher to use for encrypting the data transfer|
|-F||Specifies an alternative per-user configuration file for SSH|
|-i||Select the file from which the identity (private key) for public-key authentication is read|
|-l||Limits the used bandwidth specified in Kbit/s|
|-o||Use 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.
You might also like
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
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