How to download a file from the command line? via ssh?

21

7

I want to download files from a remote server to my local drive, and do it from the command line. I also want to be able to do this over SSH. How can I do this?

Note: the remote server is Ubuntu, the local is Mac OS X

Andrew

Posted 2010-01-11T21:53:14.377

Reputation: 11 982

Answers

29

Use scp-command, it runs on top of SSH. Example:

scp username@remote.host:/path/to/file localfile

It also works another way round

scp localfile username@host:/path/remotefile

Username, path, and filename can be omitted (but not the : !).

As Iain said, SFTP works also, but I tend to favor scp for its cp-like usage.

Ahe

Posted 2010-01-11T21:53:14.377

Reputation: 969

I recommend adding the -p option when copying files or folders with scp. It copies over the file attributes too (timestamps and flags). I find I want -p more often than I don't want it! – joeytwiddle – 2014-08-08T22:43:04.453

You can also remote-to-remote routed through the localhost using the -3 flag: scp -3 jeff@firsthost.com:/files/file1.zip brad@secondhost.com:/archives This is useful if you need to use a private key for access to both servers only found on localhost: scp -3i /local/path/to/.ssh/private_key dan@host1:/path/to/file.txt miri@host2:/path/to/upload/dir/ The progress bar is disabled for -3 – Dan Sandland – 2015-09-23T22:32:07.990

On mac terminal to linux box, my local path is being interpreted as a server path. scp java-named-objects.yml ~/Documents/sap Getting cp: cannot create regular file/ngs/app/gsmt/Documents/sap': No such file or directory` – johny why – 2016-05-01T13:35:43.323

Less common I'm sure, and correct me if I'm wrong, but I think scp also works remote-to-remote, if you really need to: scp username@remote1:/path/to/file username@remote2:/path/to/file – JMD – 2010-01-11T22:06:17.923

can you copy directories? – Andrew – 2010-01-11T22:10:18.773

figured it out... -r recursively copies directories too – Andrew – 2010-01-11T22:15:53.053

3

I use SFTP for this. It's command line and uses the same security as SSH.

Iain

Posted 2010-01-11T21:53:14.377

Reputation: 4 399

3

You can also use rsync for it. It can work over SSH.

osgx

Posted 2010-01-11T21:53:14.377

Reputation: 5 419

rsync -avvP is my favourite for files and/or folders, but there is one drawback: it needs to be installed on the remote machine. – joeytwiddle – 2014-08-08T22:44:57.647

If you are having trouble connecting, you may need to pass -e ssh to tell rsync to connect over ssh. – joeytwiddle – 2014-08-08T22:47:10.007

1

If you can't use scp or SFTP you can use tar over SSH:

tar cf - . | ssh otherhost "cd /mydir; tar xvf -"

This one is also good if you have sparse files which otherwise will "explode".

Jimmy Hedman

Posted 2010-01-11T21:53:14.377

Reputation: 886