Directory Size Tree
Build a Rust command-line tool that shows what's taking up space on your disk: it prints a folder's contents as a tree, with each folder's total size, sorted so the biggest items come first, like the tree and du commands combined. You can limit how deep the tree goes, hide small items, and get the result as JSON for other tools. It's a good first Rust project: you'll learn structs and enums, recursion, working with the file system through std::fs, handling errors with Result and the ? operator, parsing command-line options with clap, and producing JSON with Serde.
- Estimate
- ~8.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
Cargo · clap · Serde
Project roadmap
- 01
Define the command
~1hCreate a project with cargo new dirtree and add clap (with the derive feature), serde and serde_json. Write README.md with example output: a tree with sizes like 1.2 GB, options for the path (default: the current folder), --depth, --min-size, --json and --all (include hidden files). Define the options as a struct with #[derive(Parser)]. Done when: dirtree --help shows every option.
- 02
Build the tree
~2hDefine a Node struct (name, size in bytes, is it a folder, and children). Write a recursive function that reads a folder with std::fs::read_dir, adds up the sizes of files and subfolders, and returns a Node. Use symlink_metadata and don't follow symbolic links, to avoid loops. Return errors with Result and ?. Done when: the total size of a test folder matches du -sb (or the sum of its files).
- 03
Print the tree
~1.5hPrint the tree with branch characters (├──, └── and │), each line showing a human-readable size (B, KB, MB, GB) and the name, with folders sorted by size, biggest first. Stop printing below --depth, while still counting the sizes of everything inside. Hide items smaller than --min-size and show a line such as … 12 smaller items. Done when: the output looks like a clean tree with correct sizes.
- 04
Handle errors and edge cases
~1.5hWhen a folder or file can't be read (for example because of permissions), skip it, mark it in the tree, and list the errors at the end instead of stopping. Handle a path that doesn't exist or is a file, empty folders, and names that aren't valid UTF-8 (print them with to_string_lossy). Done when: scanning a folder with unreadable subfolders finishes and reports them.
- 05
Add JSON output
~1hDerive Serialize for Node, and with --json print the tree as JSON with serde_json instead of text, still respecting --depth and --min-size. Done when: dirtree --json produces valid JSON that can be read by jq.
- 06
Test it
~1.5hWrite tests that create a temporary folder structure (with the tempfile crate) with known file sizes, then check the total sizes, the sorting, the depth limit, the minimum size filter, and the JSON output. Run cargo clippy and fix its warnings. Done when: cargo test passes, and clippy is clean.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack