Skip to content

Instantly share code, notes, and snippets.

@LuisPaGarcia
Created May 24, 2023 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuisPaGarcia/73c595cc8dae074df50a405b280c49a1 to your computer and use it in GitHub Desktop.
Save LuisPaGarcia/73c595cc8dae074df50a405b280c49a1 to your computer and use it in GitHub Desktop.
Sort by LOC
#!/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