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
What is SCP in Linux?
scp stands for secure copy, it is 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.
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:
$scp [OPTION] [user@]SRC_HOST:]file1_Path [user@]DEST_HOST:]file2
- OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy ..etc
- [user@SRC_HOST:]file1_Path – Source file with path
- [user@DEST_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.
vijay@Ubuntu-19:~$scp usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target vijay@Ubuntu-19:~$
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@example.com:/remote/directory
- scp – Command
- file.txt – It is the source file and path what I want to copy.
- remote_username@IP address – identification of remote hosts.
- /remote/directory path on a remote system where you want to copy.
See the following example:
root@Ubuntu-19:~# scp newfile.txt firstname.lastname@example.org:/home/vijay/ email@example.com's password: newfile.txt 100% 0 0.0KB/s 00:00 root@Ubuntu-19:~#
In another example, I am trying to copy a directory but Getting error “not a regular file”
root@Ubuntu-19:~# scp mydata/ firstname.lastname@example.org:/home/vijay/ email@example.com's password: mydata: not a regular file root@Ubuntu-19:~#
If you want to copy a directory then you can use scp command followed by -r option. See Example below.
root@Ubuntu-19:~#scp -r mydata/ firstname.lastname@example.org:/home/vijay/ email@example.com's password: root@Ubuntu-19:~#
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/ firstname.lastname@example.org:/remote/directory/new_name
root@Ubuntu-19:~#scp -r mydata/ email@example.com:/home/vijay/remote_mydata firstname.lastname@example.org's password: root@Ubuntu-19:~#
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@example.com:/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 firstname.lastname@example.org:/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.
$scp email@example.com:/host1_data/file.txt firstname.lastname@example.org:/host2_data
It will prompt to enter passwords for both remote users.
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.