Skip to content

Instantly share code, notes, and snippets.

@signalnine
Created January 6, 2023 19:04
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 signalnine/b86012e2d52fbde02e919cbd5c40f6a1 to your computer and use it in GitHub Desktop.
Save signalnine/b86012e2d52fbde02e919cbd5c40f6a1 to your computer and use it in GitHub Desktop.
title case file rename
#!/bin/bash
# Set the list of small words to ignore
ignore_list="a an and but or nor the"
# Loop through all files in the current directory
for file in *
do
# Split the file name and extension into separate variables
filename="${file%.*}"
extension="${file##*.}"
# Split the file name into an array of words
words=($(echo "$filename" | tr '-' ' '))
# Initialize the new file name
new_name=""
# Loop through each word in the file name
for word in "${words[@]}"
do
# Check if the word is in the ignore list
if [[ " $ignore_list " =~ " $word " ]]; then
# If the word is in the ignore list, just add it to the new file name as-is
new_name+="$word "
else
# If the word is not in the ignore list, convert it to title case and add it to the new file name
new_name+="$(echo "$word" | gsed -e 's/\b\(.\)/\u\1/g') "
fi
done
# Trim the extra space from the end of the new file name
new_name="${new_name% }"
# Rebuild the file name with the new file name and the original extension
new_file="$new_name.$extension"
# Rename the file
mv "$file" "$new_file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment