Skip to content

Instantly share code, notes, and snippets.

@jriano
Last active August 29, 2015 14:02
Show Gist options
  • Save jriano/aea33eb366ee2a57aa37 to your computer and use it in GitHub Desktop.
Save jriano/aea33eb366ee2a57aa37 to your computer and use it in GitHub Desktop.
#!/bin/bash
# By Juan Riaño
# juanriano.com
# Feel free to use as you please, a thank you card (or email) will be appreciated,
# but don't hold me responsible for whacking your grandma's pc.
# This script standirizes file names, it is meant mostly for mp3s or pictures, just
# copy this script inside the folder where the files are located and run it.
# The folder should only have the files you want to standireze. The script does:
# - change file name to lowercase
# - removes non ascii characters
# - replaces spaces by _
# This script is not meant to be short or fast, but rather easy to read and change.
file_to_lower() {
local FILENAME # file name without the extension
local FILEEXT # just the file's extension
for file in *; do
FILENAME="$(echo "${file%.*}" | tr '[:upper:]' '[:lower:]')"
FILEEXT="$(echo "${file##*.}" | tr '[:upper:]' '[:lower:]')"
if [ $FILEEXT != 'sh' ]; then
if [ "$file" != "$FILENAME.$FILEEXT" ]; then
mv "$file" "$FILENAME.$FILEEXT"
fi
echo "Changed extension to lower case: '$FILENAME'.'$FILEEXT'"
fi
done
}
remove_non_ascii() {
local FILENAME # whole name, including extension
local FILEEXT # just the file's extension
for file in *; do
FILENAME="$(echo "$file" | tr -cd "a-zA-Z0-9._- ñáéíóú")"
FILEEXT="${file##*.}"
if [ $FILEEXT != 'sh' ]; then
if [ "$file" != "$FILENAME" ]; then
mv "$file" "$FILENAME"
fi
echo "Removed non ascii chars for: '$FILENAME'"
fi
done
}
replace_spaces() {
# replaces spaces and '-'' with '_'
local FILENAME # whole filename
local FILEEXT # just the file's extension
for file in *; do
FILENAME="$(echo "$file" | tr -s ' ' '_')"
FILENAME=$(echo "$FILENAME" | tr -s '-' '_')
FILENAME=$(echo "$FILENAME" | tr -s '_' '_')
FILEEXT="${file##*.}"
if [ $FILEEXT != 'sh' ]; then
if [ "$file" != "$FILENAME" ]; then
mv "$file" "$FILENAME"
fi
echo "Removed spaces, new file name: $FILENAME"
fi
done
}
file_to_lower
remove_non_ascii
replace_spaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment