Last active
April 5, 2017 18:10
-
-
Save KeyboardCowboy/2531be88ebadf41bd04d to your computer and use it in GitHub Desktop.
Bulk Rename Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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