Skip to content

Instantly share code, notes, and snippets.

@dbarjs
Last active May 18, 2023 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarjs/b620ac18f274606d3496df002d8a2a4d to your computer and use it in GitHub Desktop.
Save dbarjs/b620ac18f274606d3496df002d8a2a4d to your computer and use it in GitHub Desktop.
SH - Directory Listing with Sizes
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment