-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_size.sh
More file actions
38 lines (30 loc) · 882 Bytes
/
check_size.sh
File metadata and controls
38 lines (30 loc) · 882 Bytes
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
#!/bin/bash
# Check if directory is provided as argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory_path>"
exit 1
fi
# Store directory path
DIR="$1"
# Check if directory exists
if [ ! -d "$DIR" ]; then
echo "Error: Directory '$DIR' does not exist"
exit 1
fi
# Output file
OUTPUT_FILE="file_sizes.txt"
# Get total size of the directory
TOTAL_SIZE=$(du -sh "$DIR" | cut -f1)
# Use du to get sizes, sort numerically in reverse, and format output
# -a: all files and directories
# -h: human-readable format
# --max-depth=1: only list items in the specified directory
# sort -hr: sort human-readable numbers in reverse order
{
echo "Total Usage: $TOTAL_SIZE"
echo "--------------------------------"
du -ah --max-depth=1 "$DIR" | sort -hr
} > "$OUTPUT_FILE"
# Display contents to terminal
cat "$OUTPUT_FILE"
echo "Output written to $OUTPUT_FILE"