zeewagon zeewagon - 6 months ago 17
Bash Question

How to export daily disk space usage?

I am new to scripting.

Is it possible to export the disk space usage of a linux server to an excel shell(.csv) sheet daily ?
If so, what will be the scripting for that?

Answer

Use awk to parse df result:

Assuming ; is your csv separator, then:

df -h | awk 'FNR == 2 {print $2";"$3";"$4}'

gives:

24G;5.4G;18G

In a full script with the date:

#!/bin/bash

SEPARATOR=","
SIZES=`df -h | awk -v SEP="$SEPARATOR" 'FNR == 2 {print SEP$2SEP$3SEP$4}'`
echo `date +%Z-%Y-%m-%d_%H-%M-%S`"$SIZES" >> test.csv