Sometimes you want to automate system tasks with scripts that need to be executed in a specific order. Then you need to edit your process and the numbering is not correct anymore. The following script will help to number your roughly ordered files by providing the necessary move command string.
The Script
#!/bin/bash
# This script sorts files in a directory and creates move commands to number them.
# Copyright 2010 by Reiner Rottmann. Released under BSD license.
#
d=${1%/*}
sep="-"
cnt=0
if [ -z "$1" ]; then
pat="*"
else
pat="$1"
fi
for f in $pat
do
f=${f##*/}
if [ $cnt -ge 0 ] && [ $cnt -lt 10 ]; then
dst=00$cnt$sep"$f"
elif [ $cnt -ge 10 ] && [ $cnt -lt 100 ]; then
dst=0$cnt$sep"$f"
else
dst=$cnt$sep"$f"
fi
echo mv "$d"/"$f" "$d"/"$dst"
cnt=$(($cnt + 1))
done
The Script in Action
$ ls /tmp/test
apple.schema core.schema fmserver.schema
microsoft.ext.schema nadf.schema ppolicy.schema
apple_auxillary.schema cosine.schema inetorgperson.schema
microsoft.schema netinfo.schema samba.schema
collective.schema duaconf.schema java.schema
microsoft.std.schema nis.schema
corba.schema dyngroup.schema krb5-kdc.schema
misc.schema openldap.schema
$ ./fn "/tmp/test/*"
mv /tmp/test/apple.schema /tmp/test/000-apple.schema
mv /tmp/test/apple_auxillary.schema /tmp/test/001-apple_auxillary.schema
mv /tmp/test/collective.schema /tmp/test/002-collective.schema
mv /tmp/test/corba.schema /tmp/test/003-corba.schema
mv /tmp/test/core.schema /tmp/test/004-core.schema
mv /tmp/test/cosine.schema /tmp/test/005-cosine.schema
mv /tmp/test/duaconf.schema /tmp/test/006-duaconf.schema
mv /tmp/test/dyngroup.schema /tmp/test/007-dyngroup.schema
mv /tmp/test/fmserver.schema /tmp/test/008-fmserver.schema
mv /tmp/test/inetorgperson.schema /tmp/test/009-inetorgperson.schema
mv /tmp/test/java.schema /tmp/test/010-java.schema
mv /tmp/test/krb5-kdc.schema /tmp/test/011-krb5-kdc.schema
mv /tmp/test/microsoft.ext.schema /tmp/test/012-microsoft.ext.schema
mv /tmp/test/microsoft.schema /tmp/test/013-microsoft.schema
mv /tmp/test/microsoft.std.schema /tmp/test/014-microsoft.std.schema
mv /tmp/test/misc.schema /tmp/test/015-misc.schema
mv /tmp/test/nadf.schema /tmp/test/016-nadf.schema
mv /tmp/test/netinfo.schema /tmp/test/017-netinfo.schema
mv /tmp/test/nis.schema /tmp/test/018-nis.schema
mv /tmp/test/openldap.schema /tmp/test/019-openldap.schema
mv /tmp/test/ppolicy.schema /tmp/test/020-ppolicy.schema
mv /tmp/test/samba.schema /tmp/test/021-samba.schema
| < Prev | Next > |
|---|