Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RubybeginnercliAI generated

File Extension Report

Build a Ruby command-line tool that scans a folder and all its subfolders and reports what kinds of files it contains: how many files of each extension there are and how much space each type takes, sorted from the largest, with a total. It's handy for finding what's filling up a disk. It skips folders it can't read instead of crashing, can include or ignore hidden files, and can save the report as CSV or JSON. It's a good first Ruby project: you'll learn walking folders with Find or Dir.glob, File methods for sizes, hashes for grouping and totals, sorting, formatting output, OptionParser for options, and testing with Minitest.

Estimate
~6h
Steps
5
Completed by
0
Proposed by
codeseed.app

Ruby · OptionParser · Minitest

Project roadmap

  1. 01

    Define the report

    ~1h

    Write README.md: the command (extreport FOLDER, with --top N, --hidden, --min-size and --format table, csv or json), how files without an extension are shown, and an example report with the extension, number of files, total size in a readable unit and share of the total. Create a Gemfile with minitest. Done when: README.md shows the options and example output.

  2. 02

    Walk the folder

    ~1.5h

    Use Find.find (or Dir.glob with **) to go through all files, skipping symbolic links, hidden files and folders unless --hidden is given, and anything below --min-size. Get each file's size with File.size, and rescue permission errors so unreadable folders are counted and skipped. Done when: the total file count and size match what the file manager shows for a test folder.

  3. 03

    Group by extension

    ~1h

    Group files by their lowercase extension (File.extname, with (none) for files without one) in a Hash, adding up counts and sizes. Sort by total size, largest first, and keep the top N with an other line for the rest. Keep this logic in a method that takes a list of paths and sizes, so it can be tested without real files. Done when: the groups add up to the total number of files and bytes.

  4. 04

    Print and save the report

    ~1h

    Print an aligned table with sizes shown as B, KB, MB or GB and percentages, plus the totals and the number of skipped folders. With --format csv or json, print that format instead using the csv and json libraries. Validate options with OptionParser and show usage on errors. Done when: all three formats work, and invalid options print the usage.

  5. 05

    Test it

    ~1.5h

    Write Minitest tests using a temporary folder (Dir.mktmpdir) with files of set sizes: grouping, files without extensions, upper and lower case extensions, hidden files, the top N with other, size formatting, and each output format. Done when: rake test (or ruby -Itest) passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~6h · 5 steps

Tech stack

RubyOptionParserMinitest