Skip to content

Instantly share code, notes, and snippets.

@seanriceaz
Last active May 19, 2016 21:01
Show Gist options
  • Save seanriceaz/e36ade67b67163ce04f1c6f74f1c6576 to your computer and use it in GitHub Desktop.
Save seanriceaz/e36ade67b67163ce04f1c6f74f1c6576 to your computer and use it in GitHub Desktop.
The purpose of this script is to churn through a directory with a bunch of files in it that have been misnamed. It will rename files similar to a find and replace.
#!/bin/bash
# The purpose of this script is to churn through a directory with a bunch of
# files in it that have been misnamed. It will rename files similar to a find
# and replace.
#Find this string
FINDTHIS="--"
#Replace it with this string
REPLACEWITH="-"
for file in ./**; do
if [[ -f "$file" ]]; then
dirname="${file%/*}/"
basename="${file:${#dirname}}"
#Find and Replace
newname="${basename//$FINDTHIS/$REPLACEWITH}"
#Concatenate
#newname="STRINGTOSTARTWITH--$basename"
#regex Rearrange
#newname=$(sed 's/^\(.*--\)\(.*-\)\(.*-\)\(.*-\)\(.*-\)\(.*$\)/\1\2to-\4\3\5\6/' <<< $basename)
#echo $newname
mv "$file" "$dirname$newname"
fi
done
@seanriceaz
Copy link
Author

Adding more options like concatenating and using a regex to rearrange the file name

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