SCP Command in Linux with Examples
SCP Command
SCP stands for secure copy. This command is helpful in transferring files from remote host to the local system or from local host to remote system. It follows the SSH server for authentication and encrypted data transfer.
Files from local to remote
Here the text file file1.txt from user1 is copied remotely to user user2.
Example
[user1@linuxhelp ~]$ scp /home/user1/Desktop/file1.txt user2@192.168.7.13:/home/user2/Desktop/
user2@192.168.7.13' s password:
file1.txt 100% 0 0.0KB/s 00:00
This method secure to transfer the file from local to client, the output shows first they ask the password for the remote user. The entered password was wrong, then the file transfer will be terminated.
Multiple files from local to remote
SCP can be used to transfer multiple files at a time to another location either in local or remote.
Example
[user1@linuxhelp Desktop]$ scp file1.txt file2.txt file3.txt user2@192.168.7.13:/home/user2/Desktop/
user2@192.168.7.13' s password:
file1.txt 100% 0 0.0KB/s 00:00
file2.txt 100% 0 0.0KB/s 00:00
file3.txt 100% 0 0.0KB/s 00:00
Files from local to remote with port number
While copying the files from local to remote there may be some block in remote such as port numbers. To copy with port number -P option is used.
Example
[user1@linuxhelp ~]$ scp -P 24865 /home/user1/Desktop/file1.txt user2@192.168.7.13:/home/user2/Desktop/
user2@192.168.7.13' s password:
file1.txt 100% 0 0.0KB/s 00:00
If the port number is wrong or it’ s not mentioned then it returns the connection refused message.
Directories from local to remote
To copy the directory dir1 from local user1 to remote user2. Directories cannot be sent simply as files it has to be provided with -r option.
Example
[user1@linuxhelp~]$ scp -r -P 24865 dir1 user2@192.168.7.13: /home/user2/Desktop/
user2@192.168.7.13' s password:
dir-file1 100% 6KB 6.9KB/s 00:00
dir-file2 100% 3KB 3.0KB/s 00:00
Thus the directory with that of the contents is transferred to the desired location.
Copy with detailed information
While copying from local to remote or to local itself it can be done with step by step detailed information about the transferring files. For that -v option is used.
Example
[user1@linuxhelp~]$ scp -v -P 24865 file2.txt user2@192.168.7.13:/home/user2/Documents/
Executing: program /usr/bin/ssh
host 192.168.7.13, user user3, command scp -v -t /home/user3/Desktop/
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.7.13 [192.168.7.13] port 22.
debug1: connect to address 192.168.7.13 port 22: Connection refused
.
.
Transferred: sent 1736, received 2144 bytes, in 0.3 seconds
Bytes per second: sent 5940.0, received 7336.0
debug1: Exit status 1
Hiding the transfer details
Use -q option to hide all the status about file transfer in the terminal. It will disable all status about file transfer.
Example
[user1@linuxhelp~]$ scp -q -P 24865 file1.txt user2@192.165.7.13:/home/user2/Desktop/
user2@192.168.7.13' s password:
Displaying the transfer details
Using -p options it will display the status of the file transfer details and also it will display the file name, file transferred rate in percentage, size of file, file transfer rate and file transfer time.
Example
[user1@linuxhelp~]$ scp -p -P24865 file2.txt user2@192.165.7.13:/home/user2/Downloads/
user2@192.168.7.13' s password:
file2.txt 100% 16 0.0KB/s 00:00
-p option to be used to display transfer details should be in lowercase since -P is used for specifying the port numbers.
Transfer the files faster
Use option -C to speed up the transfer to save time and bandwidth. The transfer speed is depending on how many files could be compressed.
Example
[user1@linuxhelp~]$ scp -Cpv -P24865 test-dir user2@192.168.7.13:/home/user2/Desktop/
It will not works on all files, if the source file is already compressed means it will not works. File types like .zip, .rar, pictures .iso will not be affected by -C option.
Limits the bandwidth usage
One of the important option in scp is limiting the bandwidth when copying files with using option ' -l ' . The bandwidth is specified in Kbit/sec, it is nothing but file transfer speed per second.
Example
[user1@linuxhelp~]$ scp -P24865 -rl 600 new-file.txt user2@192.168.7.13:/home/user2/Documents/
e.g. If you want to transfer your file in 25 kB/sec you should mention 200 .The 200 is nothing but 25 * 8 = 200 (8 bit equal to one byte)
Here, I have used 600, so the new-file.txt file transfer rate is 75 Kb/sec.
To copy from remote to local transfer
The following command will copy the file file3.txt from remote user to local user at desktop location.
Example
[root@linuxhelp~]# scp file4.txt user2@192.168.7.13:/home/user2/Desktop/
user1@192.168.5.13' s password:
The same procedure to be followed for transferring directories from one location another location.
Multiple files from remote to local
The following command will copy the multiple files file4.txt and file5.txt from remote user to local user user2 at desktop location.
Example
[user1@linuxhelp Desktop]$ scp file4.txt file5.txt user2@192.168.7.13:/home/user2/Desktop/
user2@192.168.7.13' s password:
file4.txt 100% 18 0.0KB/s 00:00
file5.txt 100% 0 0.0KB/s 00:00
Transfer from remote to remote
SCP helps in transferring files or directories from one remote host to another host.
Example
[root@linuxhelp Desktop]# scp user1@192.168.7.251:/home/user1/Desktop/file5.txt user2@192.165.7.13:/home/user2/Desktop/
Here, the file5.txt is transferred from 192.168.7.251 host to 192.168.7.13
Comments ( 0 )
No comments available