-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmufire.sh
More file actions
executable file
·162 lines (137 loc) · 4.62 KB
/
mufire.sh
File metadata and controls
executable file
·162 lines (137 loc) · 4.62 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
#-----------------------------------------------------------------------------------
# __ _
# _ __ _ _ / _(_)_ _ ___
# | ' \ || | _| | '_/ -_)
# |_|_|_\_,_|_| |_|_| \___|
# Multifile Rename
#-----------------------------------------------------------------------------------
VERSION="1.2.4"
#-----------------------------------------------------------------------------------
#
# Allows batch file renaming. This function is not recursive, so only the files in the
# parent directory get renamed (folders are skipped). You will be prompted to specify
# the name you would like the files to be renamed to, and the starting number of the
# incrementing counter.
#
# Usage:
#
# cd into the directory containing files you want to rename, then execute mufire.sh
#
# /path/to/mufire.sh
#
#-----------------------------------------------------------------------------------
# Author: Rick Ellis
# URL: https://github.com/rickellis/Shell-Scripts
# License: MIT
#-----------------------------------------------------------------------------------
_currentdir=$(pwd)
_tempname="TMP129384756XYZ"
# Load colors script to display pretty headings and colored text
# This is an optional (but recommended) dependency
BASEPATH=$(dirname "$0")
if [ -f "${BASEPATH}/colors.sh" ]; then
. "${BASEPATH}/colors.sh"
else
heading() {
echo " ----------------------------------------------------------------------"
echo " $2"
echo " ----------------------------------------------------------------------"
echo
}
fi
clear
heading green "Multi-File Rename ${VERSION}"
echo " You are in the following directory:"
echo
echo " ${_currentdir}"
echo
read -p " Do you want to rename the files in this directory? [y|n] " _consent
if [[ $_consent != "" ]] && [[ $_consent != "y" ]] && [[ $_consent != 'Y' ]]; then
echo
echo " Goodbye..."
echo
exit 1
fi
if [ -z "$(ls -A ${_currentdir})" ]; then
echo
echo " The directory you have specified is empty. Aborting..."
echo
exit 1
fi
echo
read -p " What would you like the new file name to be? " _newname
if [[ -z $_newname ]]; then
echo " New name is required. Aborting..."
exit 1
fi
echo
read -p " What should the starting number be? " _startn
if ! echo $_startn | egrep -q '^[0-9]+$'; then
echo " Invalid number. Aborting..."
exit 1
fi
# Performs the file renaming
function rename() {
# $1 = the source directory
# $2 = the new file name
# $3 = the starting number
if [ -z $1 ] ||[ -z $2 ] || [ -z $3 ]; then
echo " The rename function requires three arguments: source dir, new filename, starting n."
exit 1
fi
# Set the result code. This determines whether we show a message at the end.
result=1
if [ -z $4 ]; then
result=0
elif [ $4 -eq "0" ] || [ $4 -eq "1" ]; then
result=$4
fi
# set these for clarity
sourcedir=$1
newname=$2
i=$3
n=0
# Here we go!...
j="$_startn"
for filepath in `ls -v` "$sourcedir"/*
do
# Is the current filename acutally a file?
if [[ -f $filepath ]]; then
# extract the filename from the path
filename="${filepath##*/}"
# skip dotfiles
if echo $filename | egrep -q '^[.]'; then
continue 2
fi
# extract the file extension
ext="${filename##*.}"
# Show progress, but only on actual files, not temp ones
if [ "${filename:0:15}" != "$_tempname" ]; then
echo " ${filename} renamed to ${_newname}${j}.${ext}"
((j++))
fi
# rename the file
mv "$filename" "${newname}${i}.${ext}"
((i++))
((n++))
fi
done
if [[ $result -eq "1" ]]; then
echo -e "\n ${n} files renamed!\n"
fi
}
# Below we run the rename function twice.
# The first time sets the filename to a temporary string. The second time sets the new filename.
# Why do we do it in two passes? An earlier version of this script just skipped renaming any
# files that already had the new name. However, this created a couple problems. 1. the original
# sort order of the files wasn't retained. And 2. identically named files with different file
# extensions wouldn't get sequentially renamed. The simplest solution was to do it in two passes.
#
# Required arguments:
# $1 = the source directory
# $2 = the new file name
# $3 = the starting number
# $4 = whether to show renamed file count
rename "$_currentdir" "$_tempname" "1" "0"
rename "$_currentdir" "$_newname" "$_startn" "1"