Our task is to learn how to copy files between computers joined into a single network and running operating systems of the Unix family, whether Linux (Debian or CentOS, Ubuntu or RedHat) or BSD (for example, FreeBSD).
For copying files between computers in a Unix environment, one of the most popular utilities is SCP. It not only allows simply copying files, but also uses the SSH protocol for this, protecting the transmitted information with encryption. Moreover, this doesn't noticeably affect the copying speed itself.
You don't need to download the utility - it comes "out of the box" in OSes such as Linux Debian, Ubuntu, CentOS, RedHat and FreeBSD.
Now let's show a few basic examples of using this utility, based on which you can easily learn to use SCP.
It's worth noting right away - the SCP utility uses SSH to access the remote computer, so the following conditions must be met:
- There must be an account on the remote computer that you will use for copying
- This account must have a working command-line shell (bash/sh; nologin or false won't work)
- This account must have write permission to the directory you are copying the file into
Copying a file to a remote computer
$ scp mylocalfile username@192.168.0.1:/home/username/mydestfile
Here:
- mylocalfile: the name and path of the file on this computer that you want to copy
- username: the username on the remote computer
- 192.168.0.1: the IP address or hostname of the remote computer
- /home/username/mydestfile: the path on the remote server and the file name to which the local file will be copied. If a file name is not specified, the file will be copied to the specified path with its local path/name
Copying a file from a remote computer
$ scp username@192.168.0.1:/home/username/mydestfile ./mylocalfile
As you can see, it's the reverse situation. The parameters are the same.
Copying several files to a remote computer
$ scp myfile1 myfile2 myfile3 username@192.168.0.1:/home/username/
As you can see - here the files to be copied are listed first, and the last parameter is the destination.
Recursively copying directories with all their contents
$ scp /my/path/sourcedir/ username@192.168.0.1:/home/username/destdir/
In this example, the entire sourcedir folder will be copied and placed inside the destdir folder on the remote computer. I.e., on the remote computer, inside the destdir folder, a sourcedir folder with all its files will appear.
Based on these examples, it's easy to understand how to use the SCP utility to copy files and entire directories between computers over a network.
Applies to: Any Unix (Linux Debian/CentOS, FreeBSD)
Comments