-
Notifications
You must be signed in to change notification settings - Fork 154
/
batch_file_rename.sh
59 lines (44 loc) · 1.69 KB
/
batch_file_rename.sh
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
#!/bin/bash
# Script Name : batch_file_rename.sh
# Author : Craig Richards
# Created : 06-August-2012
# Last Modified :
# Version : 1.0
# Modifications :
# Description : This will batch rename all files from one extension to another
#################################
# Start of procedures/functions #
#################################
funct_check_params() # Start of the procedure
{
if [ ${NARG} -lt 3 ]; then # Check there are 3 arguments passed to the script, if not display the help
echo "Usage: $0 directory *.old_extension *.new_extension"
exit 1 # Quit the program
elif # Else if
# If the argument passed is -h or --h then display the following message in the echo statement
[[ ${script_name} = "-h" ]] || [[ ${script_name} = "--h" ]]; then
echo "Usage: $0 directory old_extension new_extension"
exit 1 # Quit the program
fi # End of the if statement
} # End of the function
funct_batch_rename() # Start of the procedure
{
old_ext_cut=$(echo "${old_ext}" | cut -f2 -d .) # Cut just the extension passed, lose the .
new_ext_cut=$(echo "${new_ext}" | cut -f2 -d .) # Cut just the extension passed, lose the .
# This will carry out the rename
for file in "$work_dir"/*"$old_ext"; do mv "$file" "$(echo "$file" | sed 's/\(.*\.\)'"$old_ext_cut"'/\1'"$new_ext_cut"/)" ; done
}
################
# Main Program #
################
# Variable Settings
script_name=$0
work_dir=$1
old_ext=$2
new_ext=$3
NARG=$#
{ # Start the main program
funct_check_params # Call the procedure
funct_batch_rename # Call the procedure
} # End of the main program
## End of Script