278

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.

3
  • 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. Sep 28, 2012 at 4:50
  • 2
    Wow, sftp is basically useless as is. lftp helps a lot. Dec 10, 2019 at 14:40
  • sftp supports now recursive mode: get -R [directory]
    – AndreyP
    Feb 8, 2023 at 11:39

11 Answers 11

412

Use the -R (recursive) flag:

get -R .
12
  • 27
    Thank you for putting an answer that actually answers the question.
    – aVeRTRAC
    Jan 19, 2012 at 19:56
  • 32
    Gives me: get: Invalid flag -r
    – ghbarratt
    Jul 18, 2012 at 21:21
  • 3
    This also gives me Invalid flag. I'm not sure this is supported on all systems.
    – Ben
    Aug 28, 2012 at 7:35
  • 15
    @silvinci not all current systems. Illegal option with CentOS 6.4 openssh-clients-5.3p1-84.1.el6.x86_64 Nov 5, 2013 at 1:25
  • 1
    For some reason uploading recursively does not work with put -r * it can't create directories. However for me the lftp solution below worked just fine.
    – Ciantic
    Dec 22, 2014 at 8:24
64

Use:

scp -r mpirocch@my-server:/home/mpirocch/Documents Documents
10
  • 5
    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, 2009 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. Sep 12, 2009 at 23:53
  • 1
    scp -r will follow symlinks...
    – jsleuth
    Feb 13, 2013 at 15:57
  • 11
    Some systems may allow sftp, but not scp access.
    – Turion
    Aug 4, 2017 at 9:41
  • 3
    This doesn't answer the question. Dec 10, 2019 at 14:20
50

Use lftp:

lftp sftp://user@host

Then, within lftp, cd into the directory you want to copy, and use the mirror command to recursively download the selected directory, like this:

mirror

This command accepts options and arguments:

mirror [OPTIONS] [source [target]]

For example, the -R (or --reverse) option will cause it to upload the local directory tree to the remote directory:

mirror -R

See the lftp(1) man page at the project’s site or at Debian.org for other commands and options.

1
  • 6
    +1 lftp is like a little bundle of magic and joy!
    – Ibby
    Apr 27, 2011 at 13:32
27

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

lftp sftp://user:[email protected]: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

3
  • Thanks, this worked for me, after the other methods failed (in particular no "-r" option on sftp). The first part of the command in my case needed to be quoted, due to dollar characters in the password lftp 'sftp://user:[email protected]:22' -e 'mirror --verbose --use-pget-n=8 -c /remote/path /local/path'
    – Jason
    Nov 9, 2014 at 22:57
  • Excellent, thanks. I wanted to copy from local to server, so I used mirror -R ... Dec 10, 2019 at 14:29
  • If you get mirror: Fatal error: Host key verification failed., the host key fingerprint probably still needs to be accepted (i.e. use ssh / sftp to make an initial connection, and confirm the fingerprint with yes)
    – mwfearnley
    Jan 20, 2021 at 10:25
16

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.

2
  • 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, 2009 at 14:13
  • 1
    I've had performance issue with Nautilus SFTP. Yeah, it's really convenient but I wasn't able to saturate 1 Gbit/s network via Nautilus, while with plain sftp/ssh command line interface I was (cca 100 MB/s file transfers). So the issue was Nautilus on GNU/Linux Mint.
    – stamster
    Jan 25, 2018 at 22:15
15
get -r [directory]

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

4
  • 2
    IMO this is the best answer to this question. :)
    – ramrunner
    Jan 24, 2013 at 18:28
  • 6
    This is a duplicate of an answer from Nov 29 '10 (almost one year and a half ago). Jan 4, 2014 at 19:43
  • 1
    @CristianCiupitu Not really. The answer you refer to recommends get -r * which will recursively get everything at the current directory level. My suggestion gets just a specific directory, which is what the OP requested. So I would argue that my answer is more accurate than the accepted answer, and I'll have my upvotes back, thankyou! ;)
    – drkvogel
    Jul 20, 2022 at 11:36
  • Worked as a charm!
    – ADV-IT
    Apr 3, 2023 at 22:40
14

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.

3
  • 1
    mget doesn't work any better than get for me.
    – Matthew
    Sep 12, 2009 at 22:53
  • 2
    getting Cannot download non-regular file with this
    – Dchris
    Aug 24, 2015 at 9:04
  • 2
    This worked for me on a system where 'get -r' didn't work.
    – Tchotchke
    Sep 20, 2016 at 19:47
2

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.

3
  • 1
    This is basically a duplicate of an answer from Nov 29 '10 (almost two years ago). Jan 4, 2014 at 19:47
  • This is especially useless if only sftp is allowed. Dec 10, 2019 at 14:19
  • Also non-useful when you don't have shell access to the remote node.
    – Chiwda
    Aug 21, 2023 at 4:26
0

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
0

I recently solved this with lftp:

lftp -c '
open sftp://USER:[email protected]:22
mirror --verbose --use-pget-n=8 -c /remote/catalogue/ /local/catalogue/
'

Inspired by this post.

0

sftp -r user@myserver:/some_remote_directory /tmp/localdir
(it's important to specify the destination directory, otherwise it will just log in)

... worked for me (on Ubuntu 23)

or interactively:

sftp user@myserver
cd some_remote_directory
mget *

(mget stands for "multiple get" and the *-wildcard to get all files)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .