-
Notifications
You must be signed in to change notification settings - Fork 0
/
organize-multimedia.linux-unix.sh
106 lines (103 loc) · 4.04 KB
/
organize-multimedia.linux-unix.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
#!/bin/bash
cfgSource="$1"
cfgDestRoot="$2"
toUpper() {
echo $1 | tr "[:lower:]" "[:upper:]"
}
toLower() {
echo $1 | tr "[:upper:]" "[:lower:]"
}
escapeSpaces(){
echo $1 | sed 's/ /\\ /g'
}
if [ "$#" != "2" ]; then
echo "Usage: $0 <source> <destination>"
exit 1
fi
which stat > /dev/null
# make sure stat command is installed
if [ $? -eq 1 ]
then
echo "stat command not found!"
exit 2
fi
which identify > /dev/null
# make sure identify command is installed
if [ $? -eq 1 ]
then
echo "identify command not found!"
exit 3
fi
# using a tmp file you can have spaces in the file path
find "$cfgSource" -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.nef" -o -iname "*.png" -o -iname "*.bmp" -o -iname "*.avi" -o -iname "*.flv" -o -iname "*.VOB" -o -iname "*.mov" -o -iname "*.mpg" -o -iname "*.mp4" > images.tmp
cat ./images.tmp | while read f;
do
# Make sure the file we've been given by find actually exists.
if [ -f "${f}" ]; then
echo " "
FILETYPE=$(toUpper ${f#*.})
timestamp=""
if [ "$FILETYPE" = "JPG" -o "$FILETYPE" = "NEF" ]; then
timestamp="$(identify -format '%[exif:DateTimeOriginal]' ${f})"
timestamp=${timestamp%T*}
fi
#If identify fails to read the date from exif
# or file is not an image, use stat to get last modification date
if [ "${timestamp}" = "" ]; then
timestamp=$(stat -c %y "$f")
fi
# Looks like there are three possible timestamp formats:
# 2014-05-05T14:46:47.16+01:00
# 2015:02:28 12:57:50
# 2013-05-25 19:24:26.000000000 +0100
# Thankfully, cut will handle all of these formats.
y=$(echo $timestamp | cut -c 1-4)
m=$(echo $timestamp | cut -c 6-7)
d=$(echo $timestamp | cut -c 9-10)
destFile=$cfgDestRoot/$y/$m/$d/$(basename "${f}")
# If the directory doesn't exist recursively create it.
if [ ! -d "$cfgDestRoot/$y/$m/$d" ]; then
mkdir -p "$cfgDestRoot/$y/$m/$d"
fi
# Move the file.
if [ -f "${destFile}" ]; then
# Existing file found.
echo "Existing file found: ${destFile}"
echo "Source: ${f}"
# Is it the same file? If so, delete the file we're processing.
md5src=$(md5sum "${f}")
md5src=${md5src% *}
md5dst=$(md5sum "${destFile}")
md5dst=${md5dst% *}
if [ $md5src = $md5dst ]; then
echo "Duplicate found, discarding identical file"
rm "$f"
else
# Is this file larger than the existing one?
sizeSrc=$(stat -c%s "$f")
sizeDst=$(stat -c%s "$destFile")
echo "Duplicate Found, keeping the larger file."
if [ $sizeSrc -gt $sizeDst ]; then
mv "$f" "$destFile"
else
rm "$f"
fi
fi
else
mv "$f" "$destFile"
echo "Moved $f to $destFile"
fi
else
echo "${f}"
echo "File not found!"
fi
echo "== =="
done
#Now delete empty folders from source
find "$cfgSource" -iname "*.DS_Store" -exec rm -v {} +
find "$cfgSource" -iname "*.thm" -exec rm -v {} +
find "$cfgSource" -iname "*.IND" -exec rm -v {} +
find "$cfgSource" -iname "Thumbs.db" -exec rm -v {} +
find "$cfgSource" -type d -empty -delete
#Delete temporary file
rm images.tmp