The command to transfer files between Linux servers typically has two options: sftp/ftp and scp. Both can accomplish file uploads and downloads. However, if not scripted and used as pure commands, sftp and ftp require at least two commands to execute operations like cd or cp after logging in. In contrast, scp can complete file uploads and downloads with a single command.
For an introduction to the options of scp, refer to man,
scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2
Explanation of option parameters,
-1: Forces the scp command to use protocol ssh1 -2: Forces the scp command to use protocol ssh2 -4: Forces the scp command to use only IPv4 addressing -6: Forces the scp command to use only IPv6 addressing -B: Use batch mode (does not ask for passwords or phrases during transfer) -C: Allows compression (passes the -C flag to ssh to enable compression) -p: Preserves original file modification time, access time, and permissions. -q: Does not display the transfer progress bar. -r: Recursively copies entire directories. -v: Verbosely displays output. scp and ssh(1) will show debugging information for the entire process, which is useful for debugging connection, authentication, and configuration issues. -c cipher: Encrypts the data transfer using cipher, this option is directly passed to ssh. -F ssh_config: Specifies an alternative ssh configuration file, this parameter is directly passed to ssh. -i identity_file: Reads the key file used for transfer from the specified file, this parameter is directly passed to ssh. -l limit: Limits the bandwidth that the user can use, in Kbit/s. -o ssh_option: If you prefer to use parameters from ssh_config(5), -P port: Note that P is uppercase, port specifies the port number used for data transfer -S program: Specifies the program used for encrypted transfer. This program must understand the options for ssh(1).
The scp abbreviation is as follows,
scp [optional_parameters] file_source file_target
By using one machine, you can simulate file uploads and downloads. For demonstration convenience, I created two directories on one server: one called local representing the local directory and another called remote representing the remote directory. The local directory contains two files, 1.txt and 2.txt.
P.S. scp can not only upload but also download, which is why the abbreviation above uses file_source for the source file and file_target for the target file, rather than file_local for the local file and file_remote for the remote file.
Scenario 1: Uploading Files from Local to Remote Server
(1) Upload the local file 1.txt to the path /home/oracle/test/remote of user oracle at 10.221.0.1, keeping the filename as 1.txt,
[test@app local]$ scp 1.txt [email protected]:/home/oracle/test/remote/
(2) If the command does not include a username, it defaults to the current executing user’s name on the remote server. Thus, the file is uploaded to the user test’s path on server 10.221.0.1 as shown below,
[test@app local]$ scp 1.txt 10.221.0.1:/home/oracle/test/remote/
(3) If you specify the filename for the remote path, the uploaded file will be renamed. For example, uploading the local 1.txt to the remote server, changing the filename to a.txt,
[oracle@app local]$ scp 1.txt [email protected]:/home/oracle/test/remote/a.txt[test@app local]$ scp 1.txt 10.221.0.1:/home/oracle/test/remote/a.txt
Scenario 2: Uploading a Folder from Local to Remote Server
At this point, you can use the -r option to recursively copy the folder without changing its name,
[oracle@app test]$ scp -r local [email protected]:/home/oracle/test/remote/[oracle@app test]$ scp -r local 10.221.0.1:/home/oracle/test/remote/
To change the folder’s name,
[oracle@app test]$ scp -r local [email protected]:/home/oracle/test/remote/test[oracle@app test]$ scp -r local 10.221.0.1:/home/oracle/test/remote/test
Scenario 3: Downloading Files/Folders from Remote Server
(1) Downloading a file,
[oracle@app local]$ scp [email protected]:/home/oracle/test/remote/1.txt 1.txt[oracle@app local]$ scp 10.221.0.1:/home/oracle/test/remote/1.txt 1.txt
(2) Downloading a folder,
[oracle@app local]$ scp -r [email protected]:/home/oracle/test/remote/test test[oracle@app local]$ scp -r 10.221.0.1:/home/oracle/test/remote/test test
Scenario 4: Uploading Multiple Files from Local to Remote Server
Input multiple filenames,
[oracle@app local]$ scp 1.txt 2.txt [email protected]:/home/oracle/test/remote[oracle@app local]$ scp 1.txt 2.txt 10.221.0.1:/home/oracle/test/remote
Scenario 5: Downloading Multiple Files from Remote Server
Referring to Scenario 4, one might write it like this, which indeed works but requires entering the username for each file (of course, setting up SSH trust would simplify this),
[oracle@app local]$ scp [email protected]:/home/oracle/test/remote/1.txt [email protected]:/home/oracle/test/remote/2.txt [email protected]'s password: 1.txt 100% 0 0.0KB/s 00:00 [email protected]'s password: 2.txt 100% 0 0.0KB/s 00:00
You can also use wildcards, but special characters need to be handled, making it simpler with a single operation,
[oracle@app local]$ scp [email protected]:/home/oracle/test/remote/{1.txt,2.txt} [email protected]'s password: 1.txt 100% 0 0.0KB/s 00:00 2.txt 100% 0 0.0KB/s 00:00
References,
https://blog.csdn.net/life_journey/article/details/100916011
https://www.runoob.com/linux/linux-comm-scp.html
Recently Updated Articles:
“How to Build a Tech Team That Can Bring Down a Company?”
“Rules for IP Address Resolution”
“MySQL’s Skip-Grant-Tables”
“An Extraordinary Year for Domestic Databases”
“Oracle’s Requirement for Ordered Top Data Retrieval”
“Several Technical Issues Encountered in Daily Work”
“Understanding sqlhc”
“Introduction to Oracle’s MD5 Function”
“Silent Installation of Oracle 19c Examples”
“Solutions for Slow sqlplus Logins”
“Pitfalls Encountered During VMWare 11 Installation of RedHat Linux 7”
“Same COST Value? True or False?”
“Silent Installation of Oracle 11g Examples”
“Clarifying Same Name Synonyms and Views”
“Some Mysteries of v$ and v_$”
Article Classification and Index:
“Classification and Index of 700 Articles from the Public Account”