[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14. Disk usage

No disk can hold an infinite amount of data. These commands report how much disk storage is in use or available, report other file and file status information, and write buffers to disk.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.1 df: Report file system disk space usage

df reports the amount of disk space used and available on file systems. Synopsis:

 
df [option]… [file]…

With no arguments, df reports the space used and available on all currently mounted file systems (of all types). Otherwise, df reports on the file system containing each argument file.

Normally the disk space is printed in units of 1024 bytes, but this can be overridden (see section Block size). Non-integer quantities are rounded up to the next higher unit.

If an argument file is a disk device file containing a mounted file system, df shows the space available on that file system rather than on the file system containing the device node (i.e., the root file system). GNU df does not attempt to determine the disk usage on unmounted file systems, because on most kinds of systems doing so requires extremely nonportable intimate knowledge of file system structures.

The program accepts the following options. Also see Common options.

`-a'
`--all'

Include in the listing dummy file systems, which are omitted by default. Such file systems are typically special-purpose pseudo-file-systems, such as automounter entries.

`-B size'
`--block-size=size'

Scale sizes by size before printing them (see section Block size). For example, `-BG' prints sizes in units of 1,073,741,824 bytes.

`--total'

Print a grand total of all arguments after all arguments have been processed. This can be used to find out the total disk size, usage and available space of all listed devices.

`-h'
`--human-readable'

Append a size letter to each size, such as `M' for mebibytes. Powers of 1024 are used, not 1000; `M' stands for 1,048,576 bytes. This option is equivalent to `--block-size=human-readable'. Use the `--si' option if you prefer powers of 1000.

`-H'

Equivalent to `--si'.

`-i'
`--inodes'

List inode usage information instead of block usage. An inode (short for index node) contains information about a file such as its owner, permissions, timestamps, and location on the disk.

`-k'

Print sizes in 1024-byte blocks, overriding the default block size (see section Block size). This option is equivalent to `--block-size=1K'.

`-l'
`--local'

Limit the listing to local file systems. By default, remote file systems are also listed.

`--no-sync'

Do not invoke the sync system call before getting any usage data. This may make df run significantly faster on systems with many disks, but on some systems (notably SunOS) the results may be slightly out of date. This is the default.

`-P'
`--portability'

Use the POSIX output format. This is like the default format except for the following:

  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.
  2. The labels in the header output line are changed to conform to POSIX.
  3. The default block size and output format are unaffected by the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables. However, the default block size is still affected by POSIXLY_CORRECT: it is 512 if POSIXLY_CORRECT is set, 1024 otherwise. See section Block size.
`--si'

Append an SI-style abbreviation to each size, such as `M' for megabytes. Powers of 1000 are used, not 1024; `M' stands for 1,000,000 bytes. This option is equivalent to `--block-size=si'. Use the `-h' or `--human-readable' option if you prefer powers of 1024.

`--sync'

Invoke the sync system call before getting any usage data. On some systems (notably SunOS), doing this yields more up to date results, but in general this option makes df much slower, especially when there are many or very busy file systems.

`-t fstype'
`--type=fstype'

Limit the listing to file systems of type fstype. Multiple file system types can be specified by giving multiple `-t' options. By default, nothing is omitted.

`-T'
`--print-type'

Print each file system's type. The types printed here are the same ones you can include or exclude with `-t' and `-x'. The particular types printed are whatever is supported by the system. Here are some of the common names (this list is certainly not exhaustive):

`nfs'

An NFS file system, i.e., one mounted over a network from another machine. This is the one type name which seems to be used uniformly by all systems.

`4.2, ufs, efs…'

A file system on a locally-mounted hard disk. (The system might even support more than one type here; Linux does.)

`hsfs, cdfs'

A file system on a CD-ROM drive. HP-UX uses `cdfs', most other systems use `hsfs' (`hs' for "High Sierra").

`pcfs'

An MS-DOS file system, usually on a diskette.

`-x fstype'
`--exclude-type=fstype'

Limit the listing to file systems not of type fstype. Multiple file system types can be eliminated by giving multiple `-x' options. By default, no file system types are omitted.

`-v'

Ignored; for compatibility with System V versions of df.

An exit status of zero indicates success, and a nonzero value indicates failure. Failure includes the case where no output is generated, so you can inspect the exit status of a command like `df -t ext3 -t reiserfs dir' to test whether dir is on a file system of type `ext3' or `reiserfs'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.2 du: Estimate file space usage

du reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments). Synopsis:

 
du [option]… [file]…

With no arguments, du reports the disk space for the current directory. Normally the disk space is printed in units of 1024 bytes, but this can be overridden (see section Block size). Non-integer quantities are rounded up to the next higher unit.

If two or more hard links point to the same file, only one of the hard links is counted. The file argument order affects which links are counted, and changing the argument order may change the numbers that du outputs.

The program accepts the following options. Also see Common options.

`-a'
`--all'

Show counts for all files, not just directories.

`--apparent-size'

Print apparent sizes, rather than disk usage. The apparent size of a file is the number of bytes reported by wc -c on regular files, or more generally, ls -l --block-size=1 or stat --format=%s. For example, a file containing the word `zoo' with no newline would, of course, have an apparent size of 3. Such a small file may require anywhere from 0 to 16 KiB or more of disk space, depending on the type and configuration of the file system on which the file resides. However, a sparse file created with this command:

 
dd bs=1 seek=2GiB if=/dev/null of=big

has an apparent size of 2 GiB, yet on most modern systems, it actually uses almost no disk space.

`-b'
`--bytes'

Equivalent to --apparent-size --block-size=1.

`-B size'
`--block-size=size'

Scale sizes by size before printing them (see section Block size). For example, `-BG' prints sizes in units of 1,073,741,824 bytes.

`-c'
`--total'

Print a grand total of all arguments after all arguments have been processed. This can be used to find out the total disk usage of a given set of files or directories.

`-D'
`--dereference-args'

Dereference symbolic links that are command line arguments. Does not affect other symbolic links. This is helpful for finding out the disk usage of directories, such as `/usr/tmp', which are often symbolic links.

`--files0-from=file'

Disallow processing files named on the command line, and instead process those named in file file; each name being terminated by a zero byte (ASCII NUL). This is useful when the list of file names is so long that it may exceed a command line length limitation. In such cases, running du via xargs is undesirable because it splits the list into pieces and makes du print with the `--total' (`-c') option for each sublist rather than for the entire list. One way to produce a list of ASCII NUL terminated file names is with GNU find, using its `-print0' predicate. If file is `-' then the ASCII NUL terminated file names are read from standard input.

`-h'
`--human-readable'

Append a size letter to each size, such as `M' for mebibytes. Powers of 1024 are used, not 1000; `M' stands for 1,048,576 bytes. This option is equivalent to `--block-size=human-readable'. Use the `--si' option if you prefer powers of 1000.

`-H'

Equivalent to `--dereference-args' (`-D').

`-k'

Print sizes in 1024-byte blocks, overriding the default block size (see section Block size). This option is equivalent to `--block-size=1K'.

`-l'
`--count-links'

Count the size of all files, even if they have appeared already (as a hard link).

`-L'
`--dereference'

Dereference symbolic links (show the disk space used by the file or directory that the link points to instead of the space used by the link).

`-m'

Print sizes in 1,048,576-byte blocks, overriding the default block size (see section Block size). This option is equivalent to `--block-size=1M'.

`-P'
`--no-dereference'

For each symbolic links encountered by du, consider the disk space used by the symbolic link.

`--max-depth=depth'

Show the total for each directory (and file if -all) that is at most MAX_DEPTH levels down from the root of the hierarchy. The root is at level 0, so du --max-depth=0 is equivalent to du -s.

`-0'
`--null'

Output a zero byte (ASCII NUL) at the end of each line, rather than a newline. This option enables other programs to parse the output of du even when that output would contain data with embedded newlines.

`--si'

Append an SI-style abbreviation to each size, such as `M' for megabytes. Powers of 1000 are used, not 1024; `M' stands for 1,000,000 bytes. This option is equivalent to `--block-size=si'. Use the `-h' or `--human-readable' option if you prefer powers of 1024.

`-s'
`--summarize'

Display only a total for each argument.

`-S'
`--separate-dirs'

Normally, in the output of du (when not using `--summarize'), the size listed next to a directory name, d, represents the sum of sizes of all entries beneath d as well as the size of d itself. With `--separate-dirs', the size reported for a directory name, d, is merely the stat.st_size-derived size of the directory entry, d.

`--time'

Show time of the most recent modification of any file in the directory, or any of its subdirectories.

`--time=ctime'
`--time=status'
`--time=use'

Show the most recent status change time (the `ctime' in the inode) of any file in the directory, instead of the modification time.

`--time=atime'
`--time=access'

Show the most recent access time (the `atime' in the inode) of any file in the directory, instead of the modification time.

`--time-style=style'

List timestamps in style style. This option has an effect only if the `--time' option is also specified. The style should be one of the following:

`+format'

List timestamps using format, where format is interpreted like the format argument of date (see section date: Print or set system date and time). For example, `--time-style="+%Y-%m-%d %H:%M:%S"' causes du to list timestamps like `2002-03-30 23:45:56'. As with date, format's interpretation is affected by the LC_TIME locale category.

`full-iso'

List timestamps in full using ISO 8601 date, time, and time zone format with nanosecond precision, e.g., `2002-03-30 23:45:56.477817180 -0700'. This style is equivalent to `+%Y-%m-%d %H:%M:%S.%N %z'.

`long-iso'

List ISO 8601 date and time in minutes, e.g., `2002-03-30 23:45'. These timestamps are shorter than `full-iso' timestamps, and are usually good enough for everyday work. This style is equivalent to `+%Y-%m-%d %H:%M'.

`iso'

List ISO 8601 dates for timestamps, e.g., `2002-03-30'. This style is equivalent to `+%Y-%m-%d'.

You can specify the default value of the `--time-style' option with the environment variable TIME_STYLE; if TIME_STYLE is not set the default style is `long-iso'. For compatibility with ls, if TIME_STYLE begins with `+' and contains a newline, the newline and any later characters are ignored; if TIME_STYLE begins with `posix-' the `posix-' is ignored; and if TIME_STYLE is `locale' it is ignored.

`-x'
`--one-file-system'

Skip directories that are on different file systems from the one that the argument being processed is on.

`--exclude=pattern'

When recursing, skip subdirectories or files matching pattern. For example, du --exclude='*.o' excludes files whose names end in `.o'.

`-X file'
`--exclude-from=file'

Like `--exclude', except take the patterns to exclude from file, one per line. If file is `-', take the patterns from standard input.

On BSD systems, du reports sizes that are half the correct values for files that are NFS-mounted from HP-UX systems. On HP-UX systems, it reports sizes that are twice the correct values for files that are NFS-mounted from BSD systems. This is due to a flaw in HP-UX; it also affects the HP-UX du program.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.3 stat: Report file or file system status

stat displays information about the specified file(s). Synopsis:

 
stat [option]… [file]…

With no option, stat reports all information about the given files. But it also can be used to report the information of the file systems the given files are located on. If the files are links, stat can also give information about the files the links point to.

Due to shell aliases and built-in stat command, using an unadorned stat interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env stat …) to avoid interference from the shell.

`-L'
`--dereference'

Change how stat treats symbolic links. With this option, stat acts on the file referenced by each symbolic link argument. Without it, stat acts on any symbolic link argument directly.

`-f'
`--file-system'

Report information about the file systems where the given files are located instead of information about the files themselves.

`-c'
`--format=format'

Use format rather than the default format. format is automatically newline-terminated, so running a command like the following with two or more file operands produces a line of output for each operand:

 
$ stat --format=%d:%i / /usr
2050:2
2057:2
`--printf=format'

Use format rather than the default format. Like `--format', but interpret backslash escapes, and do not output a mandatory trailing newline. If you want a newline, include `\n' in the format. Here's how you would use `--printf' to print the device and inode numbers of `/' and `/usr':

 
$ stat --printf='%d:%i\n' / /usr
2050:2
2057:2
`-t'
`--terse'

Print the information in terse form, suitable for parsing by other programs.

The valid format directives for files with `--format' and `--printf' are:

When listing file system information (`--file-system' (`-f')), you must use a different set of format directives:

Time stamps are listed according to the time zone rules specified by the TZ environment variable, or by the system default rules if TZ is not set. See (libc)TZ Variable section `Specifying the Time Zone with TZ' in The GNU C Library Reference Manual.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.4 sync: Synchronize data on disk with memory

sync writes any data buffered in memory out to disk. This can include (but is not limited to) modified superblocks, modified inodes, and delayed reads and writes. This must be implemented by the kernel; The sync program does nothing but exercise the sync system call.

The kernel keeps data in memory to avoid doing (relatively slow) disk reads and writes. This improves performance, but if the computer crashes, data may be lost or the file system corrupted as a result. The sync command ensures everything in memory is written to disk.

Any arguments are ignored, except for a lone `--help' or `--version' (see section Common options).

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.5 truncate: Shrink or extend the size of a file

truncate shrinks or extends the size of each file to the specified size. Synopsis:

 
truncate optionfile

Any file that does not exist is created.

If a file is larger than the specified size, the extra data is lost. If a file is shorter, it is extended and the extended part (or hole) reads as zero bytes.

The program accepts the following options. Also see Common options.

`-c'
`--no-create'

Do not create files that do not exist.

`-o'
`--io-blocks'

Treat size as number of I/O blocks of the file rather than bytes.

`-r rfile'
`--reference=rfile'

Set the size of each file to the same size as rfile.

`-s size'
`--size=size'

Set the size of each file to this size. size may be, or may be an integer optionally followed by, one of the following multiplicative suffixes:

 
`KB' =>           1000 (KiloBytes)
`K'  =>           1024 (KibiBytes)
`MB' =>      1000*1000 (MegaBytes)
`M'  =>      1024*1024 (MebiBytes)
`GB' => 1000*1000*1000 (GigaBytes)
`G'  => 1024*1024*1024 (GibiBytes)

and so on for `T', `P', `E', `Z', and `Y'.

size may also be prefixed by one of the following to adjust the size of each file based on their current size:

 
`+'  => extend by
`-'  => reduce by
`<'  => at most
`>'  => at least
`/'  => round down to multiple of
`%'  => round up to multiple of

An exit status of zero indicates success, and a nonzero value indicates failure.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on January, 20 2010 using texi2html 1.76.