-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversions.sh
More file actions
executable file
·109 lines (95 loc) · 2.68 KB
/
versions.sh
File metadata and controls
executable file
·109 lines (95 loc) · 2.68 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
#!/usr/bin/env bash
set -eu
usage() {
cat << EOT >&2
$(basename $0): Get/set version ("vYYYYMMDD" string) for CMORised data by
listing version directories (get) or moving files (set) to the appropriate
directory.
If needed, a new version directory is created. Empty version directories are
removed. When moving files would overwrite existing files, the user is
prompted.
Note that the script has a safety switch (-m)! If the switch is *not* used on
the command line, it runs in dry-run mode (i.e. no files are moved).
Usage: $(basename $0) -l | -v <version> [-m] DIR
-l List versions present in DIR
-v <version> Set version to be used for all files
<version> must match: v20[0-9][0-9][01][0-9][0-9][0-9]
-m Safety switch: actually move files (dry-run if not set)
EOT
}
error() {
echo "ERROR: $1" >&2
[ -z ${2+x} ] && exit 99
exit $2
}
warning() {
echo "WARNING: $1" >&2
}
while getopts ":lmv:" opt
do
case "$opt" in
l) list=1
;;
m) move=1
;;
v) version=$OPTARG
;;
\?) usage
error "Invalid option: -$OPTARG" 1
;;
:) error "Option -$OPTARG requires an argument." 2
;;
esac
done
shift $((OPTIND-1))
if [ -z ${version+x} ] && [ -z ${list+x} ]
then
usage
error "Either -l or -v must be used" 3
fi
if [ -z ${1+x} ] || [ ! -d $1 ]
then
usage
set +u
error "Missing or invalid directory: '$1'" 4
else
directory=$1
fi
if [ ! -z ${list+x} ]
then
for d in $(find $directory -type d -name "v20[0-9][0-9][01][0-9][0-9][0-9]")
do
basename $d
done | sort -u
exit 0
fi
if [ ! -z ${version+x} ]
then
[ -z ${move+x} ] && warning "Not moving any files (dry-run mode)"
[[ $version =~ v20[0-9][0-9][01][0-9]{3}$ ]] || error "Invalid version string '$version'" 5
for d in $(find $directory -type d -name "v20[0-9][0-9][01][0-9][0-9][0-9]")
do
this_version=$(basename $d)
if [[ ! $this_version =~ $version ]]
then
echo "Processing directory '$d'"
new_version_dir="$(dirname $d)/$version/"
for f in $d/*.nc
do
if [ -z ${move+x} ]
then
echo "Moving: $f --> $new_version_dir"
else
[ -d $new_version_dir ] || mkdir --verbose $new_version_dir
mv --interactive --verbose $f $new_version_dir
fi
done
if [ -z ${move+x} ]
then
echo "Removing directory: $d"
else
rmdir --verbose $d || warning "Directory not empty: '$d'"
fi
fi
done
fi