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