How to Copy All Files From a Remote Directory
This article explains methods to copy files between different hosts on a network. Furthermore, it explores the scp
command and how it is used to copy files from a remote location.
the scp
Command
Secure copy protocol (SCP) is a network protocol that securely copies that facilitates the smooth copying of files and folders between Unix-compliant operating systems.
The scp
command is a much safer variant of the cp
command. It encrypts the passwords and the files during remote copying over a secure shell (SSH) connection, thereby securing the communication from sniffing attacks.
For more information on scp
, use the command below.
man scp
The man pages (short form of Manual Pages) is a Unix utility that displays information on system calls, library calls, formal standards and conventions, and even abstract concepts. As the man pages are downloaded alongside the kernel, they require no internet connection.
Copy Files in Bash
There are several ways to copy files in Bash, cp
, cat
, vim
(any text editor), and scp
are just some of the many ways we can do so. The code below explores ways to copy files in Bash.
1) cp <source file>.txt <destination file>.txt
2) cat 0<source_file.txt 1>destination_file.txt
3) cat <source_file.txt >destination_file.txt
The first method copies the contents from the source file and then pastes them into the destination file. It is important to note that cp
will paste contents to the destination file even if it doesn’t exist.
It does this as the kernel will automatically create the destination file.
The second method uses concepts you would have studied in your Operating Systems course. Recall the ppfdt
(process file descriptor table), the file descriptor 0
points to the keyboard (stdin
), the file descriptor 1
points to the monitor (stdout
), and the file descriptor 2
points to the monitor (stderror
).
This second method overrides the descriptor 0
to point to the source file. It also overrides the stdout
to point to the destination file.
Method three is similar to the second method except that in this method, descriptor 3
points to the source file, while descriptor 4
points to the destination file.
In both methods 2 and 3, reading is done from the source file, and writing is done to the destination file. It is, again, essential to note that the kernel will paste contents to the destination file even if it doesn’t exist.
This is because the kernel will automatically create the destination file.
Copy Files Securely in Bash Across Remote Locations
The general syntax for the scp
command is below.
scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:destination/path]
If we don’t provide any user_name
, the scp
uses the current user as a default. If you exclude the target path, the scp
command considers copying from the source to the current local path.
Similarly, omitting the source path will cause the scp
command to use the current local path as a source for copying the files.
In the option, if you specify -r
, it recursively copies all files and subdirectories. So if you want to copy files, don’t specify any option. It will copy the files but will exclude the sub-directories.
The benefit of scp
over other commands is that it’s much more secure as it encrypts the files.