Sometimes you want to know how much disk space is used by a specific directory. For example, if your data take up a lot of space, you
may want to free up disk space by deleting or
compressing files[1]. To learn how much space a directory
occupies, use the
du
command. The simplest form is the command by itself:
% du
This command reports the size of the directory tree that begins at the current directory. In particular,
du
lists the size of each individual subdirectory, along with the total size for the current directory. The total size includes subdirectories and individual files in the current directory. Sizes are displayed as units of 512 bytes, known as blocks. In other words, a block equals half a kilobyte (Kb).
The following
series of images shows some common uses of the
du
command:
The du command in Unix is used to estimate the disk usage of files and directories. It reports the space used by each file and directory, as well as the total size of each directory. The du command is useful for determining which files and directories are taking up the most space on a system.
Here is the basic syntax for the du command:
du [OPTIONS] [FILES or DIRECTORIES]
Here are some common options for the du command:
- -h or --human-readable: display sizes in a more human-readable format, using units like KB, MB, GB, etc.
- -s or --summarize: display only a total for each argument
- -c or --total: display a grand total of all arguments
- -a or --all: display information for all files, not just directories
Here are some examples of how to use the du command:
- To display the disk usage of the current directory:
du
- To display the disk usage of a specific file or directory:
du /path/to/file/or/directory
- To display the disk usage in a human-readable format:
du -h
- To display only the total size of a specific directory:
du -sh /path/to/directory
- To display the total size of multiple directories:
du -c /path/to/directory1 /path/to/directory2
The du command can be used in combination with other Unix commands to analyze disk usage and find the largest files or directories on a system. For example, piping the output of du to the sort command can be used to display the largest files or directories in order of size.
In the next lesson, you will learn to compress and uncompress files. (disk usage: Disk usage is the amount of storage space your files occupy on the disk.