Skip to content

Instantly share code, notes, and snippets.

@mattiasb
Created September 29, 2017 00:10
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 mattiasb/c3373712f0b59a5d6463184d857aadbe to your computer and use it in GitHub Desktop.
Save mattiasb/c3373712f0b59a5d6463184d857aadbe to your computer and use it in GitHub Desktop.
#!/bin/bash
function _help-flag() {
[[ " ${@} " == *" --help "* ]] || [[ " ${@} " == *" -h "* ]]
}
if _help-flag "${@}" || [ $# -lt 3 ] ; then
echo "Usage: ${0} <STR1> <STR2> FILE..."
echo
echo "Rename a bunch of files by replacing STR1 with STR2 in their filename."
exit 2
fi
str1=$1
str2=$2
shift 2
for f in "$@"; do
echo "${f} => ${f/$str1/$str2}"
done | column -t
echo Proceed with renaming? [yes / no]
read -r answer
if [ "${answer}" = "yes" ]; then
for f in "$@"; do
mv "${f}" "${f/$str1/$str2}"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment