-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfseg.sh
More file actions
executable file
·151 lines (125 loc) · 3.06 KB
/
fseg.sh
File metadata and controls
executable file
·151 lines (125 loc) · 3.06 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
function generate_dir {
# generate list of directories by files modification date
# and move/copy them right away
#
# $new_dir assumes a value according to stdin
# if $2 isn't exist, then by default
# get as a value, current directory.
# $dir name of current directory.
# Read form the stdin (check the main section)
# $files Stores list of files to segregate (with the path of each)
if [ $# -eq 2 ]; then
new_dir=$2
else
new_dir=$dir
fi
if [ setting_reccur = true ]; then
files=`find $dir/* -type f`
else
files=`find $dir/* -maxdepth 0 -type f`
fi
for file in $files
do
mdate_year=`date +%Y -r $file`
mdate_month=`date +%m -r $file`
if [ $setting_day = true ]; then
mdate_day=`date +%d -r $file`
else
mdate_day=''
fi
if [ $setting_backup = true ]; then
echo $mdate_year >> files_to_backup
fi
path=$new_dir/$mdate_year/$mdate_month/$mdate_day
if [ -d $path ]; then
$setting_action $file $path
else
mkdir -p $path
$setting_action $file $path
fi
done
}
function backup {
# $new_dir [ check in generate_dir ]
name='backup_'`date +%F`
b_file=`cat files_to_backup | uniq`
rm files_to_backup
cd $new_dir
tar -cvzf $name.tar.gz $b_file
}
function help {
echo
echo 'Skladnia: `fseg.sh [KATALOG_ZRODLO] [KATALOG_DOCELOWY]`'
echo 'Jeśli nie podany [KATALOG_DOCELOWY], to segreguje wewnątrz [KATALOG_ZRODLO]'
echo
echo 'Argumenty nieobowiązkowe, wyłącznie krótkie opcje:'
echo ' -b spakowanie backupu posegregowanego katalogu (nazwa tworzona automatycznie)'
echo ' -d zawęża datę segregacji do dnia ostatniej modyfikacji pliku'
echo ' -m przenosi segregowane pliki przez `mv`'
echo ' -r uwzględnia podkatalogi podczas segregacji'
echo
}
function error {
# $code store the number of error
# $option store the name of option (only in 405 err)
code=$1
option=$2
case $code in
0) echo $0': brak argumentow.'
echo 'Spróbuj `fseg.sh --help` dla uzyskania informacji.'
;;
401) echo $0': Brak dostępu do pliku lub katalogu ';;
403) echo $0': Podany plik nie jest katalogiem';;
404) echo $0': Podany plik '$dir' nie istnieje';;
405) echo $0': '$option': Nieznana opcja '
echo 'Spróbuj `fseg.sh -- help` dla uzyskania informacji.'
;;
*) echo 'We like trains';;
esac
exit 0
}
#
# main()
# ------------------------------------------
#
if [ $# -eq 0 ]; then
error 0
else
setting_backup=false
setting_action='cp'
setting_day=false
setting_reccur=false
while [ $# -gt 0 ]; do # options
case $1 in
-b) setting_backup=true;;
-m) setting_action='mv';;
-d) setting_day=true;;
-r) setting_reccur=true;;
--help) help;;
--) shift
break
;;
-*) error 405 $1;;
*) break;;
esac
shift
done
dir=$1
if [ -e $dir ]; then # File exist?
if [ -d $dir ]; then # File is a directory?
if [ -r $dir ]; then # Can we read file?
generate_dir $dir $2 # we can do it, finally!
if [ $setting_backup = true ]; then
backup
fi
else
error 401
fi
else
error 403
fi
else
error 404
fi
fi