smb2-cli
A command-line SMB2/3 client. It talks to a share directly over the network, so you never mount it: no root, no mount_smbfs, no kernel client. One binary and a target like //host/share/path.
Built on the smb2 crate, which does all the protocol work. Both live in the
same repo, so a protocol fix and the CLI change that uses it ship together.
Why
A mounted share is fine until you have thousands of small metadata operations. macOS smbfs does one synchronous round trip per syscall with no overlap, so a bulk delete crawls. smb2-cli opens N independent connections and spreads the batch across them.
Measured against a Raspberry Pi over LAN, 500-path batches, each arm getting its own disjoint slice:
| Operation | Mounted share | smb2 at -j 16 |
|---|---|---|
| Delete files | 34/s | 500/s |
| Remove empty directories | 3.3/s | 155/s |
For reference, one SSH process doing plain local syscalls on the Pi managed 556/s on the deletes. So the CLI beats going through the mount, and it beats shelling into the box. On a longer run (39,176 files) it sustained 877/s.
-j 16 was the sweet spot on that four-core Pi. -j 32 was slower, so don't crank it past what the server can serve.
benchmarks/cli-bulk-ops/bulk-bench.sh in the repo reproduces all of this.
Every command also prints JSON, which makes it pleasant to drive from a script or an agent.
Install
No Rust toolchain needed. On macOS or Linux, with Homebrew:
Or with the install script (macOS and Linux):
|
On Windows (PowerShell):
powershell -ExecutionPolicy Bypass -c "irm https://github.com/vdavid/smb2/releases/latest/download/smb2-cli-installer.ps1 | iex"
Prebuilt binaries for each platform are on GitHub Releases. If you have Rust, cargo install smb2-cli works too.
The binary is called smb2.
Credentials
In order of preference:
--password-commandruns a command and reads its stdout:--password-command 'secret RASPI_SMB_PASSWORD'. Keeps the password out of your shell history and out ofps.SMB2_PASS, usually from a.envfile or a secrets manager.- A prompt, when you're on a terminal and gave none of the above.
There's also --password, but your shell will remember it and so will the process list. Avoid it.
--user (or SMB2_USER) sets the username; --guest connects with no credentials.
--require-encryption encrypts the whole session even when the server only allows it, and refuses to connect when it can't encrypt (the server agreed on no cipher, or answered with a guest session) rather than sending your files in the clear.
Commands
# Look around
# Move bytes
# Change things
# Extended attributes and named streams
xattr works like macOS's own command and stores attributes the way macOS's SMB client does, so a Finder tag is the same bytes under the same name either way. -w never creates a missing file. -d on an attribute that isn't there succeeds. It takes -j and --from-file like the other batch commands, and --json reports values as valueHex.
ls prints as the server answers, a batch of a few hundred entries at a time, in the order the server lists them. So a folder of 100,000 files starts showing up after one round trip, and smb2 ls … | head stops listing as soon as head has enough. Pass --sort to get the rows sorted by path instead; that waits for the whole listing first. --json is always sorted, since a JSON array is only useful once it's complete. -R lists each directory to the end before going into its subdirectories. Measured against a QNAP NAS on a LAN, ls -R over 15,250 photos printed its first line after 0.15 s instead of 7.6 s, and finished in the same time as before.
ls -i puts each entry's index number in front of it: the file system's ID for the file, like an inode number, which stays put when the file is renamed. With --json it's indexNumber, or null when the server has none for that entry.
stat shows four times: created, modified, accessed, and changed. changed moves on any change to the file, its attributes and timestamps included, so it catches an edit modified doesn't. ls --json carries changed too.
realpath prints the path the way the server stores it: its casing, and long names in place of 8.3 aliases like PROGRA~1. It needs SMB 3.1.1 (Samba, or Windows 10 / Server v1803 and later) and says so when the server can't answer.
mv won't replace a file that's already at the destination unless you pass --replace. With it, the server swaps the file in one step, so the destination is never missing in between. It never replaces a directory.
mkdir -p is happy to find the directory already there, and stops when the name belongs to a file, the way GNU mkdir -p does.
put picks its destination the way cp does. A target that ends in /, or that's already a directory on the share, gets the file inside it under its own name; anything else is the full path to write. An upload never replaces a directory: if the destination is one, put says so and stops.
Transfers stream in both directions, so size isn't a limit and neither is your memory: a 600 MB get runs in about 60 MB of RAM, and put is the same the other way.
get, cat, and put ride out a dropped connection. They reconnect and carry on: a download from the last byte it wrote, an upload from its last checkpoint (one every 64 MiB). A download refuses to carry on if the file changed on the share meanwhile, so it never stitches two versions into one. After five drops in one transfer, the command gives up.
Neither direction leaves a half-written file under the real name. get writes to name.smb2-part next to the destination and put does the same on the share, and each renames the finished file over the destination in one step. If a transfer fails, the file that was already there stays exactly as it was.
shares lists the disk shares you'd see browsing the server. -a adds the hidden ones, whose names end in $ (C$, ADMIN$, or a share someone hid on purpose). IPC$ and printers never show up.
Names that can't print as they are get two escapes, the way Rust spells them: \\ is a backslash, and \u{D800} is a character no terminal can show (a Windows share can hold a name with half of an emoji's UTF-16 pair, which isn't valid text anywhere else). ls, stat, and every message print names this way, and every target and --from-file line reads them back, so a name copied from ls is an argument naming the same file. A backslash that starts neither escape is refused. get saves such a file under the same escaped name, since a Mac or Linux disk can't hold the original. --json carries the names unescaped, and the Windows target form (\\host\share\path) takes plain names only, since its backslashes are separators.
--json works on every command. --dry-run works on everything that writes (mkdir, rm, rmdir, mv, put, get, and xattr -w / -d): it prints what would happen and makes no connection at all.
Batches
Every path-taking command accepts --from-file, where each line is a path relative to the target you give. Use - to read from stdin. mv --from-file takes from<TAB>to pairs.
# Delete 5,000 empty directories over 16 connections
# Rename in bulk
|
-j sets how many connections to spread the work over (default 8). It goes after the subcommand, and only the commands that actually spread work take it: stat, mkdir, rm, rmdir, and mv. Everything in one batch has to live on the same host and share.
Exit codes
0 when everything worked, 1 when anything failed.
A batch doesn't abandon the run on the first bad path. It processes the rest, reports the per-path failures on stderr, and exits non-zero. So you always find out, and you never have to guess how far it got.
License
MIT or Apache-2.0, your choice.