Classic Shell Scripting - Arnold Robbins [160]
* * *
df
Usage
df [ options ] [ files-or-directories ]
Purpose
Show the inode or space usage in one or more filesystems.
Major options
-i
Show inode counts rather than space.
-k
Show space in kilobytes rather than blocks.
-l
Lowercase L. Show only local filesystems.
Behavior
For each file or directory argument, or for all filesystems if there are no such arguments, df produces a one-line header that identifies the output columns, followed by a usage report for the filesystem containing that file or directory.
Caveats
The output of df varies considerably between systems, making it hard to use reliably in portable shell scripts.
df's output is not sorted.
Space reports for remote filesystems may be inaccurate.
Reports represent only a single snapshot that might be quite different a short time later in an active multiuser system.
* * *
You can supply a list of one or more filesystem names or mount points to limit the output to just those:
$ df -lk /dev/sda6 /var
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 4032092 1684660 2142608 45% /ww
/dev/sda9 13432904 269704 12480844 3% /var
For network-mounted filesystems, entries in the Filesystem column are prefixed by hostname:, making the column wide enough that some df implementations split the display into two lines, which is a nuisance for other software that parses the output. Here's an example from a Sun Solaris system:
$ df
Filesystem 1k-blocks Used Available Use% Mounted on
...
/dev/sdd1 17496684 15220472 1387420 92% /export/local
fs:/export/home/0075
35197586 33528481 1317130 97% /a/fs/export/home/0075
...
df's reports about the free space on remote filesystems may be inaccurate, because of software implementation inconsistencies in accounting for the space reserved for emergency use.
In Section B.4.3 in Appendix B, we discuss the issue that the inode table in a filesystem has an immutable size that is set when the filesystem is created. The -i (inode units) option provides a way to assess inode usage. Here is an example, from the same web server:
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda5 640000 106991 533009 17% /
/dev/sda2 10040 35 10005 1% /boot
/dev/sda3 1281696 229304 1052392 18% /export
none 128491 1 128490 1% /dev/shm
/dev/sda8 26104 144 25960 1% /tmp
/dev/sda9 1706880 996 1705884 1% /var
/dev/sda6 513024 218937 294087 43% /ww
The /ww filesystem is in excellent shape, since its inode use and filesystem space are both just over 40 percent of capacity. For a healthy computing system, system managers should routinely monitor inode usage on all local filesystems.
df is one of those commands where there is wide variation in the options and output appearance, which again is a nuisance for portable programs that want to parse its output. Hewlett-Packard's implementation on HP-UX is radically different, but fortunately, HP provides a Berkeley-style equivalent, bdf, that produces output that is similar to our example. To deal with this variation, we recommend that you install the GNU version everywhere at your site; it is part of the coreutils package cited in Section 4.1.5.
The du Command
df summarizes free space by filesystem, but does not tell you how much space a particular directory tree requires. That job is done by du (disk usage). Like its companion, df, du's options tend to vary substantially between systems, and its space units also may vary. Two important options are widely implemented: -k (kilobyte units) and -s (summarize). Here are examples from our web server system:
$ du /tmp
12 /tmp/lost+found
1 /tmp/.font-unix
24 /tmp
$ du -s /tmp
24 /tmp
$ du -s /var/log /var/spool /var/tmp
204480 /var/log
236 /var/spool
8 /var/tmp
The GNU version provides the -h (human-readable) option:
$ du -h -s /var/log /var/spool /var/tmp
200M /var/log
236k /var/spool
8.0k /var/tmp
du does not count extra hard links to the same file, and normally ignores soft links. However, some implementations provide options to force soft links to