Skip to content

Instantly share code, notes, and snippets.

@geta6
Created November 20, 2012 07:07
Show Gist options
  • Save geta6/4116512 to your computer and use it in GitHub Desktop.
Save geta6/4116512 to your computer and use it in GitHub Desktop.
rename script

Rename Script

Requirement

  • zsh

Usage

  • target is current directory
  • -f from, zsh regular expression
  • -t to, zsh regular expression
  • --notest, finally rename the files
% rename -f test -t my

test-hoge -> my-hoge
test-fuga -> my-fuga
test-piyo -> my-piyo

% rename -f 'glyphicons_[0-9]##_' -t ''

glyphicons_000_glass.png -> glass.png
glyphicons_001_leaf.png  -> leaf.png
:
:
#!/usr/bin/env zsh
rename () {
EXE='no'
_FM='no'
_TO='no'
FM=''
TO=''
for A in $@
do
if [ $_FM = 'yes' ]
then
_FM='no'
FM=$A
fi
if [ $_TO = 'yes' ]
then
_TO='no'
TO=$A
fi
if [ $A = '--notest' ]
then
EXE='yes'
elif [ $A = '-f' ]
then
_FM='yes'
elif [ $A = '-t' ]
then
_TO='yes'
fi
done
echo "/$FM/ replace to '$TO'\n"
if [ $EXE = 'yes' ]
then
for F in ./*
do
mv -iv "$F" "${F/$FM/$TO}"
done
else
for F in ./*
do
echo "$F -> ${F/$FM/$TO}"
done
echo "\nNo changes to your files done.\nUse --notest to finally rename the files."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment