Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I grep or cut the "173G" under "Verf"?

I need this for Unix scripting in school.

jonas@jonaspc:~/$ df -h /dev/sda2
Dateisystem    Größe Benutzt Verf. Verw% Eingehängt auf
/dev/sda2       293G    121G  173G   42% /media/Windows
share|improve this question
add comment

4 Answers 4

up vote 5 down vote accepted

The most comfortable solution for such task is awk:

df -h /dev/sda2 | awk 'NR==2{print$4}'

Or if more partitions are listed, you can pick the right line by the mount point:

df -h | awk '$1=="/dev/sda2"{print$4}'

Is also simple with sed, but less nice if you need to debug it a few mounts later :

df -h /dev/sda2 | sed -rn '2s/^((\S+)\s+){4}.*/\2/p'

df -h | sed -rn '/^\/dev\/sda2/s/^((\S+)\s+){4}.*/\2/p'

That supposes GNU sed. POSIX compatible syntax includes many escaping:

df -h /dev/sda2 | sed -n '2s/^\(\(\S\+\)\s\+\)\{4\}.*/\2/p'

df -h | sed -n '/^\/dev\/sda2/s/^\(\(\S\+\)\s\+\)\{4\}.*/\2/p'
share|improve this answer
add comment

If you must use grep and cut, you can do the following:

df -h /dev/sda2 | grep sda | cut -d" " -f14

But this is pretty ugly, since you have to count the spaces (-f14) and the reason @manatwork used awk. You could use tr or other tools to make it nicer and be able to specify the real field you want for cut:

df -h /dev/sda2 | grep sda | tr -s " " " " | cut -d" " -f4

Alternatively, newer grep supports outputing only a partial match and you can use that in combination with a bash trick:

df -h /dev/sda2 | grep -o '[^[:space:]]*G' | cut -d$'\n' -f3

The regex could be safer, but this will find all the size fields and output them one per line, while cut selects the second.

share|improve this answer
1  
A minor point: you only need tr -s " ", unless you want to change the remaining space to something else.... (+1), but -f2 should be -f3 –  Peter.O Jul 1 '12 at 16:31
    
I looked up the wrong field in my tests, thanks. –  lynxlynxlynx Jul 1 '12 at 16:40
add comment

All other answers here are great, but if you use Bash, there is no need to use external programs such as awk, sed, grep, cut etc.

The following line will do what you want:

{ read; read -r _ _ _ av _; } < <(df -h /dev/sda1)

"$av" will be the available size on /dev/sda1

If you want to use only the numeric part of "$av" ( e.g 123 in 123G ), you can use Parameter Expansion to trim the irrelevant part like that:

ir="${av##*[0-9]}"; echo "${av%$ir}"

or just "${av%?}" if you are sure that the suffix of "$av" is only one letter.

share|improve this answer
    
+1 for creativity. –  paraxor Apr 18 '13 at 23:30
add comment

If it doesn't have to be grep/cut, you may be able to get the same information using stat. In filesystem mode, %a is number of free blocks and %s blocksize, so %a * %s is the free space in bytes, and you can do math to your liking with it to break it down to a more suitable unit of your choice, for example:

echo $((`stat -f -c "%a*%s" /media/windows/`/1024/1024/1024))GiB
share|improve this answer
add comment

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.