51

When I try to fetch a directory with get "Path To\Directory\", I get the following error:

NT_STATUS_FILE_IS_A_DIRECTORY opening remote file Path To\Directory

How do I recursively download this directory?

(Using smbclient v3.6.23. The server is a computer running Windows 7 Home Edition.)

hololeap
  • 1,321
  • 1
  • 9
  • 14
  • smbclient uses the same type of semantics that server clients like FTP and HTTP do, where each get or put targets one file. you can write scripts to perform retrievals by directory, or you can use the mget/mput commands to specify a mask or wildcard to retrieve multiple files, as shown in my answer. it may be that smbclient isn't quite the right tool for your purposes. – Frank Thomas Dec 25 '14 at 04:23

4 Answers4

93

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

galoget
  • 393
  • 2
  • 10
Frank Thomas
  • 35,097
  • 3
  • 77
  • 98
  • 3
    Also, I think you've got your quotes a bit confused in the one-liner. My `smbclient` only seems to like dealing with directories in "double quotes". – c24w Jun 07 '17 at 14:32
  • Just copied and replaced folders but it didnt work - ends with trailing > – Wax Cage Aug 07 '17 at 21:40
  • For people really want to copy without problems follow this article: https://indradjy.wordpress.com/2010/04/14/getting-whole-folder-using-smbclient/ (helped me) – Wax Cage Aug 07 '17 at 21:46
  • 1
    Is it possible to skip already existing files? This question was asked here and I need its answer, too: https://unix.stackexchange.com/questions/551104/only-mget-new-files-with-smbclient – mgutt Apr 21 '20 at 23:10
  • 2
    @mgutt, consider using rsync on top of SMB, once you have the smbshare mounted. SMB has no semantics for for uni/bi directional sync by itself, but tools like rsync (linux) and robocopy (windows) can provide very robust sync functionality on an existing mount point. Also note that SMB does introduce some idiosyncracies related to file locking, so you may need to shoot for eventual concurrence, rather than single point in time concurrency. – Frank Thomas Apr 22 '20 at 03:02
  • @mgutt, you can make the comnmand ask you for input on each file if it exists, but setting `PROMPT ON`, and you can include files by specifying a MASK if you like. more than that will require additional applications to provide additional functionality. – Frank Thomas Apr 22 '20 at 03:28
  • 1
    Worked perfectly replacing the internal single quotes with double quotes. – Julio Batista Silva Jul 15 '20 at 15:43
2

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.

m13r
  • 121
  • 2
1

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"
aGuegu
  • 119
  • 2
  • The question is not about how to download particular files from a directory on a share, but how to download *all* files of that directory recursively. – stackprotector May 25 '23 at 12:28
1

In addition to one-liner above, I found a solution for Teamcity with hidden password prompt (build step written as a command, TC version 2020.1.2):

smbclient '%smbPath%' '%smbPassword%' -W %domain% -U %smbUser% -c 'prompt OFF; recurse OFF; cd %smbSource%; lcd tomcat/conf; mget *'

where

%smbPath%     = \\smbserver\share
%smbPassword% = domain user's password, use single quotes
%domain%      = domain
%smbUser%     = username
%smbSource%   = subdirectory(-ies) inside samba share (i.e. -D option)
recurse OFF   = in my case I don' want to recurse subfolders
tomcat/conf   = destination path relative to %system.teamcity.build.tempDir% (i.e. working directory) 

It will download all files from samba directory to working directory on build agent. The most problem here is the proper quotation of a password. The password containing @, # should be written separately from username. Escaping mode %smbUser%%%%smbPassword% may not work.

Rapekas
  • 131
  • 5