This repository was archived by the owner on Sep 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory
More file actions
executable file
·179 lines (169 loc) · 4.42 KB
/
Copy pathinventory
File metadata and controls
executable file
·179 lines (169 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
###################################
# inventory #
# github.com/teccdev/inventory #
###################################
MODE="none"
RECURSIVE=false
TARGET="."
NUMBER_FORMAT="auto"
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
echo "Usage: inventory <mode> [options] [target]"
echo "Modes:"
echo " -c, --count Count files in the target directory"
echo " -s, --size Show size of the target file or directory"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " -r, --recursive Process directories recursively (for count mode)"
echo " -#, --#-decimals Number of decimal places to show (e.g., '-2' for 2 decimal places)"
echo " -K, --kb Show size in kilobytes (for size mode)"
echo " -M, --mb Show size in megabytes (for size mode)"
echo " -G, --gb Show size in gigabytes (for size mode)"
echo
echo "Examples:"
echo " inventory -c /path/to/dir Count files in directory"
echo " inventory -s -r /path/to/dir Show recursive size of directory"
echo " inventory -s -2 file.txt Show file size with 2 decimal places"
echo " inventory -s -M file.txt Show file size in megabytes"
exit 0
;;
-c | --count)
MODE="count"
shift
;;
-s | --size)
MODE="size"
shift
;;
-r | --recursive)
RECURSIVE=true
shift
;;
-[0-9] | --[0-9]-decimals)
DECIMALS=${1//[!0-9]/}
if ! [[ $DECIMALS =~ ^[0-9]+$ ]]; then
echo "inventory: invalid number of decimal places"
exit 1
fi
shift
;;
-K | --kb)
NUMBER_FORMAT="KB"
shift
;;
-M | --mb)
NUMBER_FORMAT="MB"
shift
;;
-G | --gb)
NUMBER_FORMAT="GB"
shift
;;
-B | --bytes)
NUMBER_FORMAT="bytes"
shift
;;
-*)
echo "inventory: $1 is not a valid option"
exit 1
;;
*)
if [ -e "$1" ]; then
TARGET="$1"
else
echo "inventory: $1 is not a file or directory"
exit 1
fi
shift
;;
esac
done
if [ $MODE = "count" ]; then
if [ -d "$TARGET" ]; then
if [ $RECURSIVE = true ]; then
COUNT=$(find "$TARGET" -type f | wc -l)
else
COUNT=$(find "$TARGET" -maxdepth 1 -type f | wc -l)
fi
echo "inventory: there are $COUNT files in $TARGET"
elif [ -f "$TARGET" ]; then
echo "inventory: $TARGET is not a directory"
fi
elif [ $MODE = "size" ]; then
# Function to format size with specified unit
format_size() {
local size=$1
local unit=${2:-auto}
local result=""
local decimals=${DECIMALS:-2}
# Handle bytes specifically if explicitly requested
if [[ "$unit" == "bytes" ]]; then
echo "$size bytes"
return
fi
# Handle auto-detection if no specific unit is requested
if [[ "$unit" == "auto" ]]; then
if ((size < 1000)); then
echo "$size bytes"
return
elif ((size < 1000000)); then
unit="KB"
elif ((size < 1000000000)); then
unit="MB"
else
unit="GB"
fi
fi
# Convert based on unit
case $unit in
KB)
result=$(awk -v size="$size" -v dec="$decimals" 'BEGIN { printf "%0." dec "f", size/1000 }')
unit="KB"
;;
MB)
result=$(awk -v size="$size" -v dec="$decimals" 'BEGIN { printf "%0." dec "f", size/1000000 }')
unit="MB"
;;
GB)
result=$(awk -v size="$size" -v dec="$decimals" 'BEGIN { printf "%0." dec "f", size/1000000000 }')
unit="GB"
;;
*)
echo "inventory: invalid unit format"
exit 1
;;
esac
# Clean up trailing .00 if decimals is 0
if [[ -n "$decimals" && $decimals -eq 0 ]]; then
result=${result%.*}
fi
echo "$result $unit"
}
if [ -d "$TARGET" ]; then
if [ $RECURSIVE = true ]; then
FILE_SIZE=$(find "$TARGET" -type f -exec stat -c%s {} + | awk '{sum+=$1} END {print sum}')
FILES=$(find "$TARGET" -type f | wc -l)
FORMATTED_SIZE=$(format_size "$FILE_SIZE" "$NUMBER_FORMAT")
echo "inventory: $TARGET is $FORMATTED_SIZE across $FILES files"
else
FILE_SIZE=$(find "$TARGET" -maxdepth 1 -type f -exec stat -c%s {} + | awk '{sum+=$1} END {print sum}')
FILES=$(find "$TARGET" -maxdepth 1 -type f | wc -l)
FORMATTED_SIZE=$(format_size "$FILE_SIZE" "$NUMBER_FORMAT")
echo "inventory: $TARGET is $FORMATTED_SIZE across $FILES files"
fi
else
if [ $RECURSIVE = true ]; then
echo "inventory: $TARGET is not a directory"
exit 1
else
FILE_SIZE=$(stat -c%s "$TARGET")
FORMATTED_SIZE=$(format_size "$FILE_SIZE" "$NUMBER_FORMAT")
echo "inventory: $TARGET is $FORMATTED_SIZE"
fi
fi
else
echo "inventory: no mode specified (use --help for a list of options)"
fi