-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename
executable file
·55 lines (49 loc) · 1.09 KB
/
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
#!/bin/sh
# 03/01/17 - Created by Andrew Puckett
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ $# -lt 1 ]; then
echo "NAME"
echo " $(basename $0) options"
echo
echo "DESCRIPTION"
echo " $(basename $0) is used to rename files in a directory."
echo
echo "OPTIONS"
echo " -d"
echo " Directory to use. Optional, default is current directory."
echo " -p"
echo " File prefix to remove. Either this or -q is required."
echo " -q"
echo " File postfix to remove. Either this or -p is required."
echo "AUTHOR"
echo " Andrew Puckett"
echo
exit 1
fi
DIR=.
I=0
PREFIX=
POSTFIX=
while getopts d:p:q: opt
do
case "$opt" in
d) DIR=$OPTARG;;
p) PREFIX=$OPTARG;;
q) POSTFIX=$OPTARG;;
esac
done
if [ -z "$PREFIX" ] && [ -z "$POSTFIX" ]; then
echo "You must provide a prefix or postfix with the -p or -q option" && exit 1
fi
IFS="$(printf '\n\t')"
FILES=$(find $DIR/$PREFIX* -type f)
for FILE in $FILES
do
if [ -z "$PREFIX" ]; then
#only removes from the very end
mv "$FILE" "$DIR/${FILE%$POSTFIX}"
else
mv "$FILE" "$DIR/${FILE#$DIR/$PREFIX}"
fi
I=$((I + 1))
done
echo "Renaming $I files"