-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzm_sort_images.sh
executable file
·50 lines (39 loc) · 1.4 KB
/
zm_sort_images.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
#!/bin/bash
UPLOADPATH="/export/zoneminder/uploads";
CAMERAS=($(ls ${UPLOADPATH}));
# Command variables
AWK=`which awk || "/usr/bin/awk"`;
BASENAME=`which basename || "/usr/bin/basename"`;
ECHO=`which echo || "/bin/echo"`;
FIND=`which find || "/usr/bin/find"`;
MKDIR=`which mkdir || "/bin/mkdir"`;
MV=`which mv || "/bin/mv"`;
SED=`which sed || "/bin/sed"`;
# Cycle through the cameras
for i in ${CAMERAS[@]};
do
${ECHO} "Sorting ftp uploads for ${i}";
# Find the files in the UPLOAD PATH base directory to sort
for j in `${FIND} ${UPLOADPATH}/${i} -maxdepth 1 -iname '*.jpg' -exec ${BASENAME} '{}' \;`;
do
# Grab the date/time string from the filename
DTM=`${ECHO} ${j} | ${AWK} -F'_' '{print $3}'`;
# Split the date/time into the individual date components
YEAR=${DTM:0:4};
MONTH=${DTM:4:2};
DAY=${DTM:6:2}
HOUR=${DTM:8:2}
# Define the full path for sanitizing
FILE="${UPLOADPATH}/${i}/${j}";
# Sanitize: (
FILE=`${ECHO} ${FILE} | ${SED} -e 's/(/\\(/g'`;
# Sanitize: )
FILE=`${ECHO} ${FILE} | ${SED} -e 's/)/\\)/g'`;
# Make the applicable directory for the sorted file
${MKDIR} -p ${UPLOADPATH}/${i}/${YEAR}/${MONTH}/${DAY}/${HOUR};
# Move the file into place
${MV} "${FILE}" ${UPLOADPATH}/${i}/${YEAR}/${MONTH}/${DAY}/${HOUR}/;
done
done
# Remove any empty directories from the UPLOAD PATH
${FIND} ${UPLOADPATH} -type d -empty -delete