Skip to content

Instantly share code, notes, and snippets.

@camaleaun
Created November 7, 2017 15:12
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 camaleaun/f41a35c94e63bdacb900db53b1e21388 to your computer and use it in GitHub Desktop.
Save camaleaun/f41a35c94e63bdacb900db53b1e21388 to your computer and use it in GitHub Desktop.
Rename images without spaces and special characteres for import
#!/bin/bash
slug() {
slug="$1"
slug=${slug,,}
slug=${slug// /_}
slug=${slug//ó/o}
slug=${slug//ô/o}
slug=${slug//é/e}
slug=${slug//ú/u}
slug=${slug//ê/e}
slug=${slug//í/i}
slug=${slug//ç/c}
slug=${slug//./_}
slug=${slug//ã/a}
slug=${slug//á/a}
slug=${slug//#/}
slug=${slug//+/}
slug=${slug//¦/o}
slug=${slug//¬/e}
slug=${slug//(/}
slug=${slug//)/}
slug=${slug//,/_}
slug=${slug//_-_/-}
slug=${slug//__/_}
slug=${slug//_&&_/_}
}
sanitize() {
#echo "sanitizing: $i"
#folder="${1//\s/-}"
if [ -d "$i" ];then
origin="$i"
slug "$(basename "$origin")"
#echo "sanitizing dir: $slug"
folder=$(dirname "$origin")/$slug
if [ ! -d "$folder" ]; then
mv "$origin" $folder
fi
i=$folder
elif [ -f "$i" ]; then
origin="$i"
filename=$(basename "$i")
ext="${filename##*.}"
ext=${ext,,}
filename="${filename%.*}"
slug "${filename%.*}"
newfile=$slug.$ext
path=$(dirname "$origin")/$newfile
if [ ! -f "$path" ]; then
#mv "origin" $path
mv "$origin" $path
elif [ -f "$path" ]; then
n=1
newfile=$slug$n.$ext
path=$(dirname "$origin")/$newfile
while [ -f "$path" ]
do
(( n++ ))
newfile=$slug$n.$ext
path=$(dirname "$origin")/$newfile
done
mv "$origin" $path
fi
#echo "sanitizing file ext: $extension"
fi
}
# loop & print a folder recusively,
print_folder_recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
# #echo "dir: $i"
sanitize "$1"
# #echo "dir sanitized: $folder"
print_folder_recurse "$i"
elif [ -f "$i" ]; then
sanitize "$1"
total=4776
pp=$(expr $p \* 100)
percent=$(expr $pp / $total)
echo "$percent%"
(( p++ ))
#echo "file: $i"
fi
done
}
# try get path from param
path=/mnt/c/git/movelhome/Fotos
if [ -d "$1" ]; then
path=$1;
else
path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
p=1
#echo "base path: $path"
print_folder_recurse $path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment