Building a du-style directory size tool in Rust with clap, walkdir and humansize

This post builds a small command-line tool that walks a directory tree, adds up the size of every file in it, and prints the total in human-readable units, roughly like du -sh --apparent-size. It uses three crates:
clapto parse command-line arguments,walkdirto traverse the directory tree,humansizeto format byte counts.
Set up the project
Section titled “Set up the project”cargo new dirsizecd dirsizeAdd the dependencies to Cargo.toml (all under a single [dependencies] table):
[dependencies]clap = { version = "4.6", features = ["derive"] }walkdir = "2.5"humansize = "2.1"These are the current releases at the time of writing (clap 4.6.7, walkdir 2.5.0, humansize 2.1.3).
The code
Section titled “The code”Put this in src/main.rs:
use std::path::{Path, PathBuf};
use clap::Parser;use humansize::{format_size, BINARY};use walkdir::WalkDir;
/// Print the total size of the files under a directory.#[derive(Parser)]struct Cli { /// Directory to measure path: PathBuf,
/// How many directory levels to descend (default: unlimited) #[arg(short, long)] depth: Option<usize>,}
fn measure_dir(path: &Path, depth: Option<usize>) -> u64 { WalkDir::new(path) .max_depth(depth.unwrap_or(usize::MAX)) .into_iter() .filter_map(|entry| entry.ok()) // skip entries we can't read .filter(|entry| entry.file_type().is_file()) .filter_map(|entry| entry.metadata().ok()) .map(|metadata| metadata.len()) .sum()}
fn main() { let args = Cli::parse(); let total = measure_dir(&args.path, args.depth); println!("{}\t{}", format_size(total, BINARY), args.path.display());}How it fits together:
#[derive(Parser)]generates the argument parser from the struct. The field without an attribute becomes a required positional argument;#[arg(short, long)]creates-d/--depth. The doc comments become the--helptext.WalkDir::new(path)yields every entry underpath.max_depth(0)means only the starting path itself,max_depth(1)adds its direct children, and so on.- Entries that error (for example, permission denied) are skipped by
filter_map(|entry| entry.ok()). Directories are filtered out because their own metadata size isn’t the size of their contents. format_size(total, BINARY)formats with powers of 1024 and IEC units (KiB,MiB). UseDECIMALfor powers of 1000 (MB,GB).
Run it
Section titled “Run it”cargo run -- ~/projectscargo run -- ~/projects --depth 1cargo run -- --helpThe first command prints something like 1.42 GiB /home/you/projects. With --depth 1, only files directly inside the directory are counted.
How it differs from du
Section titled “How it differs from du”- Apparent size vs disk usage.
metadata.len()is the file’s length in bytes.duby default reports allocated disk blocks, which is larger for many small files and smaller for sparse files. That’s why this tool matchesdu --apparent-sizerather than plaindu. - Symlinks.
walkdirdoesn’t follow symbolic links by default, and a symlink’sfile_type()isn’t a file, so links aren’t counted. Call.follow_links(true)if you want to follow them (walkdir detects loops). - Hard links are counted once per path, so a file with two hard links is counted twice.
ducounts it once. - Errors are silent. A real tool should report skipped entries to stderr instead of dropping them.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)