Last active
March 10, 2023 07:25
-
-
Save Nader-abdi/0becdbf36e4f4cc40d8deb4f1092d8df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define usage function | |
usage() { | |
echo "Usage: scanner.sh [-u] [-e excluded_dir] [-i included_ext] [-d] <directory_path>" | |
echo "" | |
echo "Options:" | |
echo " -u Do not convert file names to lowercase" | |
echo " -e excluded_dir Exclude directory from scan" | |
echo " -i included_ext Include only specified file extensions in scan" | |
echo " -d Only include directories in scan" | |
echo "" | |
echo "directory_path Directory to scan" | |
exit 1 | |
} | |
# Define color codes | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
BLUE='\033[0;34m' | |
PURPLE='\033[0;35m' | |
CYAN='\033[0;36m' | |
NC='\033[0m' # No Color | |
# Set default values | |
to_lower=true | |
exclude_dirs=(vendor node_modules .git .svn) | |
include_ext=() | |
dirs_only=true | |
# Parse command-line options | |
while getopts ":ue:i:d" opt; do | |
case ${opt} in | |
u ) | |
to_lower=false | |
;; | |
e ) | |
exclude_dirs+=("$OPTARG") | |
;; | |
i ) | |
include_ext+=("$OPTARG") | |
;; | |
d ) | |
dirs_only=false | |
;; | |
\? ) | |
usage | |
;; | |
esac | |
done | |
# Remove parsed options from argument list | |
shift $((OPTIND -1)) | |
# Check if directory path argument is provided | |
if [ $# -ne 1 ]; then | |
usage | |
fi | |
dir_path=$1 | |
# Check if directory exists | |
if [ ! -d "$dir_path" ]; then | |
echo -e "${RED}Directory '$dir_path' not found.${NC}" | |
exit 1 | |
fi | |
# List all files, directories, and subdirectories in the directory path, | |
# remove extensions from file names, and remove any duplicate names | |
echo -e "${GREEN}Scanning files and directories in '$dir_path'...${NC}" | |
while read -r name; do | |
# Check if directory should be excluded | |
for exclude_dir in "${exclude_dirs[@]}"; do | |
excluded=false | |
if [[ "$name" == *"$exclude_dir"* ]]; then | |
excluded=true | |
break | |
fi | |
done | |
if [ "$excluded" = true ]; then | |
continue | |
fi | |
if [ ${#include_ext[@]} -ne 0 ]; then | |
included=false | |
for ext in "${include_ext[@]}"; do | |
if [[ "$name" == *".$ext" ]]; then | |
included=true | |
break | |
fi | |
done | |
if [ "$included" = false ]; then | |
continue | |
fi | |
fi | |
if [ "$dirs_only" = true ] && [ ! -d "$name" ]; then | |
echo $name | |
continue | |
fi | |
if [ "$to_lower" = true ]; then | |
# Convert name to lowercase | |
lowercase=$(echo "$name" | tr '[:upper:]' '[:lower:]') | |
# Write lowercase name to output file | |
echo "${lowercase##*/}" | sed -E 's|\.[^\.]*$||' >> files_and_directories.txt | |
else | |
# Write original name to output file | |
echo "${name##*/}" | sed -E 's|\.[^\.]*$||' >> files_and_directories.txt | |
fi | |
done < <(find "$dir_path" -type f -o -type d) | |
if [ $? -ne 0 ]; then | |
echo -e "${RED}Error occurred while scanning directory '$dir_path'${NC}" | |
exit 1 | |
fi | |
# Remove duplicates from output file | |
sort -u -o files_and_directories.txt files_and_directories.txt | |
# Get line count of output file | |
line_count=$(wc -l < files_and_directories.txt) | |
echo -e "${GREEN}Files, directories, and subdirectories in '$dir_path' have been saved to files_and_directories.txt with extensions removed, duplicates removed, and names converted to lowercase (if applicable).${NC}" | |
echo -e "${GREEN}Output file contains $line_count lines.${NC}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment