2

I am using 'df -h' command to get disk space details in my directory and it gives me response as below :

enter image description here

Now I want to be able to do this check automatically through some batch or script - so I am wondering, if I will be able to check disk space only for specific folders which I care about, as shown in image - I am only supposed to check for /nas/home that it does not go above 75%.

How can I achieve this ? Any help ?


My work till now:

I am using

df -h > DiskData.txt

... this outputs to a text file

grep "/nas/home" "DiskData.txt"

... which gives me the output:

*500G  254G  247G  51% /nas/home*

Now I want to be able to search for the number previous or right nearby '%' sign (51 in this case) to achieve what I want.

smci
  • 32,567
  • 20
  • 113
  • 146
dynamicJos
  • 129
  • 3
  • 14
  • Yes, you can do check. But what do you want to do if it's more than 75% ? You can write bash script and put it to crontab, so you can automate periodical check – Alex Kapustin Aug 14 '17 at 04:01
  • I want to actually send a mail that it is more than 75% but as of now an echo would do, check the updated question – dynamicJos Aug 14 '17 at 04:14

3 Answers3

5

This command will give you percentage of /nas/home directory

df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1

So basically you can use store as value in some variable and then apply if else condition.

var=`df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1`
if(var>75){
#send email
}
nagendra547
  • 5,672
  • 3
  • 29
  • 43
3

another variant:

df --output=pcent /nas/home | tail -n 1 | tr -d '[:space:]|%'

output=pcent - show only percent value (for coreutils => 8.21 )

beliy
  • 445
  • 6
  • 13
1

A more concise way without extensive piping could be:

df -h /nas/home | perl -ane 'print substr $F[3],0,-1 if $.==2'

Returns: 51 for your example.

reflective_mind
  • 1,475
  • 3
  • 15
  • 28