mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 05:40:11 -08:00
2.1 KiB
2.1 KiB
Folder Metrics Script Documentation
This document provides an overview and step-by-step explanation of the folder-metrics.sh script. This script calculates disk usage and file count for a directory and its subdirectories.
Script Overview
The script performs the following main tasks:
- Checks if a directory is provided as an argument.
- Calculates the disk usage of the directory.
- Iterates over each subdirectory to calculate disk usage and file count.
- Outputs the results in a tabular format.
Detailed Steps
1. Check if a Directory is Provided
if [ -z "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
This block checks if a directory is provided as an argument. If not, it prints a usage message and exits.
2. Calculate Disk Usage of the Directory
echo "Disk usage for directory: $1"
echo "--------------------------"
du -sh "$1"
This block calculates and prints the disk usage of the provided directory.
3. Iterate Over Each Subdirectory
echo
echo "Usage for each subdirectory"
echo "--------------------------"
for dir in "$1"/*; do
if [ -d "$dir" ]; then
space_used=$(du -sh "$dir" | cut -f1)
file_count=$(find "$dir" -type f | wc -l)
printf "%-30s %-10s %10s files\n" "$(basename "$dir")" "$space_used" "$file_count"
fi
done
This block iterates over each subdirectory, calculates the disk usage and file count, and prints the results in a tabular format.
Usage
To use the script, run it with the directory path as an argument:
./folder-metrics.sh <directory>
<directory>: The path to the directory for which you want to calculate disk usage and file count.
Important Information
- Ensure that the script is executable. You can make it executable with the following command:
chmod +x folder-metrics.sh - The script requires a directory path as an argument. Ensure that you provide a valid directory path when running the script.
By following this documentation, you should be able to understand and use the folder-metrics.sh script effectively.