Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want from the output below grab all the volume names that have a capacity over 85%. I know that there isn't one in ths example but lets pretend there is. :)

Filesystem               total       used      avail capacity  Mounted on
/vol/mc3240a/            190GB       18GB      171GB      10%  /vol/mc3240a/
/vol/mc3240a/.snapshot       10GB      355MB     9884MB       3%  /vol/mc3240a/.snapshot
/vol/mc3240b_root_backup/      210GB     9019MB      201GB       4%  /vol/mc3240b_root_backup/
/vol/mc3240b_root_backup/.snapshot        0MB     1691MB        0MB     ---%  /vol/mc3240b_root_backup/.snapshot
share|improve this question
    
What have you tried? –  Wilf Feb 18 at 12:46
    
To clarify, I want to grab the name, mc3240a, not the entire row –  Jocke Feb 18 at 12:51
    
errr... I would use df -hx tmpfs -x devtmpfs or something as the input... –  Wilf Feb 18 at 12:58

4 Answers 4

up vote 2 down vote accepted
df -h | egrep "(8[5-9]|9[0-9]|100)%" | cut -f 1 -d " "
  • This will catch 85% up to 100% (the 0, does the {1}00%)
  • The cut will show the 1st column (anything after the 1st space in the output is deleted; a device can not have a space)

Examples:

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             139G  132G  218M 100% /
varrun                2.0G  368K  2.0G   1% /var/run
varlock               2.0G     0  2.0G   0% /var/lock
udev                  2.0G   48K  2.0G   1% /dev
devshm                2.0G     0  2.0G   0% /dev/shm

$ df -h | egrep "(8[5-9]|9[0-9]|100)%" | cut -f 1 -d " "
/dev/sda1

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             144G  130G  7.5G  95% /
varrun                506M  148K  506M   1% /var/run
varlock               506M  4.0K  506M   1% /var/lock
udev                  506M   72K  506M   1% /dev
devshm                506M     0  506M   0% /dev/shm
$ df -h | egrep "(8[5-9]|9[0-9]|100)%" | cut -f 1 -d " "
/dev/sda1
share|improve this answer
    
but it does not catch 90...?! –  Wolfgang Vogl Feb 18 at 13:05
1  
but it catchs 81%, too –  Wolfgang Vogl Feb 18 at 13:13
    
now, after editing this will do the job :D –  Wolfgang Vogl Feb 18 at 13:32
    
@WolfgangVogl we almost got 3x the same answer now :D funny –  Rinzwind Feb 18 at 13:34
    
Thx, this helped alot @Rinzwind . made a little modification ssh root@hostname 'df -h' | egrep "(8[5-9]|9[0-9]|100)%" | cut -f 3 -d "/" –  Jocke Feb 18 at 13:51

Try this (put it into a script, make it executable):

#!/bin/bash

DF_BIN='/bin/df'

DF_OUT=`${DF_BIN}`

tring='first line
second line
third line'

oldIFS="$IFS"   # store old IFS value
IFS='
'
IFS=${IFS:0:1} # tab formatting
lines=( $DF_OUT )

IFS=', '

COUNTER=0
for line in "${lines[@]}"
    do
        if [  $COUNTER -gt 0 ]; then    # strip first line
            fields=( $line )
            if [[ ${fields[4]} =~ (8[5-9]|[9][0-9]|100)%$ ]]; then
                echo "==> $line"
            fi
        fi
        let COUNTER=COUNTER+1 
done

IFS="$oldIFS"

exit 0

Not as short as the answers above but it catches 85% to 100% in the right row...

or: SHORT one-line:

df -hx tmpfs -x devtmpfs | egrep '(8[5-9]|9[0-9]|100)%'

(edited Wilfs version) just like the other's solution but using egrep with alternatives)

share|improve this answer
    
Damn - I thought I could of done it as one-liner... Brilliant +1. –  Wilf Feb 18 at 13:12
1  
I like the one liner but I expect you need to cut the output to only show the device ;) –  Rinzwind Feb 18 at 13:16
    
What about df -hx tmpfs -x devtmpfs | egrep '(8[5-9]|9[0-9]|100)%' | cut -f 1 -d " " | sed -e 's/\/vol\///g'? –  Wilf Feb 18 at 13:26

Like the other ones, but in a script to make sure it is all grabbed:

#!/bin/bash
#For 85% to 89%, and 95& to 99%
df -hx tmpfs -x devtmpfs | egrep '[8-9][5-9]%'
#To find if anything is 90%-94%
df -hx tmpfs -x devtmpfs | egrep '9[0-4]%'
#To find if anything is 100%
df -hx tmpfs -x devtmpfs | egrep '100%'

Sample input (tmpfs etc filtered out):

wilf@comp:~$ df -hx tmpfs -x devtmpfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        96G   21G   71G  23% /
/dev/sda3        58G   43G   12G  79% /home
/dev/sda5       357G  335G  4.2G  99% /run/media/wilf/Storage
/dev/sda6        27G   19G  6.8G  74% /run/media/wilf/433b971a

Sample output - with drive name with other stuff as I have not filtered that out:

wilf@comp:~$ ./testScript
/dev/sda5       357G  335G  4.2G  99% /run/media/wilf/Storage

Based @wolfgang 's , this should should print out just the drive name without the /vol:

df -hx tmpfs -x devtmpfs | egrep '(8[5-9]|9[0-9]|100)%' | cut -f 1 -d " " | sed -e 's/\/vol\///g'
share|improve this answer
    
yep, simplest (and i think best) is yours @Wilf - just use the alternative selection and all's done! –  Wolfgang Vogl Feb 18 at 13:15
    
@WolfgangVogl - Your one-liner is better :D –  Wilf Feb 18 at 13:15
    
@wilf in the end we all end up with the same line. Now what :D :D –  Rinzwind Feb 18 at 13:19
    
@Rinzwind - Dunno :) –  Wilf Feb 18 at 13:20
1  
@wilf you are forgetting to cut the result.OP wants device only ;) –  Rinzwind Feb 18 at 13:20

Suppose you are using commnad say xyz to get this output then

xyz | grep "[8-9][5-9]%"

should give you proper output

share|improve this answer
    
To clarify, I want to grab the name, mc3240a, not the entire row –  Jocke Feb 18 at 12:51
2  
That will be little tricky, I think... –  Null pointer Feb 18 at 12:54
    
I may be wrong, but wouldn't those not list numbers like 92%, as the second number is below 5? Same for @Rinzwind –  Wilf Feb 18 at 12:59
    
think we got enough correct answers - but this one gives not 90 to 94 and 100 as you already told Wilf... –  Wolfgang Vogl Feb 18 at 13:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.