File: sfs.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # sfs [options...] [filenames...]
  27 #
  28 # Show File Sizes, using my other scripts `nn` and `cext`.
  29 
  30 
  31 case "$1" in
  32     -h|--h|-help|--help)
  33         awk '/^# +sfs /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  34         exit 0
  35     ;;
  36 esac
  37 
  38 # turn arg-list into single-item lines
  39 printf "%s\n" "$@" |
  40 # calculate file-sizes, and reverse-sort results
  41 xargs -d '\n' wc -c | sort -rn |
  42 # add/realign fields to improve legibility
  43 awk '
  44     # start output with a header-like line, and add a MiB field
  45     BEGIN { printf "%6s  %10s  %8s  name\n", "n", "bytes", "MiB"; fflush() }
  46     # make table breathe with empty lines, so tall outputs are readable
  47     (NR - 1) % 5 == 1 && NR > 1 { print "" }
  48     # emit regular output lines
  49     {
  50         printf "%6d  %10d  %8.2f  ", NR - 1, $1, $1 / 1048576
  51         # first field is likely space-padded
  52         gsub(/^ */, "")
  53         # slice line after the first field, as filepaths can have spaces
  54         $0 = substr($0, length($1) + 1)
  55         # first field is likely space-padded
  56         gsub(/^ /, "")
  57         printf "%s\n", $0; fflush()
  58     }
  59 ' |
  60 # make zeros in the MiB field stand out with a special color
  61 awk '
  62     {
  63         gsub(/ 00*\.00* /, "\x1b[38;2;135;135;175m&\x1b[0m")
  64         print; fflush()
  65     }
  66 ' |
  67 # make numbers nice, alternating styles along 3-digit groups
  68 nn --gray |
  69 # color-code file extensions
  70 cext |
  71 # make result interactively browsable
  72 less -JMKiCRS