Rev Author Line No. Line
3407 kakl 1 #!/usr/bin/env bash
2 #
3 # tidyup.sh
4 #
5 # This script will sort create organized directory structure
6 # from observation files in one directory.
7 #
8 # Takes one argument: path to the directory
9 #
10 # Returns 0 if sorting succeeds
11 # Returns 1 if there are no files to sort
12 # Returns >1 in case of error
13 #
14 # before:
15 #
16 # |
17 # | 20131214214014084_SVAKOV-R2_met.jpg
18 # | 20131214214124184_SVAKOV-R2_met.jpg
19 # | 20131224214124184_SVAKOV-R2_met.jpg
20 # | .
21 # | .
22 # | .
23 #
24 # after:
25 #
26 # |
27 # |- SVAKOV-R2 <- observatory
28 # | |- 2013 <- year
29 # | |- 12 <- month
30 # | |- 14 <- day
31 # | |- 21 <- hour
32 # | | |- 20131214214014084_SVAKOV-R2_met.jpg
33 # | | |- 20131214214124184_SVAKOV-R2_met.jpg
34 # | |
35 # | |- 22
36 # | |- 20131224214124184_SVAKOV-R2_met.jpg
37 # |
38 # .
39 # .
40 # .
41 #
42 #
43 # filename format must be as 20131214214014084_SVAKOV-R2_met.jpg
44  
45 DELIM="_"
46 SLASH="/"
47 LAST=""
48  
49  
50 # turn on debug
51 set -x
52  
53 # none or 1 argument allowed
54 [[ "$#" -ne 1 ]] && echo "Wrong number of arguments ($#)" && exit 1
55  
56 # directory in which to sort must exists
57 [[ ! -d "$1" ]] && echo "Directory doesn't exist" && exit 1
58  
59 # captured images
60 cd "$1"/capture
61  
62 # if there are no files with $EXT extension in the directory then quit
63 EXT="jpg"
64 ls -f *."$EXT" > /dev/null 2>&1
65 if [ "$?" -eq 0 ]; then
66 for i in *."$EXT"; do
67 echo "processing " $i
68 OBSERVATORY=`echo $i | cut -d $DELIM -f2`
69 OBSERVATORY=`echo "$OBSERVATORY" | cut -d "." -f1`
70  
71 TIMESTAMP=`echo "$i" | cut -d $DELIM -f1`
72 YEAR=`echo "$TIMESTAMP" | cut -c 1-4`
73 MONTH=`echo "$TIMESTAMP" | cut -c 5-6`
74 DAY=`echo "$TIMESTAMP" | cut -c 7-8`
75 HOUR=`echo "$TIMESTAMP" | cut -c 9-10`
76  
77 # observatory / year / month / day / hour
78 DAYDIR="$YEAR$SLASH$MONTH$SLASH$DAY$SLASH$HOUR"
79  
80 # create directory with observatory name, year, month and day if hasn't existed before
81 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
82  
83 # check if directory really exists (if fs is full it might not be created)
84 [[ -d "$DAYDIR" ]] && mv "$i" "$DAYDIR"
85  
86 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
87 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > "$1"/LAST
88 done
89 else
90 echo "No .""$EXT"" files to sort"
91 fi
92  
93 #captured sounds
94 cd "$1"/audio
95  
96 EXT="wav"
97 ls -f *."$EXT" > /dev/null 2>&1
98 if [ "$?" -eq 0 ]; then
99 for i in *."$EXT"; do
100 echo "processing " $i
101 OBSERVATORY=`echo $i | cut -d $DELIM -f2`
102 OBSERVATORY=`echo "$OBSERVATORY" | cut -d "." -f1`
103  
104 TIMESTAMP=`echo "$i" | cut -d $DELIM -f1`
105 YEAR=`echo "$TIMESTAMP" | cut -c 1-4`
106 MONTH=`echo "$TIMESTAMP" | cut -c 5-6`
107 DAY=`echo "$TIMESTAMP" | cut -c 7-8`
108 HOUR=`echo "$TIMESTAMP" | cut -c 9-10`
109  
110 # observatory / year / month / day / hour
111 DAYDIR="$YEAR$SLASH$MONTH$SLASH$DAY"
112  
113 # create directory with observatory name, year, month and day if hasn't existed before
114 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
115  
116 # check if directory really exists (if fs is full it might not be created)
117 [[ -d "$DAYDIR" ]] && mv "$i" "$DAYDIR"
118  
119 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
120 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > "$1"/LAST
121 done
122 else
123 echo "No .""$EXT"" files to sort"
124 fi
125  
126 EXT="aux"
127 ls -f *."$EXT" > /dev/null 2>&1
128 if [ "$?" -eq 0 ]; then
129 for i in *."$EXT"; do
130 echo "processing " $i
131 OBSERVATORY=`echo $i | cut -d $DELIM -f2`
132 OBSERVATORY=`echo "$OBSERVATORY" | cut -d "." -f1`
133  
134 TIMESTAMP=`echo "$i" | cut -d $DELIM -f1`
135 YEAR=`echo "$TIMESTAMP" | cut -c 1-4`
136 MONTH=`echo "$TIMESTAMP" | cut -c 5-6`
137 DAY=`echo "$TIMESTAMP" | cut -c 7-8`
138 HOUR=`echo "$TIMESTAMP" | cut -c 9-10`
139  
140 # observatory / year / month / day / hour
141 DAYDIR="$YEAR$SLASH$MONTH$SLASH$DAY"
142  
143 # create directory with observatory name, year, month and day if hasn't existed before
144 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
145  
146 # check if directory really exists (if fs is full it might not be created)
147 [[ -d "$DAYDIR" ]] && mv "$i" "$DAYDIR"
148  
149 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
150 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > "$1"/LAST
151 done
152 else
153 echo "No .""$EXT"" files to sort"
154 fi
155  
156 #metadata for Bolidozor
157 cd "$1"/data
158  
159 EXT="dat"
160 ls -f *."$EXT" > /dev/null 2>&1
161 if [ "$?" -eq 0 ]; then
162 for i in $(find *."$EXT" -maxdepth 1 -mmin +59); do
163 echo "processing " $i
164 OBSERVATORY=`echo $i | cut -d $DELIM -f2`
165 OBSERVATORY=`echo "$OBSERVATORY" | cut -d "." -f1`
166  
167 TIMESTAMP=`echo "$i" | cut -d $DELIM -f1`
168 YEAR=`echo "$TIMESTAMP" | cut -c 1-4`
169 MONTH=`echo "$TIMESTAMP" | cut -c 5-6`
170 DAY=`echo "$TIMESTAMP" | cut -c 7-8`
171 HOUR=`echo "$TIMESTAMP" | cut -c 9-10`
172  
173 # observatory / year / month / day / hour
174 DAYDIR="$YEAR$SLASH$MONTH$SLASH$DAY"
175  
176 # create directory with observatory name, year, month and day if hasn't existed before
177 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
178  
179 # check if directory really exists (if fs is full it might not be created)
180 [[ -d "$DAYDIR" ]] && mv "$i" "$DAYDIR"
181  
182 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
183 echo -n "$YEAR$SLASH$MONTH$SLASH$DAY" > "$1"/LAST
184 done
185 else
186 echo "No .""$EXT"" files to sort"
187 fi
188  
189  
190 exit 0
191  
192  
193