Du (Unix)
Template:Lowercase
Template:Short description
Template:Infobox software
du is a shell command for reporting the file system storage allocated to files and directory trees. With no command-line arguments, it reports the space allocated to the working directory and to each directory tree that it contains, recursively. Space allocated to files is reported if files are specified for inclusion. For a symbolic link file, the size of the link file is reported; not what it links to.
Although du is short for disk usage,<ref>Template:Cite web</ref> the command is not limited to disk storage. It was developed during the long period of time when disk-based storage was the ubiquitous mass storage technology.
<syntaxhighlight lang="text" class="" style="" inline="1">du</syntaxhighlight> differs from df in that <syntaxhighlight lang="text" class="" style="" inline="1">du</syntaxhighlight> reports size information of file system items whereas <syntaxhighlight lang="text" class="" style="" inline="1">df</syntaxhighlight> reports statistics about the storage media as a whole. <syntaxhighlight lang="text" class="" style="" inline="1">du</syntaxhighlight> can report more detailed information, but can take longer to complete when processing many files. Also, since a storage media may have allocated space that is not associated with an accessible file (i.e. file was deleted but space not freed), <syntaxhighlight lang="text" class="" style="" inline="1">df</syntaxhighlight> might report more allocated space than <syntaxhighlight lang="text" class="" style="" inline="1">du</syntaxhighlight> if it were used to calculate the space of all files of a media. Also, the minfree setting that allocates data blocks for the file system and the super user processes creates a discrepancy between total blocks and the sum of used and available blocks.
The du command first appeared in version 1 of AT&T UNIX. It is specified by the Single UNIX Specification (SUS). The implementation in GNU coreutils was written by Torbjorn Granlund, David MacKenzie, Paul Eggert, and Jim Meyering.<ref>Template:Man</ref> The command is also available for FreeDOS.<ref>Template:Cite web</ref> A similar command is available for Windows in Sysinternals by Mark Russinovich.
Use
du accepts any number of parameters that each specify a file by path to specify the starting scope. If none specified, the working directory is used. SUS mandates the following optional options:
- <syntaxhighlight lang="text" class="" style="" inline="1">-a</syntaxhighlight>, In addition to the default output, include information for each non-directory entry
- <syntaxhighlight lang="text" class="" style="" inline="1">-c</syntaxhighlight>, Report the grand total of the storage usage for the specified scope
- <syntaxhighlight lang="text" class="" style="" inline="1">-d #</syntaxhighlight>, The maximum directory tree depth of the scope; deeper directories are ignored; for example, 0 sums the starting scope directory only and 1 sums the starting scope directory and its subdirectories
- <syntaxhighlight lang="text" class="" style="" inline="1">-H</syntaxhighlight>, Calculate storage usage for link references specified on the command line
- <syntaxhighlight lang="text" class="" style="" inline="1">-k</syntaxhighlight>, Show sizes as multiples of 1024 bytes, not 512-byte
- <syntaxhighlight lang="text" class="" style="" inline="1">-L</syntaxhighlight>, Calculate storage usage for link references
- <syntaxhighlight lang="text" class="" style="" inline="1">-s</syntaxhighlight>, Report only the sum of the usage of the starting scope directory; not for subdirectories
- <syntaxhighlight lang="text" class="" style="" inline="1">-x</syntaxhighlight>, Only traverse files and directories on which the path argument is specified
Some implementations support other options. For example, BSD and GNU support a <syntaxhighlight lang="text" class="" style="" inline="1">-h</syntaxhighlight> option that selects numbers to be formatted using metric units and notation (e.g. 10 MB) instead of bytes.
Examples
Report the storage use for each file and directory tree in kilobytes (<syntaxhighlight lang="text" class="" style="" inline="1">-k</syntaxhighlight>):
<syntaxhighlight lang="console"> $ du -sk * 152304 directoryOne 1856548 directoryTwo </syntaxhighlight>
Report the storage use in a more human-readable format (<syntaxhighlight lang="text" class="" style="" inline="1">-h</syntaxhighlight>:
<syntaxhighlight lang="console"> $ du -sh * 149M directoryOne 1.8G directoryTwo </syntaxhighlight>
Report the storage use of all subdirectories and files including hidden files within the working directory sorted by file size:
<syntaxhighlight lang="console"> $ du -sk .[!.]* *| sort -n </syntaxhighlight>
Report the storage use under in the working directory (<syntaxhighlight lang="text" class="" style="" inline="1">-d 1</syntaxhighlight>) with a sum total at the end (<syntaxhighlight lang="text" class="" style="" inline="1">-c</syntaxhighlight>), formatted as human-readable (<syntaxhighlight lang="text" class="" style="" inline="1">-h</syntaxhighlight>):
<syntaxhighlight lang="console"> $ du -d 1 -c -h </syntaxhighlight>
For the GNU implementation, <syntaxhighlight lang="text" class="" style="" inline="1">--max-depth</syntaxhighlight> is used instead of <syntaxhighlight lang="text" class="" style="" inline="1">-d</syntaxhighlight>.
Report the storage use under the root directory (<syntaxhighlight lang="text" class="" style="" inline="1">-d 1</syntaxhighlight>, trailing Template:Char) with a sum total at the end (<syntaxhighlight lang="text" class="" style="" inline="1">-c</syntaxhighlight>), formatted as human-readable (<syntaxhighlight lang="text" class="" style="" inline="1">-h</syntaxhighlight>) without traversing into other file systems (<syntaxhighlight lang="text" class="" style="" inline="1">-x</syntaxhighlight>). Useful when /var, /tmp or other directories are on separate storage from the root directory:
<syntaxhighlight lang="console"> $ du -d 1 -c -h -x / </syntaxhighlight>
See also
References
External links
Template:Disk space analyzers Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands