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

A Telnet session on a terminal interface with the following content:
1) A Telnet session on a terminal interface with the following content:
Connect Edit Terminal Help
danielg % cd project
danielg %

Analysis of the Displayed Terminal Session:
  • Command: `cd project` is a command used to change the directory to one named "project". The prompt returns to "danielg %", indicating that the command executed successfully without any output.
  • User: The session continues under the user "danielg".
  • Contextual Instruction: A tip below the command prompt suggests to "enter cd project to make it your current directory." This is likely a hint or instructional guide for managing directories in the terminal, focusing on checking disk usage of specific directories.

This image illustrates a basic navigation command in Unix/Linux environments, emphasizing basic command-line operations, consistent with system administration tasks.


A Telnet session on a terminal interface with the following content:
2) The image demonstrates the output of the `du` (disk usage) command in a Unix-like system. The output displayed is:
danielg % du
276     ./docs
166     ./mktg
84      ./qa
38      ./sw
568     .
danielg %

Explanation:
  • The du command lists disk usage for each directory within the current directory.
  • The numbers on the left indicate the size in 512-byte blocks (default unit).
  • Subdirectory sizes:
    • ./docs: 276 blocks
    • ./mktg: 166 blocks
    • ./qa: 84 blocks
    • ./sw: 38 blocks
  • The final line (568 .) shows the total disk usage for the current directory (including all subdirectories).


A Telnet session on a terminal interface with the following content:
3)
danielg % du -k
64      ./docs
83      ./mktg
42      ./qa
19      ./sw
210
danielg %

This command, `du -k`, calculates and displays disk usage for each directory and subdirectory in kilobytes (`-k` option). Each entry shows the size of the directory followed by the directory name. The last number (210) represents the total disk usage for the listed directories.
Navigation command in Unix 4
4) Navigation command in Unix 4

Navigation command in Unix 5
5) Navigation command in Unix 5



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:
du [OPTIONS] [FILES or DIRECTORIES]

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:
    du
    
  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.

SEMrush Software 2 SEMrush Banner 2