mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 06:40:13 -08:00
76 lines
2.1 KiB
Markdown
76 lines
2.1 KiB
Markdown
# 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:
|
|
|
|
1. Checks if a directory is provided as an argument.
|
|
2. Calculates the disk usage of the directory.
|
|
3. Iterates over each subdirectory to calculate disk usage and file count.
|
|
4. Outputs the results in a tabular format.
|
|
|
|
## Detailed Steps
|
|
|
|
### 1. Check if a Directory is Provided
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
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:
|
|
|
|
```shell
|
|
./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:
|
|
|
|
```shell
|
|
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.
|