filesize.bash
· 1.7 KiB · Bash
Raw
#!/usr/bin/env bash
set -euo pipefail
# defaults
sort="name"
depth=1
dirs=()
# Function to print help message
print_help() {
echo "Description:"
echo " Displays a directory tree with human-readable sizes."
echo " Under the hood it uses GNU tree with: -s --du -L --sort ."
echo ""
echo ""
echo "Usage: filesize [directory...] [options]"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -s, --sort SORT Sort by: name, version, size, mtime, ctime, none"
echo " -d, --depth N Descend only N levels deep"
echo ""
echo ""
echo "Examples:"
echo " # Show current directory, name-sorted"
echo " filesize"
echo ""
echo " # Three levels deep, size-sorted"
echo " filesize --depth 3 --sort size"
echo " filesize -d 3 -s size"
echo ""
echo " # On /var/log, two levels deep, by modification time"
echo " filesize /var/log --depth 2 --sort mtime"
echo " filesize /var/log -d 2 -s mtime"
echo ""
exit 0
}
# parse flags
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_help
;;
-s|--sort)
if [[ -z "${2-}" ]]; then
echo "Error: --sort requires an argument" >&2
exit 1
fi
sort=$2
shift
;;
-d|--depth)
if [[ -z "${2-}" ]]; then
echo "Error: --depth requires an argument" >&2
exit 1
fi
depth=$2
shift
;;
--*)
echo "Unknown option: $1" >&2
exit 1
;;
-*)
echo "Unknown short option: $1" >&2
exit 1
;;
*)
dirs+=("$1")
;;
esac
shift
done
# if no dirs specified, use current
if [[ ${#dirs[@]} -eq 0 ]]; then
dirs=(.)
fi
# run
exec tree -sh --du -L "$depth" --sort "$sort" "${dirs[@]}"
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | # defaults |
| 5 | sort="name" |
| 6 | depth=1 |
| 7 | dirs=() |
| 8 | |
| 9 | # Function to print help message |
| 10 | print_help() { |
| 11 | echo "Description:" |
| 12 | echo " Displays a directory tree with human-readable sizes." |
| 13 | echo " Under the hood it uses GNU tree with: -s --du -L --sort ." |
| 14 | echo "" |
| 15 | echo "" |
| 16 | echo "Usage: filesize [directory...] [options]" |
| 17 | echo "" |
| 18 | echo "Options:" |
| 19 | echo " -h, --help Show this help message and exit" |
| 20 | echo " -s, --sort SORT Sort by: name, version, size, mtime, ctime, none" |
| 21 | echo " -d, --depth N Descend only N levels deep" |
| 22 | echo "" |
| 23 | echo "" |
| 24 | echo "Examples:" |
| 25 | echo " # Show current directory, name-sorted" |
| 26 | echo " filesize" |
| 27 | echo "" |
| 28 | echo " # Three levels deep, size-sorted" |
| 29 | echo " filesize --depth 3 --sort size" |
| 30 | echo " filesize -d 3 -s size" |
| 31 | echo "" |
| 32 | echo " # On /var/log, two levels deep, by modification time" |
| 33 | echo " filesize /var/log --depth 2 --sort mtime" |
| 34 | echo " filesize /var/log -d 2 -s mtime" |
| 35 | echo "" |
| 36 | exit 0 |
| 37 | } |
| 38 | |
| 39 | # parse flags |
| 40 | while [[ $# -gt 0 ]]; do |
| 41 | case $1 in |
| 42 | -h|--help) |
| 43 | print_help |
| 44 | ;; |
| 45 | -s|--sort) |
| 46 | if [[ -z "${2-}" ]]; then |
| 47 | echo "Error: --sort requires an argument" >&2 |
| 48 | exit 1 |
| 49 | fi |
| 50 | sort=$2 |
| 51 | shift |
| 52 | ;; |
| 53 | -d|--depth) |
| 54 | if [[ -z "${2-}" ]]; then |
| 55 | echo "Error: --depth requires an argument" >&2 |
| 56 | exit 1 |
| 57 | fi |
| 58 | depth=$2 |
| 59 | shift |
| 60 | ;; |
| 61 | --*) |
| 62 | echo "Unknown option: $1" >&2 |
| 63 | exit 1 |
| 64 | ;; |
| 65 | -*) |
| 66 | echo "Unknown short option: $1" >&2 |
| 67 | exit 1 |
| 68 | ;; |
| 69 | *) |
| 70 | dirs+=("$1") |
| 71 | ;; |
| 72 | esac |
| 73 | shift |
| 74 | done |
| 75 | |
| 76 | # if no dirs specified, use current |
| 77 | if [[ ${#dirs[@]} -eq 0 ]]; then |
| 78 | dirs=(.) |
| 79 | fi |
| 80 | |
| 81 | # run |
| 82 | exec tree -sh --du -L "$depth" --sort "$sort" "${dirs[@]}" |
| 83 |