Last active 1745503491

biscuide's Avatar biscuide revised this gist 1745503490. Go to revision

1 file changed, 82 insertions

filesize.bash(file created)

@@ -0,0 +1,82 @@
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[@]}"
Newer Older