How to transfer file and directory? how to copy files and directory? If you think it is very easy and simple and you can use cp commands to copy and transfer files and directory in the local computer.
Then you are not wrong, you are absolutely right.
But it will be hard for an answer if asked about the transfer file between the two systems that have different locations.
Oh Yeah, you are not thinking wrong. You can use ftp to perform the same task.
Do you know one more thing? You can use ssh to transfer files between two systems remotely. And it is possible by SCP command in Linux. To know more about ssh server read my article How to Enable ssh on Ubuntu 19.04
I am going to cover in this article, What is scp in Linux? How to use SCP commands in Linux. and how to transfer files and directory from local to the remote system and remote to local or remote to the remote system and many more.
- What is SCP in Linux?
- Basic Syntax How to use SCP command in Linux
- SCP command to copy file and directory from remote to local System
- Transfer file and directory with SCP from local to remote.
- How to use SCP to transfer file one remote to another remote system
- How to use SCP command in Linux Video Tutorial in Hindi
What is SCP in Linux?
SCP stands for secure copy, it is a command-line utility that allows users to copy files and directories securely between two and more systems.
By using SCP command, You can copy a file or directory:
- From a remote machine to your local machine
- From a local machine to a remote machine.
- Between two remote systems from your local computer.
When you transfer data from one system to another system by using ssh it’s transferred over the secure channel. Hackers can’t sniff data when transferring over the secure channel. You can send anything sensitive through SCP.
SCP commands Table
|$scp [OPTION] [[email protected]]SRC_HOST:]file1_Path [[email protected]]DEST_HOST:]file2||Basic syntax of scp command in Linux|
|$scp file.txt [email protected]:/remote/directory||Copy local file to remote System|
|scp -r mydata/ [email protected]:/remote/directory/new_name||Copy local Directory to remote System|
|ssh -P 4444 file.txt [email protected]:/remote/directory||Copy data using different port|
|scp [email protected]:/remote/path_to_file/file.txt /local/directory||Copy file from remote to local|
Basic Syntax How to use SCP command in Linux
Let’s discuss basic scp command syntax before executing any task by scp, Basic command syntax as follows:
- OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy ..etc
- [[email protected]_HOST:]file1_Path – Source file with path
- [[email protected]_HOST:]file2 – Destination path and file name
You can specify local files by using an absolute or relative path while you should use user and host address for the remote file.
scp provides a number of scp options that control every aspect of its behavior. The most widely used options are:
- -P Specifies the remote host ssh port.
- -p Preserves files modification and access times.
- -q Use this option if you want to suppress the progress meter and non-error messages.
- -C. This option will force scp to compresses the data as it is sent to the destination machine.
- -r This option will tell scp to recursively copy directories.
I am using Ubuntu 19.04 and I write command scp and hit enter. As I hit the enter I get all available options for scp command. Look at the following example.
[email protected]:~$scp usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target [email protected]:~$
Transfer file and directory with SCP from local to remote.
I want to copy from local to a remote system by using scp command, I will use the following syntax:
$scp file.txt [email protected]:/remote/directory
- scp – Command
- file.txt – It is the source file and path what I want to copy.
- [email protected] address – identification of remote hosts.
- /remote/directory path on a remote system where you want to copy.
See the following example:
[email protected]:~# scp newfile.txt [email protected]:/home/vijay/ [email protected]'s password: newfile.txt 100% 0 0.0KB/s 00:00 [email protected]:~#
In another example, I am trying to copy a directory but Getting error “not a regular file”
[email protected]:~# scp mydata/ [email protected]:/home/vijay/ [email protected]'s password: mydata: not a regular file [email protected]:~#
If you want to copy a directory then you can use scp command followed by -r option. See Example below.
[email protected]:~#scp -r mydata/ [email protected]:/home/vijay/ [email protected]'s password: [email protected]:~#
If you want to copy file and directory with another name on the remote system then you need to specify a new name
$ scp -r mydata/ [email protected]:/remote/directory/new_name
[email protected]:~#scp -r mydata/ [email protected]:/home/vijay/remote_mydata [email protected]'s password: [email protected]:~#
By default, ssh is running on port no. 22, if the remote host using a different port then you can specify the port no using -p option
$ssh -P 4444 file.txt [email protected]:/remote/directory
scp command to copy file and directory from remote to local System
If you want to copy a file from a remote system to a local system by using scp command, then put the remote location as a source address and local location as a destination.
For example, I want to copy a file named file.txt from the remote server having IP address 192.168.225.46 following command syntax will be used.
$scp [email protected]:/remote/path_to_file/file.txt /local/directory
How to use scp to transfer file one remote to another remote system
When you are using scp command to copy files from host1 system to host2 system remotely, You don’t need to log in to one of the servers.
You can run the command directory without login, but you must know the user’s name and passwords of both systems.
I want to copy the file /host1_data/file.txt from host1 to the directory /host2_data/ on the remote host2.
It will prompt to enter passwords for both remote users.
How to use SCP command in Linux Video Tutorial in Hindi
In this article, you have learned how to use scp command to copy files and directories between to hosts in the network.
If you have any question, please leave in the comment box.