Transferring Files with smbclient
In this final step, we'll learn how to transfer files between our Linux system and the SMB share.
Reconnecting to the Share
Let's connect again to our SMB share:
smbclient //localhost/testshare -U labex
Enter the password when prompted:
Enter WORKGROUP\labex's password:
You should see the smbclient prompt:
Try "help" to get a list of possible commands.
smb: \>
Uploading Files to the Share
Let's upload the test file we created earlier to the SMB share. First, we need to check our current local directory:
smb: \> !pwd
This runs the pwd
command on your local system and should display:
/home/labex/project
We need to change to the directory where our test file is located:
smb: \> lcd ~/project/smb-files
Verify we're in the correct directory:
smb: \> !ls
You should see:
test.txt
Now, let's upload the file to the SMB share:
smb: \> put test.txt
You should see a message indicating the file was transferred:
putting file test.txt as \test.txt (38.5 kb/s) (average 38.5 kb/s)
Verify the file was uploaded:
smb: \> ls
You should see:
. D 0 Tue Nov 30 12:02:00 2022
.. D 0 Tue Nov 30 12:00:00 2022
shared-test.txt N 37 Tue Nov 30 12:00:00 2022
test-directory D 0 Tue Nov 30 12:01:00 2022
test.txt N 33 Tue Nov 30 12:02:00 2022
8467839 blocks of size 4096. 3524491 blocks available
Downloading Files from the Share
Now, let's download a file from the SMB share to our local system. First, let's create a new directory to store the downloaded files:
smb: \> !mkdir -p ~/project/smb-downloads
Change to that directory:
smb: \> lcd ~/project/smb-downloads
Now, download the file:
smb: \> get shared-test.txt
You should see a message indicating the file was transferred:
getting file \shared-test.txt of size 37 as shared-test.txt (37.0 KiloBytes/sec) (average 37.0 KiloBytes/sec)
Verify the file was downloaded:
smb: \> !ls
You should see:
shared-test.txt
Using mget and mput for Multiple Files
The mget
and mput
commands allow you to transfer multiple files at once. Let's try creating a few more files on our local system:
smb: \> !cd ~/project/smb-files && touch file1.txt file2.txt file3.txt
Now, let's upload all .txt files at once:
smb: \> lcd ~/project/smb-files
smb: \> mput *.txt
For each file, you'll be asked for confirmation. Type Y
to confirm each transfer:
mput test.txt? Y
putting file test.txt as \test.txt (38.5 kb/s) (average 38.5 kb/s)
mput file1.txt? Y
putting file file1.txt as \file1.txt (0.0 kb/s) (average 19.2 kb/s)
mput file2.txt? Y
putting file file2.txt as \file2.txt (0.0 kb/s) (average 12.8 kb/s)
mput file3.txt? Y
putting file file3.txt as \file3.txt (0.0 kb/s) (average 9.6 kb/s)
If you want to transfer all files without confirmation, you can turn off prompting:
smb: \> prompt
smb: \> mput *.txt
Similarly, to download multiple files:
smb: \> lcd ~/project/smb-downloads
smb: \> mget *.txt
When you're done, exit the smbclient session:
smb: \> quit
Non-Interactive Commands
You can also use smbclient
to execute commands without entering the interactive shell. For example, to list the contents of a share:
smbclient //localhost/testshare -U labex -c 'ls'
Enter the password when prompted, and you'll see the directory listing:
Enter WORKGROUP\labex's password:
. D 0 Tue Nov 30 12:03:00 2022
.. D 0 Tue Nov 30 12:00:00 2022
file1.txt N 0 Tue Nov 30 12:03:00 2022
file2.txt N 0 Tue Nov 30 12:03:00 2022
file3.txt N 0 Tue Nov 30 12:03:00 2022
shared-test.txt N 37 Tue Nov 30 12:00:00 2022
test-directory D 0 Tue Nov 30 12:01:00 2022
test.txt N 33 Tue Nov 30 12:03:00 2022
8467839 blocks of size 4096. 3524491 blocks available
To download a file in a single command:
smbclient //localhost/testshare -U labex -c 'get test.txt /home/labex/project/test-download.txt'
This command downloads the test.txt
file from the share and saves it as test-download.txt
in your project directory.
You now have a good understanding of how to use smbclient
to connect to SMB shares, navigate directories, and transfer files. These skills are essential when working in mixed Windows/Linux environments.