Disk space and usage in Linux

notes
Linux
Using du and df commands to collect disk information in Linux

TL;DR

To check the disk space use df (for *disk free`):

$ df -h

This will list mounted drives and list the directory where they are mounted, together with the total, used and available space in human readable form.

To check how much space a given directory and its subdirectories or files take, use du (for disk usage):

du [path_to_directory] -h -d [depth]

For example:

$ du . -h -d 2 -a

8,0K    ./posts
52K     ./notes
4,0K    ./README.md
8,0K    ./publications.qmd
142M    .

This will list all (-a) directories and files in the current directory (.), and one level below (-d 2) together with the amount of disk space they take in human readable form (-h).

Breakdown

  1. As usual with Linux commands, the built-in help is the best source of truth: du --help or df --help to obtain all available options.
  2. In both commands -h is short for human-readable, which for me is a must unless the goal is to pipe the output to some other tool.
  3. In the case of du another quite useful options is --time which adds the date and time the file or directory was last modified.

For example:

$ du . -h -d 1 -a --time

8,0K    2024-12-26 13:55    ./posts
52K     2024-12-27 12:11    ./notes
4,0K    2024-12-23 12:52    ./README.md
8,0K    2024-12-23 12:52    ./publications.qmd
142M    2024-12-27 12:11    .

Resources