Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active January 6, 2025 10:21
Show Gist options
  • Save vijinho/a8e2838b5408548ba03c to your computer and use it in GitHub Desktop.
Save vijinho/a8e2838b5408548ba03c to your computer and use it in GitHub Desktop.
List the file extensions found for a given directory tree, default current directory
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 [-d | --directory] <directory>"
echo " -d, --directory Specify the directory to search (default: current directory)"
exit 1
}
# Default directory is the current directory
DIR="."
# Parse command-line arguments
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-d | --directory )
shift; DIR="$1"
;;
*)
usage
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
# Check if directory exists
if [[ ! -d "$DIR" ]]; then
echo "Error: Directory '$DIR' does not exist."
usage
fi
# Find file extensions used in the specified directory and its subdirectories
find "$DIR" -type f | awk -F. '{print $NF}' | sort -u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment