41 lines
1.8 KiB
Bash
41 lines
1.8 KiB
Bash
#!/bin/bash
|
|
|
|
# this script cleans up the names of files and directories in the current directory.
|
|
# it ensures that names only contain letters, numbers, or the characters "-", "_", and ".".
|
|
# spaces and accented characters are replaced with allowed characters.
|
|
|
|
# usage:
|
|
# 1. save this script to a file, e.g., rename_files.sh
|
|
# 2. make the script executable: chmod +x rename_files.sh
|
|
# 3. run the script in the directory you want to clean up: ./rename_files.sh
|
|
|
|
# function to "clean" the names of files and directories
|
|
# the function converts uppercase to lowercase, replaces accented characters with their base equivalents,
|
|
# and replaces any character that is not a letter, digit, dot, underscore, or hyphen with an underscore.
|
|
clean_name() {
|
|
echo "$1" | tr '[:upper:]' '[:lower:]' | sed -e 's/[àáâãäå]/a/g' \
|
|
-e 's/[èéêë]/e/g' \
|
|
-e 's/[ìíîï]/i/g' \
|
|
-e 's/[òóôõö]/o/g' \
|
|
-e 's/[ùúûü]/u/g' \
|
|
-e 's/[ç]/c/g' \
|
|
-e 's/[^a-zA-Z0-9._-]/_/g'
|
|
}
|
|
|
|
# loop through all files and directories in the current directory
|
|
for item in *; do
|
|
# check if the item exists (avoid errors if nothing is found)
|
|
if [ -e "$item" ]; then
|
|
# get the new "cleaned" name
|
|
new_name=$(clean_name "$item")
|
|
# if the new name is different from the old one, rename the item
|
|
if [ "$item" != "$new_name" ]; then
|
|
mv "$item" "$new_name"
|
|
echo "renamed: '$item' -> '$new_name'"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# display a message indicating that all files and directories have been processed
|
|
echo "all files and directories have been processed."
|