mirror of
https://github.com/acedanger/shell.git
synced 2025-12-05 21:40:12 -08:00
refactor: replace folder_metrics.sh with folder-metrics.sh for improved output formatting
This commit is contained in:
28
folder-metrics.sh
Executable file
28
folder-metrics.sh
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Check if a directory is provided as an argument
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
echo "Usage: $0 <directory>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Calculate the disk usage of the directory and its children
|
||||||
|
echo "Disk usage for directory: $1"
|
||||||
|
echo "--------------------------"
|
||||||
|
du -sh "$1"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "Usage for each subdirectory"
|
||||||
|
echo "--------------------------"
|
||||||
|
|
||||||
|
# Iterate over each subdirectory
|
||||||
|
for dir in "$1"/*; do
|
||||||
|
if [ -d "$dir" ]; then
|
||||||
|
# Get the disk usage of the subdirectory
|
||||||
|
space_used=$(du -sh "$dir" | cut -f1)
|
||||||
|
# Count the number of files in the subdirectory
|
||||||
|
file_count=$(find "$dir" -type f | wc -l)
|
||||||
|
# Output the subdirectory name, space used, and file count in tabular format
|
||||||
|
printf "%-30s %-10s %10s files\n" "$(basename "$dir")" "$space_used" "$file_count"
|
||||||
|
fi
|
||||||
|
done
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Check if a directory is provided as an argument
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
echo "Usage: $0 <directory>"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Calculate the disk usage of the directory and its children
|
|
||||||
echo "Disk usage for directory: $1"
|
|
||||||
echo "---------------------------------"
|
|
||||||
du -sh "$1"
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
echo "Subdirectories and their usage:"
|
|
||||||
echo "---------------------------------"
|
|
||||||
du -sh "$1"/*
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
echo "Number of files in each subdirectory:"
|
|
||||||
echo "---------------------------------"
|
|
||||||
for dir in "$1"/*; do
|
|
||||||
if [ -d "$dir" ]; then
|
|
||||||
file_count=$(find "$dir" -type f | wc -l)
|
|
||||||
echo "$dir: $file_count files"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
Reference in New Issue
Block a user