25

I know df -h and pwd, but it seems a little complex for the regex matching part. Any ideas?

  • 2
    Did you mean du -h? – jweyrich Jan 18 '11 at 7:11
45

The output can be made a bit easier to parse by using the -P option which will ensure that:

  1. The information about each file system is always printed on exactly one line; a mount device is never put on a line by itself. This means that if the mount device name is more than 20 characters long (e.g., for some network mounts), the columns are misaligned.

This makes it much easier to get just the free space available:

$ df -Ph . | tail -1 | awk '{print $4}'

(-h uses megabytes, gigabytes and so on. If your system doesn't have it, use -k for kilobytes only.)

If we pass df a path, it is only going to return 2 rows: a header row and then the data about the file system that contains the path. We can use tail to grab just the second row. We know that the space available is in the 4th column, so we grab that with awk. This all could be done with awk:

$ df -Ph . | awk 'NR==2 {print $4}'

or many other sets of filters.

  • You can optimize your command by using the environment variable $PWD instead of using command substitution and removing the need for tail: df -Ph $PWD | awk 'NR==2{print $4}' – SiegeX Jan 18 '11 at 4:17
  • @SiegeX: I agree doing it all in awk is better. I included the longer command because I wanted to have an example of how one can construct a chain of simple commands to do something more complicated. I've updated the answer to include your improvement. – Steven D Jan 18 '11 at 4:41
  • -P is standard (the P is for POSIX). But -h is a GNU extension (also existing on some other systems, but e.g. on OpenBSD it's incompatible with -P). – Gilles 'SO- stop being evil' Jan 18 '11 at 22:55
  • You can do this without out by using the --output=avail switch: df -h --output=avail . | tail -1 – Floyd Dec 11 '17 at 8:14
  • "You can optimize your command". Not really. Running both pipes 10'000 times (as in time { I=0; while [ $I -lt 10000 ]; do df -Ph . | awk 'NR==2 {print $4}' > /dev/null; ((I++)); done }) reveals no meaningful difference in speed, which is not astonishing - invoking tail isn't expensive and awk will be spared to skip to the end of the input... There might have been a difference on the PDP-11... – David Tonhofer Dec 28 '18 at 17:43
10

How about doing df -h .. This will give you the available free space of the partition your current working directory is in.

A small example:

 /usr/local/nagios/libexec # df -h .
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/mapper/vg00-lvol1
                       9.9G  6.1G  3.4G  65% /
4

In bytes:

df --output=avail -B 1 "$PWD" | tail -n 1

Human readable:

df --output=avail -h "$PWD" | tail -n 1

or

df --output=avail -B 1 "$PWD" |tail -n 1 | numfmt --to="iec"

or

df --output=avail -B 1 "$PWD" |tail -n 1 | numfmt --grouping

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.