Skip to content

Instantly share code, notes, and snippets.

@rbecheras
Last active January 4, 2018 15:14
Show Gist options
  • Save rbecheras/6ce05660c4796c003e79baddd5964de2 to your computer and use it in GitHub Desktop.
Save rbecheras/6ce05660c4796c003e79baddd5964de2 to your computer and use it in GitHub Desktop.
A script to rename in mass replacing a string by an other
#!/usr/bin/env bash
# Bash colors
RED="\033[0;31m"
GREEN="\033[0;32m"
ORANGE="\033[0;33m"
YELLOW="\033[1;33m"
NC="\033[0m" # No Color
ERROR="${RED}ERREUR:"
SUCCESS="${GREEN}OK:"
WARNING="${ORANGE}ATTENTION:"
NOTICE="${YELLOW}INFO:"
if [[ -z "$1" ]]
then
printf "${ERROR} You must pass the source directory name as first argument${NC}\n"
exit 1
fi
if [[ ! -d "$1" ]]
then
printf "${ERROR} The directory '$1' doesn't exist${NC}\n"
exit 1
fi
if [[ -z "$2" ]]
then
printf "${ERROR} You must pass the search string as second argument${NC}\n"
exit 1
fi
if [[ -z "$3" ]]
then
printf "${ERROR} You must pass the replace string as third argument${NC}\n"
exit 1
fi
targetDir="${1%/}"
search="$2"
replace="$3"
printf "${NOTICE}> The process will rename the following files:${NC}\n"
for file in $(ls $targetDir)
do
newName="$(echo $file | sed s/$search/$replace/g)"
if [[ "$file" != "$newName" ]]
then
echo "* \"$file\" −−> \"$newName\" : mv $targetDir/$file $targetDir/$newName"
fi
done
printf "${NOTICE}\n> Do you confirm the process ?${NC}\n"
select yn in "Yes" "No"; do
case $yn in
Yes )
start=`date +%s`
i=$((0))
errorsNumber=$((0))
for file in $(ls $targetDir)
do
newName="$(echo $file | sed s/$search/$replace/g)"
if [[ "$file" != "$newName" ]]
then
echo "* \"$file\" −−> \"$newName\""
mv $targetDir/$file $targetDir/$newName
if [[ $? != "0" ]]
then
printf "${ERROR} Impossible de convertir '$file'${NC}\n"
errorsNumber=$(($errorsNumber+1))
else
printf "${SUCCESS}.${NC}\n"
fi
fi
done
end=`date +%s`
runtime=$((end-start))
if [[ $(($errorsNumber)) > 0 ]]
then
printf "${ERROR} $errorsNumber errors encountered!${NC}\n"
exit 1
else
printf "${SUCCESS} $i files has been renamed in ${runtime}s.${NC}\n"
fi
break;;
No )
printf "${WARNING} Process canceled by the user. Exiting.${NC}\n"
exit;;
esac
done
exit 0
@rbecheras
Copy link
Author

rbecheras commented Jan 4, 2018

Installation

$ scp me@localhost me@remotehost:/home/me/mass-rename-replace.sh
$ ssh me@remotehost
$ chmod +x /home/me/mass-rename-replace.sh

Usage

capture d ecran de 2018-01-04 16-12-35

In text mode :

remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ls scripts/
gulp-tasks  tota  toto
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh 
ERREUR: You must pass the source directory name as first argument
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh not-existant-folder
ERREUR: The directory 'not-existant-folder' doesn't exist
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh scripts/
ERREUR: You must pass the search string as second argument
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh scripts/ ta
ERREUR: You must pass the replace string as third argument
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh scripts/ ta TA
INFO:> The process will rename the following files:
* "gulp-tasks"  −−>  "gulp-TAsks" : mv scripts/gulp-tasks scripts/gulp-TAsks
* "tota"  −−>  "toTA" : mv scripts/tota scripts/toTA
INFO:
> Do you confirm the process ?
1) Yes
2) No
#? 2
ATTENTION: Process canceled by the user. Exiting.
remi@poremil:~/d/dev/generate-swap-project:24-generate-package-file
$ ./mass-rename-replace.sh scripts/ ta TA
INFO:> The process will rename the following files:
* "gulp-tasks"  −−>  "gulp-TAsks" : mv scripts/gulp-tasks scripts/gulp-TAsks
* "tota"  −−>  "toTA" : mv scripts/tota scripts/toTA
INFO:
> Do you confirm the process ?
1) Yes
2) No
#? 1
* "gulp-tasks"  −−>  "gulp-TAsks"
OK:.
* "tota"  −−>  "toTA"
OK:.
OK: 0 files has been renamed in 0s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment