-
Notifications
You must be signed in to change notification settings - Fork 3
/
eosdu
executable file
·132 lines (118 loc) · 3.06 KB
/
eosdu
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
#!/bin/bash -e
#
# eosdu
#
# Created by Jesus Orduna and Kevin Pedro
#
case `uname` in
Linux) ECHO="echo -e" ;;
*) ECHO="echo" ;;
esac
# awk command to fix stupid citrine find command
FIXFIND='{lfn=$1; while(substr(lfn,1,6)!="/store"&&length(lfn)>=6) { lfn = substr(lfn,2) }; print lfn}'
# This will sum the size of all content inside the LFN and return the number in B
# or an empty string for empty directories
getSizeOf() {
DIR=$1
SERVER=$2
MATCH=$3
# default case
# 'eos ls -l path' now prints size of dirs *inside* path, so go up to dirname and select basename
if [ -z "$MATCH" ]; then
eos $SERVER ls -l $(dirname $DIR) | grep " $(basename $DIR)"'$' | awk '{print $5}'
# matching case
else
eos $SERVER ls -l $DIR | grep "$MATCH" | awk '{sum+=$5} END {print sum}'
fi
}
# This does the same, but counts number of files
getFilesOf() {
DIR=$1
SERVER=$2
MATCH=$3
# default case
if [ -z "$MATCH" ]; then
eos $SERVER find -d $DIR | awk "$FIXFIND" | xargs -d '\n' -n1 -P4 eos $SERVER ls | wc -l | awk '{sum+=$0} END {print sum}'
# matching case
else
eos $SERVER ls $DIR | grep "$MATCH" | wc -l
fi
}
printSizeOf() {
DIR=$1
SERVER=$2
MATCH=$3
# Get the size of the LFN
if [ -z "$FILES" ]; then
theSize=$(getSizeOf $DIR $SERVER $MATCH)
else
theSize=$(getFilesOf $DIR $SERVER $MATCH)
fi
# Empty directories will evaluate true
if [ "a$theSize" = "a" ] ; then
$ECHO "Empty"
else
# Non-empty directories with content adding zero-size will evaluate true
# need to be filtered as log($theSize) will complain
if [ "$theSize" -eq "0" ] ; then
$ECHO "0 B"
elif [ -z "$HUMAN" ]; then
$ECHO ${theSize}
else
# Compute an index to refer to B, kB, MB, GB, TB, PB, EB, ZB, YB
declare -a thePrefix=( [0]="" [1]="K" [2]="M" [3]="G" [4]="T" [5]="P" [6]="E" [7]="Z" [8]="Y")
#decimal for size or files
theIndex=$(awk "BEGIN {print int(log($theSize)/(3*log(10)))}")
$ECHO "$theSize $theIndex ${thePrefix[$theIndex]}" | awk '{print $1/(10^(3*$2))$3}'
fi
fi
}
usage() {
$ECHO "eosdu [options] <LFN>"
$ECHO
$ECHO "Options:"
$ECHO "-s \txrootd server name (default = root://cmseos.fnal.gov)"
$ECHO "-f \tcount number of files instead of disk usage"
$ECHO "-g \tsearch for files matching specified string within directory"
$ECHO "-h \tprint human readable sizes"
$ECHO "-r \trun eosdu for each file/directory in <LFN>"
$ECHO " \t('recursive'/'wildcard' option, like 'du *')"
exit 1
}
HUMAN=""
FILES=""
RECURSE=""
SERVER=root://cmseos.fnal.gov
MATCH=""
#check arguments
while getopts "fhrs:g:" opt; do
case "$opt" in
f) FILES=yes
;;
h) HUMAN=yes
;;
r) RECURSE=yes
;;
s) SERVER=$OPTARG
;;
g) MATCH=$OPTARG
;;
esac
done
shift $(($OPTIND - 1))
DIR=$1
if [ "$DIR" == "" ]; then
usage
fi
#"recursive" option
if [[ -n "$RECURSE" ]]; then
for i in $(eos $SERVER find -d --maxdepth 1 $DIR | awk "$FIXFIND"); do
if [[ "$i" == *"$DIR" || "$i" == *"$DIR"/ ]]; then
continue
fi
theSize=$(printSizeOf $i $SERVER $MATCH)
$ECHO "`basename $i` $theSize"
done
else
printSizeOf $DIR $SERVER $MATCH
fi