#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # sfs [options...] [filenames...] # # Show File Sizes, making numbers easier to read by using ANSI styles. case "$1" in -h|--h|-help|--help) awk '/^# +sfs /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = "--" ] && shift # turn arg-list into single-item lines # printf "%s\n" "$@" | # turn arg-list into single-item lines for arg in "${@:-.}"; do if [ -f "${arg}" ]; then # printf "%s\n" "$(realpath "${arg}")" printf "%s\n" "${arg}" continue fi if [ -d "${arg}" ]; then stdbuf -oL find "${arg}" -type f fi done | awk '!c[$0]++ { print; fflush() }' | # calculate file-sizes, and reverse-sort results xargs -d '\n' wc -c | sort -rn | # add/realign fields to improve legibility awk ' # start output with a header-like line, and add a MiB field BEGIN { printf "%6s %10s %8s name\n", "n", "bytes", "MiB" } # make table breathe with empty lines, so tall outputs are readable (NR - 1) % 5 == 1 { print "" } # emit regular output lines { printf "%6d %10d %8.2f ", NR - 1, $1, $1 / 1048576 # first field is likely space-padded gsub(/^ */, "") # slice line after the first field, as filepaths can have spaces $0 = substr($0, length($1) + 1) # first field is likely space-padded gsub(/^ /, "") printf "%s\n", $0 } ' | # make zeros in the MiB field stand out with a special color awk ' { gsub(/ 00*\.00* /, "\x1b[38;2;135;135;175m&\x1b[0m") print } ' | # make numbers nice, alternating styles along 3-digit groups # restyle numbers with at least 4 digits to make them easier to read; ignore # numbers in the command fields at the end of lines awk ' { s = $0 # restyle digit-runs which are longer than 3 while (match(s, /[0-9]{4,}/)) { # give up on digit-runs which are unusually long, to mitigate the # quadratic time-complexity of slicing strings in a loop if (RLENGTH > 1000) { printf "%s", substr(s, 1, RSTART + RLENGTH) s = substr(s, RSTART + RLENGTH) continue } len = RLENGTH lead = len % 3 alt = lead > 0 printf "%s", substr(s, 1, RSTART + lead - 1) s = substr(s, RSTART + lead) len -= lead while (len > 0) { if (alt == 1) { printf "\x1b[38;2;168;168;168m%s\x1b[0m", substr(s, 1, 3) } else { printf "%s", substr(s, 1, 3) } alt = 1 - alt s = substr(s, 4) len -= 3 } } printf "%s\n", s } ' | # make result interactively browsable less -MKiCRS