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:


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

FNEW=`mktemp -t`
FORIG=`mktemp -t`
FCMDS=`mktemp -t`
for i in $*
        echo "\"$i\"" >> $FORIG
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 ]
        paste $FORIG $FNEW | sed -e 's/^/mv /g' > $FCMDS
        source $FCMDS
        echo Renamed/moved files
        echo Aborted rename/move


Move files to directories based on modification time

Here’s a really simple shell script to move files to directories named based on the modification time of each file. It uses only the date and the script also accepts an optional first argument to set a suffix for each directory. I sometimes use this as a first step when I organize photos and videos, the suffix is set to the source of the files (user-device in my case, eg ckk-40d if it’s from my Canon 40D), I then manually add a short description to the end based on the directory contents. Most commonly applied when I empty the card from my point-and-shoot.


# Put files on command line in one directory per date (based on mtime) with the
# suffix given by the first optional argument on the command line

if [ ! -n "$1" ]; then
   echo "Usage:"
   echo "$0 [suffix]  ... "

if [ ! -f "$1" ]; then

while [ -n "$1" -a -f "$1" ]; do
  mdate=`stat --printf "%y" $1 | cut -d\  -f1`
  mkdir -p $mdate$suffix
  mv -v $1 $mdate$suffix