How do I recursively download a directory using smbclient?

Solution 1:

Per the smbclient manpage, you need to use the mget command, with a mask and recursion and prompt set. Then cd to the directory you want to get recursively:

smbclient '\\server\share'
mask ""
recurse ON
prompt OFF
cd 'path\to\remote\dir'
lcd '~/path/to/download/to/'
mget *

Or, all on one line,

smbclient '\\server\share' -N -c 'prompt OFF;recurse ON;cd 'path\to\directory\';lcd '~/path/to/download/to/';mget *'`

If you need to authenticate to the server drop -N and use the password setting on the connect command.

http://technotize.blogspot.com/2011/12/copy-folder-with-ubuntu-smb-client.html

Solution 2:

use -D option to set the Directory

smbclient -D "\" -c ls
smbclient -D "\Path\To\Directory" -c ls

if you want to download/get the file, do

smbclient -D "\Path\To\Directory" -c "get target /tmp/target"

Solution 3:

You could also use the tar command for smbclient:

smbclient -Tc allfiles.tar /path/to/directory

This will create a tar archive allfiles.tar in the current directory the smbclient command is executed. Afterwards you can unpack the files again with tar xf allfiles.tar.