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
69 [[ "$?" -ne 0 ]] && exit 1
70  
71 for i in *.$EXT; do
72 echo "processing " $i
73 PREFIX=`echo $i | cut -d $DELIM -f1,2`
74 OBSERVATORY=`echo $PREFIX | cut -d $DELIM -f2`
75 POSTFIX=`echo $i | cut -d $DELIM -f4`
76  
77 TIMESTAMP=`echo "$i" | cut -d $DELIM -f3`
78 YEAR=20`echo "$TIMESTAMP" | cut -c 1-2`
79 MONTH=`echo "$TIMESTAMP" | cut -c 3-4`
80 DAY=`echo "$TIMESTAMP" | cut -c 5-6`
81 HOUR=`echo "$POSTFIX" | cut -c 1-2`
82  
83 # observatory / year / month / day / hour
84 DAYDIR="$OBSERVATORY$SLASH$YEAR$SLASH$MONTH$SLASH$DAY$SLASH$HOUR"
85  
86 # create directory with observatory name, year, month and day if hasn't existed before
87 [[ -d "$DAYDIR" ]] || mkdir -p "$DAYDIR"
88  
89 mv "$i" "$DAYDIR"
90 done
91  
92 echo -n "$OBSERVATORY$SLASH$YEAR$SLASH$MONTH$SLASH$DAY" > LAST
93  
94 exit 0
95  
96