-
Notifications
You must be signed in to change notification settings - Fork 1
/
logdirsizes
executable file
·64 lines (53 loc) · 1.35 KB
/
logdirsizes
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
#!/usr/bin/env bash
set -eu
SCRIPT=$(readlink -m $(type -p $0))
SCRIPTDIR=$(dirname $SCRIPT)
usage() {
cat <<EOF
Usage:
$(basename $0) <dir> [depth]
EOF
}
logfilename() {
local dir=$1
local depth=$2
local logdir=$SCRIPTDIR/_data/logdirsizes
local datestamp=$(date +"%Y%m%d")
local prefix=$(echo $dir | sed 's,\/,_,g' | sed 's,_$,,' | sed 's,^_,,')
local logfile="${prefix}-dirsizes-$depth-${datestamp}.csv"
mkdir -p $logdir
echo "$logdir/$logfile"
}
[[ $# -lt 1 || $1 == "-h" || $1 == "--help" ]] && { usage; exit 1; }
depth=3
[ -n "${2-}" ] && depth=$2
remote=false
IFS=":" read server remotepath <<< "$1"
if [ -n "${remotepath-}" ]; then
remote=true
dir=$(ssh $server "readlink -f $remotepath")
else
dir=$(readlink -f "$1")
fi
read -d '' cmd << EOF || true
du --time -b --max-depth $depth $dir |
awk '{ print strftime("%Y-%m-%d %H:%M:%S"),\$1,\$1/1024/1024/1024,\$4,\$2" "\$3 }' OFS='|' |
head -n -1
EOF
echo $cmd
if $remote; then
results=$(ssh $server "$cmd")
else
results=$(eval "$cmd")
fi
echo "results: "
echo $results
log=$(logfilename $dir $depth)
if [ -n "${results-}" ]; then
echo "Datetime, Size, SizeG, Directory, Last Modified" > $log
echo -e "$results" | python $SCRIPTDIR/dsv2csv.py >> $log
echo "Made $log"
else
>&2 echo "No output found for $dir"
touch $log
fi