Managing Disk Space   «Prev  Next»
Lesson 2 Checking your disk usage
ObjectiveUse du to display disk usage.

Checking disk usage

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 SlideShow shows some common uses of the du command:

How is the du command used in Unix?

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:

Here are some common options for the du command:
  1. -h or --human-readable: display sizes in a more human-readable format, using units like KB, MB, GB, etc.
  2. -s or --summarize: display only a total for each argument
  3. -c or --total: display a grand total of all arguments
  4. -a or --all: display information for all files, not just directories
Here are some examples of how to use the du command:
  1. To display the disk usage of the current directory:
  2. To display the disk usage of a specific file or directory:
    du /path/to/file/or/directory
  3. To display the disk usage in a human-readable format:
    du -h
  4. To display only the total size of a specific directory:
    du -sh /path/to/directory
  5. 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.
[1]file compression: File compression is a way of packing a file's contents more efficiently, so that the file takes up less disk space.