Skip to content

Instantly share code, notes, and snippets.

@KeyboardCowboy
Last active April 5, 2017 18:10
Show Gist options
  • Save KeyboardCowboy/2531be88ebadf41bd04d to your computer and use it in GitHub Desktop.
Save KeyboardCowboy/2531be88ebadf41bd04d to your computer and use it in GitHub Desktop.
Bulk Rename Files
#!/bin/bash
# File: rename
# Author: Chris Albrecht (chris at lullabot dot com)
#
# Bulk renaming of files.
FROM=$1
TO=$2
FILTER=$3
if [[ -z "$FROM" ]] || [[ -z "$TO" ]]; then
echo "Usage: rename <from> <to> [\"<filter>\"] "
exit 1
fi
# If no mask is set, get all files in the current dir.
if [[ -z "$FILTER" ]]; then
FILTER="*"
fi
for ORIG in $FILTER
do
if [[ -f $ORIG ]] || [[ -d $ORIG ]]; then
NEW=${ORIG/$FROM/$TO}
# If the new filename would be different, rename the file.
if [[ $NEW != $ORIG ]]; then
echo "$ORIG >> $NEW"
mv "$ORIG" "$NEW"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment