forked from adam8157/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass-rename
executable file
·67 lines (60 loc) · 1018 Bytes
/
mass-rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
NUM=1
NAME=$(basename "$0")
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
else
echo
echo "Just preview, use -f to make it effective"
echo
fi
IFS=$'\n'
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