-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_movie_dirs.sh
executable file
·151 lines (132 loc) · 4.46 KB
/
create_movie_dirs.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
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
#!/usr/bin/env bash
#title :create_movie_dirs.sh
#description :This script reorganizes/moves movie (and associated) files organized in a single directory to their own directory.
#author :J.D. Stone
#date :20240702
#version :2.2.1
#usage: :./create_movie_dirs.sh -d <directory> [-c <count>] [-h]
#
# ./create_movie_dirs.sh -d <directory> Set the movie directory on which the script should process.
# ./create_movie_dirs.sh [-c <count>] Set the number of files to process. Default is 25.
# ./create_movie_dirs.sh [-h] Display this help message.
#
# Examples:
# ./create_movie_dirs.sh -d ~/my_movies
# ./create_movie_dirs.sh -d ~/my_movies -c 15
#
#TODO:
# - add option for extensions to process
#==============================================================================
## CLI Options
opt_d=0
opt_c=0
## Set a limit for how many files you want to process per run
directory_file_count_limit=25
## Red Color
RED='\033[0;31m'
## No Color
NC='\033[0m'
main () {
local i=0
cd "$movie_root" || exit
echo
echo -e "${RED}!!!! $(pwd) -- Are you sure you want to continue with this movie directory? !!!!${NC}"
read -r -p "Press Ctrl-C to exit or Enter to continue"
echo
read -r -p "Are you sure you pressed the correct key (Ctrl-C to exit or Enter to continue)?"
echo; echo;
echo "!==== Creating the new movie directory structure and copying movies to their new location ====!" | tr "[:lower:]" "[:upper:]"
echo
echo
for file in *.mkv *.mp4 *.avi; do
base_filename=$file
if [ "$i" -eq "$directory_file_count_limit" ]; then break; fi
if ! { [ "$base_filename" = "*.mkv" ] || [ "$base_filename" = "*.mp4" ] || [ "$base_filename" = "*.avi" ]; }; then
movie_root_filename=$(echo $base_filename | sed 's/\.\///' | cut -d "(" -f 1 | sed 's/\.$//')
movie_titlename=$(echo $movie_root_filename | sed 's/\./ /g')
movie_titlename_upper=$(echo "$movie_titlename" | tr "[:lower:]" "[:upper:]")
file_name_wo_ext=${base_filename%.*}
echo ===========================================================================
echo "Processing $movie_titlename_upper"
echo ===========================================================================
echo
echo " Creating movie directory..."
mkdir "$movie_titlename"
echo
echo " Moving all associated movie files to movie directory..."
mv "$file_name_wo_ext"* "$movie_titlename"/
echo
echo
i=$((i+1))
fi
done
echo
if [ $i -gt 1 ] || [ $i -eq 0 ]; then
echo "$i movies were processed." && echo; echo;
elif [ $i -eq 1 ]; then
echo "$i movie was processed." && echo; echo;
fi
}
usage () {
echo "Usage: ${0} -d <directory> [-c <count>] [-h]"
echo
echo " ${0} -d <directory> Set the movie directory on which the script should process."
echo " ${0} [-c <count>] Set the number of files to process. Default is 25."
echo " ${0} [-h] Display this help message."
echo
echo " Examples:"
echo " ${0} -d ~/my_movies"
echo " ${0} -d ~/my_movies -c 15"
}
while getopts "c:d:h" opt; do
case ${opt} in
c )
# opt_c=1
directory_file_count_limit=${OPTARG}
if [[ ( ! ${directory_file_count_limit} =~ ^[0-9]+$ ) ]]; then
echo -e "${RED}[!]${NC} ${OPTARG} is an invalid argument for option -${opt}" 1>&2
echo -e "${RED}[!]${NC} a file count (number) must be specified when using the -${opt} option"
echo
usage
exit 1
fi
;;
d )
opt_d=1
movie_root="${OPTARG}"
if [[ ! -d "${movie_root}" ]]; then
echo -e "${RED}[!]${NC} '${movie_root}' is not a valid directory"
echo
usage
exit 1
fi
;;
: )
echo "illegal option -- ${OPTARG} requires an argument" 1>&2
if [[ "${OPTARG}" == "d" ]]; then
echo -e "${RED}[!]${NC} a directory must be specified when using the -${opt} option\n"
echo
elif [[ "${OPTARG}" == "c" ]]; then
echo -e "${RED}[!]${NC} a file count (number) must be specified when using the -${opt} option\n"
echo
else
echo
fi
usage
exit 1
;;
h | * )
echo
usage
exit 0
;;
esac
done
shift $((OPTIND -1))
## decision logic
if [[ ${opt_d} == 1 ]]; then
main
elif [[ ( $# -eq 0 || $# -gt 0 ) || ( ! ${opt_d} ) ]]; then
usage
exit 0
fi