Save the script to a file, such as directory_list_sizes.sh, and make it executable:
chmod +x directory_list_sizes.sh
To use the script, provide the target directory as an argument when running it:
./directory_list_sizes.sh /path/to/directory
#!/bin/bash | |
# Check if a directory is provided as an argument | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 directory_path" | |
exit 1 | |
fi | |
# Get the directory path from the command line argument | |
dir_path=$1 | |
# Check if the directory exists | |
if [ ! -d "$dir_path" ]; then | |
echo "Directory not found: $dir_path" | |
exit 1 | |
fi | |
# List top-level directories and their sizes in megabytes without root directory, sorted by size (descending) | |
cd "$dir_path" || exit | |
du -sh */ | sort -hr | awk -F'\t' '{print $2 "\t" $1}' | column -t |
Save the script to a file, such as directory_list_sizes.sh, and make it executable:
chmod +x directory_list_sizes.sh
To use the script, provide the target directory as an argument when running it:
./directory_list_sizes.sh /path/to/directory