Files
.local/bin/size-track

29 lines
779 B
Bash
Executable File

#!/bin/bash
DIR="$1"
LOGFILE="$2"
if [[ -z "$DIR" || -z "$LOGFILE" ]]; then
echo "Usage: $0 /path/to/directory /path/to/logfile.log"
exit 1
fi
if [[ ! -d "$DIR" ]]; then
echo "'$DIR': not found"
exit 1
fi
echo "logging size of $DIR to $LOGFILE every 5 seconds (in KB)."
echo "timestamp,directory size (KB),free space left (KB),total space (KB)" | tee -a "$LOGFILE"
while true; do
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
DIR_SIZE=$(du -sk "$DIR" 2>/dev/null | awk '{print $1}')
DF_OUTPUT=$(df -k "$DIR" | awk 'NR==2')
TOTAL_SPACE=$(echo "$DF_OUTPUT" | awk '{print $2}')
FREE_SPACE=$(echo "$DF_OUTPUT" | awk '{print $4}')
LOG_LINE="$TIMESTAMP,$DIR_SIZE,$FREE_SPACE,$TOTAL_SPACE"
echo "$LOG_LINE" | tee -a "$LOGFILE"
sleep 5
done