Created
May 24, 2023 16:33
-
-
Save LuisPaGarcia/73c595cc8dae074df50a405b280c49a1 to your computer and use it in GitHub Desktop.
Sort by LOC
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 | |
# Function to count lines of code in a file | |
count_lines_of_code() { | |
local file=$1 | |
local lines=$(wc -l < "$file") | |
echo "$lines" | |
} | |
# Function to sort files based on lines of code | |
sort_files() { | |
local directory=$1 | |
local file_list=$(find "$directory" -type f) # Get a list of all files in the directory | |
# Iterate over each file and count lines of code | |
local file | |
for file in $file_list; do | |
local lines=$(count_lines_of_code "$file") | |
echo "$lines $file" | |
done | sort -nr # Sort by lines of code in descending order | |
} | |
# Prompt the user for a directory path | |
read -p "Enter the directory path: " directory_path | |
# Verify if the directory exists | |
if [[ ! -d "$directory_path" ]]; then | |
echo "Error: Directory not found." | |
exit 1 | |
fi | |
# Call the sort_files function with the specified directory | |
sorted_files=$(sort_files "$directory_path") | |
# Display the sorted file list | |
echo "Sorted files based on lines of code:" | |
echo "$sorted_files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment