I know df -h
and pwd
, but it seems a little complex for the regex matching part. Any ideas?
4 Answers
The output can be made a bit easier to parse by using the -P
option which will ensure that:
- 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}'
– SiegeXJan 18, 2011 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 DJan 18, 2011 at 4:41 -
-P
is standard (theP
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
). Jan 18, 2011 at 22:55 -
2You can do this without out by using the --output=avail switch:
df -h --output=avail . | tail -1
– FloydDec 11, 2017 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 - invokingtail
isn't expensive andawk
will be spared to skip to the end of the input... There might have been a difference on the PDP-11... Dec 28, 2018 at 17:43
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% /
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
You can simply do it like this:
df -h $PWD | awk 'NR==2{print $4}'
Command Interpretation:
df
: (disk free) is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access.
-h
: to show the output in a human-readable format i.e. megabytes, gigabytes, and so on.
$PWD
: Invoke the command in the current directory.
|
: pass the output of the leftmost command to the rightmost one.
awk
: is a utility that enables a programmer to scan each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f file.
'NR==2
: Get the second row that contains the space numeric value, not the header -NR==1-.
'{print $4}': Get the 4th column which contains the available space.
du -h
?