Skip to content

Instantly share code, notes, and snippets.

@stefanlasiewski
Last active January 7, 2026 01:43
Show Gist options
  • Select an option

  • Save stefanlasiewski/bf2e9c33460808ab3a05298d0ba9ad4e to your computer and use it in GitHub Desktop.

Select an option

Save stefanlasiewski/bf2e9c33460808ab3a05298d0ba9ad4e to your computer and use it in GitHub Desktop.
ducks: Summarize disk space usage into a local file called .ducks. Works on MacOS, FreeBSD & Linux (GNU Tools)
#!/usr/bin/env bash
# Find the disk hog
# Borrowed from http://oreilly.com/pub/h/15
#set -x
set -euo pipefail
# Summarize disk space usage into a local file called .ducks
# du options
# -x File system mount points are not traversed.
# .[^.]* Include dotfiles, exclude '.' & '..'. Note that '--exclude' is not supported on a Mac
# $* Allow extra grep options, like '-n'
# 2>/dev/null -- To avoid dumb errors with stuff that doesn't matter like /proc (Linux) or $USER/Library (MacOS)
# Sort options
# --human-numeric-sort: compare human readable #s (2K 1G). Doesn't work on all OSes.
# MacOS finally supports -h (human-readable) so no need for GNU & non-GNU versions
# nullglob: expand unmatched patterns to nothing instead of literal text
# dotglob: make * pattern include dotfiles (files starting with .)
shopt -s nullglob dotglob
# Build array of files to check (dotfiles and regular files)
files=(.[^.]* *)
du -chs -x $* "${files[@]}" 2>/dev/null | sort -h > .ducks ; tail .ducks
# NonGNU fallback (if sort -h not supported on FreeBSD/macOS)
#du -cks -x $* "${files[@]}" 2>/dev/null | sort -n > .ducks ; tail .ducks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment