Skip to content

Instantly share code, notes, and snippets.

@glynhudson
Created January 31, 2017 20:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glynhudson/c159bdfa813ce30d09ff4f3aca49177b to your computer and use it in GitHub Desktop.
Save glynhudson/c159bdfa813ce30d09ff4f3aca49177b to your computer and use it in GitHub Desktop.
Bash script to rename file names in a folder to remove spaces and special characters, used when importing photos into Piwigo
#!/bin/bash
# Convert filenames to lowercase
# and replace characters recursively
#####################################
if [ -z $1 ];then echo Give target directory; exit 0;fi
find "$1" -depth -name '*' | while read file ; do
directory=$(dirname "$file")
oldfilename=$(basename "$file")
newfilename=$(echo "$oldfilename" | tr ' ' '_' | tr ',' '_' | tr '+' '_' | tr '&' '_' | tr '!' '_' | tr "'" "-" | tr "(" "-" | tr ")" "-" | tr "~" "-" | tr ']' '_' | tr '[' 'p')
if [ "$oldfilename" != "$newfilename" ]; then
mv -i "$directory/$oldfilename" "$directory/$newfilename"
echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
#echo "$directory"
#echo "$oldfilename"
#echo "$newfilename"
#echo
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment