Rename files using your favorite text editor

When renaming lots of files you usually resort to your typical for f in … command line with a sed command or similar included, or perhaps you use rename(1) but sometimes it’s just easier if you can use your normal editor. In my case this is vim, where I can then use various nice things such as changes on blocks, or just ad hoc changes to the file names. To accomplish this in an easy way I use an old quick hack; which is the following script that I’ve named viren:

#!/bin/sh

# viren
# Rename files using an editor (vim in this case)

FNEW=`mktemp -t`
FORIG=`mktemp -t`
FCMDS=`mktemp -t`
for i in $*
do
        echo "\"$i\"" >> $FORIG
done
cat $FORIG > $FNEW

vim $FNEW

wcn=`wc -l $FNEW | cut -d\  -f1`
wco=`wc -l $FORIG | cut -d\  -f1`

# go ahead if the line count is the same
if [ $wcn -eq $wco ]
then
        paste $FORIG $FNEW | sed -e 's/^/mv /g' > $FCMDS
        source $FCMDS
        echo Renamed/moved files
else
        echo Aborted rename/move
fi

rm $FNEW $FORIG $FCMDS

2 thoughts on “Rename files using your favorite text editor

  1. In the one true editor – Emacs – you just enter Ctrl-x Ctrl-q in a dired buffer, edit to your hearts content and then enter Ctrl-c Ctrl-c when your done.

  2. Nice tip for all the crazy people using Emacs… You can edit the directory contents directly in vim as well, but unfortunately it doesn’t allow you to use the normal editing mode.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>