Display filesystem's free space available to the root user

35,094

Solution 1

I am not sure there is a tool built in, but assuming you have left the reserved at the default 5% then this will tell you:

df / | grep dev | cut -f 3,6 -d\  | awk '{print ($1*.05)+$2}'

df the root, grep for the line with dev in it (to remove the header), cut the size and available fields, then use an awk script to calculate 5% of the disk size added to the available.

You could pull the actual reservation from tune2fs -l <device> and combine this with the above in a script.

Solution 2

By using the command tune2fs (found in /sbin/tune2fs), you can easily determine the reserved space: (and more!)

tune2fs -l /dev/sda1

I'll provide my system's info for reference, I'm going to remove extraneous lines not important to this question:

The header... and volume name, I label all my drives, makes them easy to identify if needed.

tune2fs 1.42.4 (12-Jun-2012)
Filesystem volume name:   xenon
Last mounted on:          /
...

REALLY want this to say "clean" while the system is running. Honest!

Filesystem state:         clean

This is where the data storage capacity information begins:

Here you can see that I have 121,179,648 blocks total... with a block size of 4K (4096), that multiplies out to some big number (462-ish GB). (Block size is noted below)

Block count:              121179648

And the reserved blocks... by looking at the number above, and the number below.. you should be able to relatively quickly figure out I have 1% reserved. In this case (4.62-ish GB)

Reserved block count:     1211796

How much free space currently available? Right here!

Free blocks:              104090586
...

And the all important block size. Useful for multiplying.

Block size:               4096
...

These lines say WHO the blocks are reserved for... user 0, root, in this case

Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
...

There's lots more information available here, but this should give you an ability to quickly ascertain how much is available, and how much more is reserved for root. Simple math.

Hope this helps. Remember...man pages are your friends.

Solution 3

This displays free space in bytes in partition related to "/path"

printf '%s' $(($(stat -f --format='%f*%S' /path)))

You do not have to be superuser to run it.

BTW I do not quite understand what is the difference between

%s block size (for faster transfers)

%S fundamental block size (for block counts)

in usage of stat.

Share:
35,094

Related videos on Youtube

Mechanical snail
Author by

Mechanical snail

🐌. Native speaker of American English. Linux user. Familiar with several programming languages in the procedural, OO, and functional paradigms. Worst Code Golf ever New badge proposals! Shakespeare bug in Ubuntu The Great Question Deletion Audit of 2012 Chat Horrible spiders Adorable fluffy things LASERS! Link rot is evil. Archive everything. The keyboard is king. Correctness over performance. Canonicalize, normalize, deduplicate. Don't repeat yourself. UTF-8 &gt; UTF-16. Use static typing: good for tooling. Re-use; don't re-invent. Correctness, then clarity, then concision and elegance. Play devil's advocate. First understand opponents' positions.

Updated on September 18, 2022

Comments

  • Mechanical snail
    Mechanical snail over 1 year

    By default, Linux reserves some fraction of a filesystem for the root user, at least on ext4 (to prevent ordinary users from filling the drive completely and crashing the system, and to reduce fragmentation). However, df only displays the free space apparent to regular users (even when run as root). How do you display the "real" free space, that would be accessible to root?

  • tuk0z
    tuk0z almost 8 years
    Right, so the following command will tell us how much of the partition space is reserved for privilegied users: tune2fs -l /dev/DEVICE | egrep "Block count|Reserved block count". E.g. for my "multimedia buffer" partition : Block count: 2621440 and Reserved block count: 128449 : 4.9% of the available blocks (conservative setting to help prevent fragmentation).
  • jarno
    jarno about 7 years
    There is no need to use the cut command: you can pick the fields by awk directly. In fact the cut command did not work for me as expected. Anyway, I gave another answer that should give more precise answer.
  • Paul
    Paul about 7 years
    Cool - does this account for the space reserved for root?
  • jarno
    jarno about 7 years
    @Paul yes, it is included
  • Paul
    Paul about 7 years
    Your output is effectively a sum - so you could pipe the output into bc to get the byte count, or even use --format='%f*%S/1024/1024/1024' | bc to get kilobytes / megabytes / gigabytes depending on how many divisions you choose.
  • jarno
    jarno about 7 years
    @Paul Well, I'd call it a product. The binary prefixes are called kibibytes, mebibytes and gibibytes, respectively. The surrounding arithmetic expansion $(( ... )) does the calculation, but it results only the integer part of the quotient (if you add division). Or do you mean your shell does not support arithmetic expansion?
  • jarno
    jarno about 7 years
    @Paul But, if you use bc, instead, you could get the quotient with arbitrary precision, if you need that.
  • Paul
    Paul about 7 years
    Ooops, crap you are right, I missed the expansion!
  • luka5z
    luka5z about 7 years
    Execute tune2fs -m <percentage> <device-name> to change disk space reserved for only for root usage.
  • mp3foley
    mp3foley over 5 years
    Inspired by the above I came up with this to calculate reserved space on root volume in MB: sudo tune2fs -l $(df | grep -E '/$' | cut -d\ -f 1) | egrep "Reserved block count|Block size" | paste -sd\ | awk '{print ($4 * $7 / ( 1024 * 1024 ) ), "MB"}'
  • trs
    trs almost 5 years
    This is not a portable answer. Check below for the answer you need (spoiler: tune2fs /dev/sda1 or even better stat -f -c '%a blocks free and %f blocks free for root (%S bytes per block)' /
  • trs
    trs almost 5 years
    This is great. Thanks. Have been looking for this for a very long time. THIS should be the accepted answer as it is portable and easy to use.
  • Martin von Wittich
    Martin von Wittich about 4 years
    As percentage: stat -f -c "(%b-%f)*100/%b" / | bc
  • jarno
    jarno about 4 years
    @MartinvonWittich that is used space as percentage.
  • Martin von Wittich
    Martin von Wittich about 4 years
    @jarno yes, same as df
  • Pedro Vernetti
    Pedro Vernetti about 3 years
    it is 2021 and I must say this command doesn't work for me. It was telling me there were 0 bytes/blocks reserved on my external ext4 drive, even though I was indirectly "seeing" the reserved space (50GB) everywhere else. But the command sudo tune2fs -m 0 /dev/sd... did work to zero the reserved space.
  • binarym
    binarym over 2 years
    Same here, i've been unable to correlate (free_blocks - reserved_blocks) * block_size with the effective free space on my device. Example on my /tmp/ partition: ( 202493 - 24371 ) * 4096 = 729587712 bytes i guess... Using df -B 1 /tmp, i get 1511116800 ... i tried several different ways, i never found something satisfying using tune2fs... that's sad.