Cutting disk space output

Running this code

df -h | head -2 | awk '{print $8}' 

Gives me the following output:

%iused
6%

What I'm trying to do is get the 6% but I'm having trouble doing this using cut -c, I think that this could be because the text is on different lines; is there a way of doing this?

What's wrong with cut -c ? What are you unhappy with? And why not use the code you posted?

Hi RudiC,

I've just figured it out, I've combined the command with tail -1 to the cut the top line.

df -h | head -2 | awk '{print $8}' | tail -1

Thank you for your reply though. :slight_smile:

You have asked for the 8th field from the first two lines of output and that is what you have been given.

If you show us the whole output from df -h and highlight the information you want to extract then we can consider the best way.

Please post the output in CODE tags to keep it clear and preserve multiple spaces.

Robin

df -h | head -2 | awk 'NR==2{print $8;exit}'
1 Like

Just noticed...

You don't need head -2

Assuming that line 2 is always / the following is more efficient

df -h / | awk 'NR>1 {print $8}'

For comparison against thresholds the {print $8+0} returns the pure number.

1 Like

If you are using bash or something similar you could try something like:-
(OSX 10.7.5, defaul bash terminal.)

Last login: Wed Aug 31 08:52:38 on ttys000
AMIGA:barrywalker~> root_percent_used=( $( df -h ) )
AMIGA:barrywalker~> echo "${root_percent_used[11]}"
4%
AMIGA:barrywalker~> _

Note that the array field [11] might not be the same for your platform so adjust to suit.
Just an idea...