3

I've been looking around and I found out that you can find your disk space with df -hT. So I did use it to get the disk space (in total) and how much is left. But the thing is, I wonder if there are any other ways to get the information? The code I copied here, will give total disk space in GB's (I added the B in the end) with awk as you can see, also cut it with awk.

This might be too messy for some of you (I'm still learning bash), so if you have any recommendations, then feel free to give them to me. Remember that I am looking for options that work in every server/machine, without software that has to be downloaded with apt-get.

df -hT /home | awk '{print $3}' | awk 'FNR == 2 {print $1 "\B"}'
awk: cmd. line:1: warning: escape sequence `\B' treated as plain `B'
912GB

Also no clue how to get rid of the awk message. This might seem a bit weird, but I have to start from somewhere!

  • The awk message comes from your \B string. backslash means that the following character means something different - "n" becomes a line feed, "t" becomes a tab character etc. There is no such meaning for B hence the message. – Thorbjørn Ravn Andersen Mar 26 '18 at 14:04
1

Removing the backslash works for me:

df -hT /home | awk '{print $3}' | awk 'FNR == 2 {print $1 "B"}'

and can be simplified to

df -hT /home | awk 'FNR == 2 {print $3 "B"}'
1

To answer your question regarding other ways to get the info, coreutils stat has the ability to query for filesystem information (the underlying syscall is statfs), using option -f. This can give more fine-grained information than plain df. For example (see stat manpage for full list of format specifiers):

$ stat -f /tmp --printf='{"block-size": %S, "total-blocks": %b, "free-blocks-users": %a, "free-blocks-root": %f}\n'
{"block-size": 4096, "total-blocks": 7559140, "free-blocks-users": 1693162, "free-blocks-root": 2077148}

A perhaps more relevant example, this will print the file system size in blocks:

$ stat -f /tmp --printf '%b\n'
7559140

Or, to compute bytes, multiply by the block size (here, using GNU dc for the numerical computation):

$ stat -f /tmp --printf '%S %b *p' | dc
30962237440

Or, to compute the size in gigabytes with two fractional digits and append the string "GB":

$ stat -f /tmp --printf '%S %b * 2k 1024 dd**/n [GB]p' | dc
28.83GB

Depending on the programming language you would like to use for this task, a direct wrapper to statfs may be available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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