Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 62 lines (54 sloc) 922 Bytes
#!/bin/bash
NUM=1
TOTAL_NUM=`ls -l |grep ^- |wc -l`
LENGTH=`echo $TOTAL_NUM |awk '{print length($0)}'`
while getopts ":fl:" arg
do
case $arg in
f)
FLAG="write";;
l)
LENGTH_OPT="$OPTARG";;
:)
echo "Error: -$OPTARG needs an argument"
exit 1;;
?)
echo "Usage: $name [-f] [-l length]"
exit 1;;
esac
done
if [[ "$LENGTH_OPT" =~ ^[0-9]+$ ]]
then
if [ "$LENGTH_OPT" -gt "$LENGTH" ]
then
LENGTH=$LENGTH_OPT
fi
fi
if [ "$FLAG" == "write" ]
then
TEMP_DIR=`mktemp -d .tmp.XXXXXXXX` || exit 1
fi
for orig in `ls -S -r`
do
if [ ! -f "$orig" ]
then
continue
fi
EXT=${orig##*.}
echo ""`printf %0.${LENGTH}d $NUM`.$EXT" <-- "$orig""
if [ "$FLAG" == "write" ]
then
mv "$orig" "${TEMP_DIR}/`printf %0.${LENGTH}d $NUM`.$EXT"
fi
NUM=$[$NUM + 1]
done
if [ "$FLAG" == "write" ]
then
mv ${TEMP_DIR}/* ./
rmdir $TEMP_DIR
else
echo
echo "Usage: $name [-f] [-l length]"
echo
fi
exit 0