Rev Author Line No. Line
2729 toxygen 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 # | meteor_uflu_130128_0010.jpg
18 # | meteor_uflu_130128_0011.jpg
19 # | meteor_uflu_130128_1310.jpg
20 # | meteor_uflu_130129_1112.jpg
21 # | meteor_uflu_130129_1113.jpg
22 # | .
23 # | .
24 # | .
25 #
26 # after:
27 #
28 # |
29 # |- uflu <- observatory
30 # | |- 2013 <- year
31 # | |- 01 <- month
32 # | |- 28 <- day
33 # | | |- 00 <- hour
34 # | | | |- meteor_uflu_130128_0010.jpg
35 # | | | |- meteor_uflu_130128_0011.jpg
36 # | | |
37 # | | |- 13
38 # | | |- meteor_uflu_130128_1310.jpg
39 # | |
40 # | |- 29
41 # | | |- 11
42 # | |- meteor_uflu_130129_0012.jpg
43 # | |- meteor_uflu_130129_0012.jpg
44 # .
45 # .
46 # .
47 #
48 #
49 # filename format must be as meteor_uflu_130128_0010.jpg
50  
51 EXT=jpg
52 DELIM="_"
53 SLASH="/"
54 LAST=""
55  
56  
57 # turn on debug
58 set -x
59  
60 # none or 1 argument allowed
61 [[ "$#" -ne 1 ]] && echo "Wrong number of arguments ($#)" && exit 1
62  
63 # directory in which to sort must exists
64 [[ ! -d "$1" ]] && echo "Directory doesn't exist" && exit 1
65 cd $1
66  
67 # if there are no files with $EXT extension in the directory then quit
68 ls -f *.$EXT > /dev/null 2>&1
2730 toxygen 69 if [ "$?" -eq 0 ]; then
70 for i in *.$EXT; do
71 echo "processing " $i
72 PREFIX=`echo $i | cut -d $DELIM -f1,2`
73 OBSERVATORY=`echo $PREFIX | cut -d $DELIM -f2`
74 POSTFIX=`echo $i | cut -d $DELIM -f4`
75  
76 TIMESTAMP=`echo "$i" | cut -d $DELIM -f3`
77 YEAR=20`echo "$TIMESTAMP" | cut -c 1-2`
78 MONTH=`echo "$TIMESTAMP" | cut -c 3-4`
79 DAY=`echo "$TIMESTAMP" | cut -c 5-6`
80 HOUR=`echo "$POSTFIX" | cut -c 1-2`
81  
2729 toxygen 82 # observatory / year / month / day / hour
2730 toxygen 83 DAYDIR="$OBSERVATORY$SLASH$YEAR$SLASH$MONTH$SLASH$DAY$SLASH$HOUR"
84  
2729 toxygen 85 # create directory with observatory name, year, month and day if hasn't existed before
2730 toxygen 86 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
87  
2787 toxygen 88 # check if directory really exists (if fs is full it might not be created)
89 [[ -d "$DAYDIR" ]] && mv "$i" "$DAYDIR"
2730 toxygen 90 done
2729 toxygen 91  
2730 toxygen 92 echo -n "$OBSERVATORY$SLASH$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
93 else
94 echo "No image files to sort"
95 fi
2729 toxygen 96  
2730 toxygen 97 DIR="bolids"
2729 toxygen 98  
2730 toxygen 99 ls -f *.wav > /dev/null 2>&1
100 if [ "$?" -eq 0 ]; then
101 for i in bolid_*.wav; do
102 echo "processing bolid " $i
103  
104 PREFIX=`echo $i | cut -d $DELIM -f1,2`
105 OBSERVATORY=`echo $PREFIX | cut -d $DELIM -f2`
106 TIMESTAMP=`echo "$i" | cut -d $DELIM -f3`
107 YEAR=20`echo "$TIMESTAMP" | cut -c 1-2`
108 OUTDIR="$OBSERVATORY$SLASH$DIR$SLASH$YEAR"
109  
110 [[ -d "$OUTDIR" ]] || mkdir -p "$OUTDIR"
111 mv $i "$OUTDIR"
112 done
113 else
114 echo "No wav files to sort"
115 fi
116  
117 ls -f *.aux > /dev/null 2>&1
118 if [ "$?" -eq 0 ]; then
119 for i in bolid_*.aux; do
120 echo "processing bolid metadata " $i
121  
122 PREFIX=`echo $i | cut -d $DELIM -f1,2`
123 OBSERVATORY=`echo $PREFIX | cut -d $DELIM -f2`
124 TIMESTAMP=`echo "$i" | cut -d $DELIM -f3`
125 YEAR=20`echo "$TIMESTAMP" | cut -c 1-2`
126 OUTDIR="$OBSERVATORY$SLASH$DIR$SLASH$YEAR"
127  
128 [[ -d "$OUTDIR" ]] || mkdir -p "$OUTDIR"
129 mv $i "$OUTDIR"
130 done
131 else
132 echo "No aux files to sort"
133 fi
134  
2729 toxygen 135 exit 0
136  
137  
2730 toxygen 138