-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfilename_formatter.sh
60 lines (51 loc) · 1.51 KB
/
filename_formatter.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
60
#!/bin/bash
echo -e "Arguments:
[0] - Used by Bash (script filename)
[1] - Base directory
[2] - Filename type (maximum two values)
[3] - Ignored files or folders (optional; use "\""./<directory_name>"\"")
"
# Separate $2 value (filename types) if it has a comma
if [[ "$2" == *","* ]];
then
string="$2"
str_value=${string#*,}
str_value2=${string%%,*}
else
str_value="$2"
str_value2="$2"
fi
# Do not run script if there are no given arguments.
if [[ "$1" == "" ]] || [[ "$2" == "" ]];
then
echo "No arguments given. Please specify minimum two arguments."
exit 1
fi
echo "Changed files:"
IFS=$'\n'; set -f
for fname in $(find $1 -type f -name "*$str_value2" -or -name "*$str_value")
do
ignored_files="$(echo "$3" | tr "," "\n")"
str="${fname}"
value=${str%/*} # If the base directory is `.`, check in all directories for the ignored filenames
for files in $ignored_files
do
if [ "${fname}" == "$value/$files" ] || [ "$value" == "$files" ];
then
continue 2
fi
done
#echo ${fname}
new_fname=$(echo "${fname}" | tr ' ' '_')
#echo " ${new_fname}"
new_fname=$(echo "${new_fname}" | tr '[:upper:]' '[:lower:]')
#echo " ${new_fname}"
new_fname=$(echo "${new_fname}" | tr '-' '_')
#echo " ${new_fname}"
if [ "${fname}" != "${new_fname}" ]
then
echo " ${fname} --> ${new_fname}"
git "mv" "${fname}" "${new_fname}" # Requires you to be in version control
fi
done
unset IFS; set +f