|
#!/bin/bash
#
# comcat <jiankemeng@gmail.com>
# usage: rn [-c start_count_number] [-p prefix_string] target_dir
###############################################################
COUNT=1000
usage()
{
echo "Usage: $0 [-c START_COUNT_NUMBER][-p PREFIX_STRING ] target_dir"
}
if [ $# -lt 1 ]
then
usage
exit 1
fi
while getopts ":c : p:" opt
do
case $opt
in
c)
COUNT="$OPTARG";;
p)
PREFIX="$OPTARG";;
\?)
usage
exit 1;;
esac
done
if [ $OPTIND -gt $# ]
then
echo "No target directory specified!"
exit 2
fi
shift $(($OPTIND-1))
TARGET=$1
for file in $(find $TARGET -type f)
do
if [ -f $file ]
then
if [ -n "$PREFIX" ]
then
new_name=`echo $file | sed -n 's|\(.*/\).\+|\1|p'`$PREFIX
new_name=$new_name\_$COUNT`echo $file | awk '{gsub(/^.+\./,".");print;}'`
else
new_name=`echo $file | sed -n 's#\(.*/\).\+#\1#p'`$COUNT`echo $file | awk \
'{gsub(/^.+\./,".");print;}'`
fi
mv $file $new_name
COUNT=`expr $COUNT + 1`
elif [ -d $file ]
then
echo "$file is a directory"
fi
done |
|