shell

Script for making an ordered list of files

You’ve got a bunch of files you want to reorder (that is rename so that they appear in a specific order) but do not want to really rename them (their original names are relevant). You may want to create a set of links to each of them with the appropriate ’001′, ’002′ term before. Here is a script for doing exactly this.
Use as follows (if named ‘mapnames.sh’):

$ mapnames.sh digits file1 [file2 file3 ...]

will use digits digits for the leading numeral (adding '0' before) and will link the files in the given order.
Here is the code:

#!/bin/sh
length=$1
shift
count=1
for name in $@
do
    zeroes=`printf "%0${length}d" $count`
    if [ -e "$zeroes$count.$name" ] ; then
        echo "$zeroes$count.$name exists, left unnamed."
    else
        ln -s "$name" "$zeroes$count.$name"
    fi
    i=$(($i+1));
done

This question came up on the UNIX bboard at sdf. Use with caution. Rafacas did a rather important code optimization...

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

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

*Required Fields