Copy files from one machine/server to another in Linux


The best way to copy file/folders from one server to another using SSH is scp command.

scp stands for Secure copy

  • How to use in command line?
scp <source path> <destination path>
  • source path : from where/what files to copy.
  • destination path : to which folder.

Examples for the user of the scp command are as follow,

Assume that your source machine IP address is 192.168.AAA.AAA and destination IP is 192.168.BBB.BBB


  • Logged in to Source Machine

So in this case you need to copy files from 192.168.AAA.AAA -> 192.168.BBB.BBB. Your logged in to 192.168.AAA.AAA

Files to copy are located in /home/files.tar of 192.168.AAA.AAA

scp /home/files.tar root@192.168.BBB.BBB:/home/

The above command will ask for the destination machine password of the root user [you can use any valid user credentials other than root]


  • Logged in to Destination Machine

So in this case you need to copy files from 192.168.AAA.AAA -> 192.168.BBB.BBB. Your logged in to 192.168.BBB.BBB

Files to copy are located in /home/files.tar of 192.168.AAA.AAA

scp root@192.168.AAA.AAA:/home/files.tar /home/

The above command will ask for the source machine’s password of the root user , as I ‘m using root as the user in above command line.


  • Copy files to the directory you are currently in

So in this case you need to copy files from 192.168.AAA.AAA -> 192.168.BBB.BBB. Your logged in to 192.168.BBB.BBB and your in pwd(present working directory) is /home/user/

Files to copy are located in /home/files.tar of 192.168.AAA.AAA

scp root@192.168.AAA.AAA:/home/files.tar .

  • Copy files with port number

Yes, you can specify the port number for coping the files using scp. By default scp uses port 22.

scp -P 2435 root@192.168.AAA.AAA:/home/files.tar /home/

  • Copy files inside directory recursively

Case when we need to files/folders in directory and all inside directories and files

scp -r root@192.168.AAA.AAA:/home/files.tar /home/

  • Copy multiple file

You can specify the files to copy . in the below example file.tar and file2.tar in /home directory.

 scp /home/files.tar /home/files2.tar root@192.168.BBB.BBB:/home/

  • Retain file attributes while copying

This will help you to retain the attributes of the file .Ex- modified date,user,file permissions etc…

scp -p root@192.168.AAA.AAA:/home/files.tar /home/

Leave a Reply

Your email address will not be published. Required fields are marked *