Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm just learning to use sftp, and I want to copy a directory from the remote computer to my local computer. If I try

get [directory]

it gives me this error:

Cannot download non-regular file: /home/mpirocch/Documents

get -R doesn't work, either.

share|improve this question
2  
Recursive put/get was added to OpenSSH in 5.4, which was released in 2010. You may consider upgrading your servers as it also adds a lot of bug fixes and better security features. –  Lèse majesté Sep 28 '12 at 4:50

10 Answers 10

up vote 88 down vote accepted

Use the -r (recursive) flag:

get -r *
share|improve this answer
12  
Thank you for putting an answer that actually answers the question. –  jonnyflash Jan 19 '12 at 19:56
12  
Gives me: get: Invalid flag -r –  ghbarratt Jul 18 '12 at 21:21
    
This also gives me Invalid flag. I'm not sure this is supported on all systems. –  Ben Aug 28 '12 at 7:35
    
@Ben: When I asked this question in 2009, the -r flag did not exist. Is your system up to date? –  Matthew Dec 10 '12 at 16:24
8  
@silvinci not all current systems. Illegal option with CentOS 6.4 openssh-clients-5.3p1-84.1.el6.x86_64 –  CrackerJack9 Nov 5 '13 at 1:25

Use:

scp -r mpirocch@my-server:/home/mpirocch/Documents Documents
share|improve this answer
    
This works, but it would be nice to be able to do this from SFTP (so I can see what I copy before I copy it). Is this possible? –  Matthew Sep 12 '09 at 22:53
    
@Matthew: I don't think so. I have been asking myself the same question for some time and the best answer that I have for the moment is scp -r. Or you could use an advanced SFTP client like Filezilla. –  Cristian Ciupitu Sep 12 '09 at 23:53
1  
scp -r will follow symlinks... –  jsleuth Feb 13 '13 at 15:57

use lftp like:

lftp sftp://user@host

and then use the mirror command to recursively download (mirror -R to recursively upload).

share|improve this answer
1  
+1 lftp is like a little bundle of magic and joy! –  WCWedin Apr 27 '11 at 13:32

Don't use the sftp program directly if you can find something better. For Linux, many file managers (at least Nautilus and Dolphin, the GNOME and KDE ones) support sftp natively, and there's always sshfs. For windows, there's WinSCP, and probably others. The point of all of these is to let you access files over sftp as if they were on a regular filesytem, so you don't have to care that you're accessing them over sftp.

share|improve this answer
    
Wow, nautilus does work very well with SFTP. I added a bookmark in Nautilus, and now it's all ridiculously easy. Thanks! –  Matthew Sep 13 '09 at 14:13

Try mget instead of get.

Clarification: mget will work if you are inside the directory you want to copy; if you do something like this:

sftp> cd dir_to_get
sftp> mget *

it will get all the files in that directory. However, it will not recursively get the contents of any subdirectories.

share|improve this answer
    
mget doesn't work any better than get for me. –  Matthew Sep 12 '09 at 22:53
get -r [directory]

gets [directory] and everything under it, where r stands for recursive. I found this just by typing help from sftp.

share|improve this answer
1  
IMO this is the best answer to this question. :) –  ramrunner Jan 24 '13 at 18:28
    
This is a duplicate of an answer from Nov 29 '10 (almost one year and a half ago). –  Cristian Ciupitu Jan 4 at 19:43

As with cp:

scp -rp user@host:/path/to/dir dir

The above will preserve times and modes of the original files and subdirectories. This is especially useful for the retrieval of backups.

share|improve this answer
    
This is basically a duplicate of an answer from Nov 29 '10 (almost two years ago). –  Cristian Ciupitu Jan 4 at 19:47

well this little guide should help, mirror a remote server to local folder with lftp

lftp sftp://user:password@server.org:22 -e 'mirror --verbose --use-pget-n=8 -c /remote/path /local/path'

  • sftp:// = uses SFTP protocol
  • mirror = mirror mode
  • verbose = shows the files being downloaded
  • use-pget-n = number of segments, realy useful to speed up big files
  • parallel = downloads multiplier files at the same time

if you want to download files in parallel switch out use-pget-n=8 with --parallel=8

hope this helps anyone needing to mirror a remote folder to a local folder

share|improve this answer

I have Java dist folder in remote server, where i have following tree:

- dist
--- Audio.jar
--- README
--- lib
----- lib.jar

Goal is: I want to use SFTP? And put them in /tmp/<>

Step 1. sftp remoteuser@ip

Step 2. cd /var/tmp

Step 2. lmkdir /tmp/dist; lmkdir /tmp/dist/lib

Step 3. lcd /tmp/dist

Step 4. mget *

Step 5. lcd /tmp/dist/lib

Step 6. mget *

Step 7. finally i have my goal

$ ls
Audio.jar  lib  README.TXT
share|improve this answer

FileZilla is the best client I have used, which of course can use SFTP underneath:

The GUI make is easy to copy over a whole directory.

share|improve this answer
1  
That's great when you want to download something to your workstation/PC (personally I use WinSCP). But when you want to do direct server-to-server transfers (e.g. migrate a site from one web host to another), using a GUI app isn't really practical. You COULD install X and a remote desktop, but that's a bit overkill for a web server. –  Lèse majesté Sep 28 '12 at 4:40

protected by studiohack Mar 14 '12 at 21:33

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.