Huge Discounts on Mobiles, Books, Cameras, Computers etc: @Flipkart
Flipkart.com

Friday, November 16, 2007

How to copy directories remotely in Linux

FTP/SFTP has commands like get and mget (put/mput) but they work only on a file or multiple files. Both commands cannot handle a directory. So how do you remotely copy a directory in linux, especially say between two Linux machines?

SCP - Secure Copy Protocol
To use the scp command to copy files between systems, use the following command:

scp filename1 userid@hostname:filename2

where filename1 is the file on the local system that you wish to copy, userid@hostname is the userid and hostname where you wish to copy it, and filename2 is the name you want to call the file on the remote system. For example:

scp /home/file1 root@mysystem.com:/root/file1

When you issue the command, you'll be prompted for the password on the remote system. You will then be given the stats of the transfer. Pay attention to the second item on the stat line; it's how much of the file got transferred (eg: 100%).

Note: SCP may also be used to copy files from a remote system to a local system. To do this in the first example above, reverse the order of filename1 and userid@hostname:filename2.

scp root@mysystem.com:/root/file1 /home/file1


To use the scp command to copy directories between systems, use the following command:

scp -r directoryname userid@hostname:directoryname2

where directoryname is the directory on the local system you wish to copy, userid@hostname is the userid and hostname where you wish to copy it, and directoryname2 is the name you want to call the file on the remote system. For example:

scp -r /home/ root@mysystem.com:/root/dir1

Note: SCP may also be used to copy directories from a remote system to a local system. To do this in the first example above, reverse the order of directoryname and userid@hostname:directoryname2.

scp -r root@mysystem.com:/root/dir1 /home/

2 comments:

  1. Thank god for this command. I was using FTP to copy whole domains from an old server to a new server when the old server crashed and I had so many timeouts and errors it wasn't funny. On top of that, it was slow as hell. SCP copies at about 5-10MB/s which is screaming fast compared to FTP. Thanks a million!!!

    ReplyDelete
  2. Thanks for the comment Brad, glad I could save your day ;) I discover things like this every day as part of increasing efficiency and smart work, wish I had enough time to share all the tweaks and tricks I find :)

    ReplyDelete