2.11.14. Analyze disk space using ncdu

The utility ncdu (NCurses Disk Usage) allows for the analysis of disk space in an interactive mode. It displays data in a pseudo-graphical interface and enables quick navigation through the directory tree structure. It is available by default on all standard and business hosting servers.

To start, connect to your hosting account via SSH and execute the command:

ncdu

After launching ncdu, it scans the disk space for a directory tree, starting from the current one. You can interrupt the scan by pressing the q key.

When starting, you can explicitly specify what needs to be scanned:

  • All disk space of the hosting account regardless of the current directory:
    ncdu ~
  • Contents of a specific directory:
    ncdu ~/path/to/directory

The utility can be run with various keys:

  • –exclude=path/to/directory — exclude the specified directory from scanning.
  • -o result.txt — perform the scan and save the results to the specified file.
  • -f result.txt — instead of scanning, open the results from the specified file. ℹ️ By default, the utility assumes that the file is not related to the current file system, so the keys for rescan, delete files/directories, and open console will be disabled.
  • -e — extended scan. Additionally scanned: file and directory modification times, permissions, owner information. ⚠️ When working with scan results from a file, the key must be used both when saving and opening.
  • –exclude pattern — exclude from scanning files and directories that match the pattern (they will be displayed but not included in the statistics). The key can be specified multiple times.
  • -X file, –exclude-from file — exclude from scanning files and directories that match the patterns listed in the specified file (each pattern on a new line).
  • -L, –follow-symlinks — when scanning, follow symlinks and include in the statistics the sizes of the files and directories they point to.
  • -r — prohibit the deletion of files and directories from the utility (read-only mode).
  • -rr — prohibits opening the console in the current directory.
  • –confirm-quit — require confirmation upon exit.

The full list of keys can be viewed in the help by executing the command:

man ncdu

After scanning, the utility displays data for all directories in the form of a list:

The information is displayed in several columns:

  • The first column may display special symbols, including:
    • e — empty directory.
    • @ — the element is not a file or directory (for example, a symlink, socket, etc.).
    • < — file/directory excluded from statistics (see keys).
    • ! — error reading directory.
    • . — error reading subdirectory (size may be incorrect).
  • Dimensions and units of measurement. They can be displayed in one of two formats:
    • Using the prefix 2 in the form of powers of 1024 (KiB, MiB, etc.).
    • Using the prefix 10 in the form of powers of 1000 (KB, MB, etc.).
  • Graph — the number of # characters indicates which of the files/subdirectories in the current directory occupies more space.
  • File/directory name.

Below is information about the current directory:

  • Disk space usage.
  • Actual size of the directory contents.
  • Number of elements (inodes).
  • , , j, k — move up and down the list.
  • , l, Enter — navigate to the selected directory.
  • , h — return to the parent directory.
  • a — size display mode: actual size or used disk space. ℹ️ Information on the disk is stored in blocks, so the used space may be larger than the actual file size.
  • n — sorting by name: alphabetically or in reverse order.
  • s — sorting by size: descending or ascending.
  • e — enable/disable the display of hidden elements (files and directories with a dot at the beginning or excluded from scanning).
  • g — display mode for occupied space: graph, percentage, both options, do not display. The percentage refers to the current directory, the graph refers to the largest element in the current directory.
  • i — information about the selected object (name, full path, type, used disk space, and actual size). In extended mode (with the -e key), permissions and modification date are also displayed.
  • r — rescan the current directory.
  • d — deletion of the selected file or directory.
  • c — enable/disable display of the number of nested elements for each directory (inodes).
  • C — sorting by the number of nested elements (inodes): in descending or ascending order.
  • t — enable/disable the display of directories before files: directories are displayed at the beginning of the list or together with files.
  • m — enable/disable display of the last modification time of files/directories (mtime). ⚠️ Only available in advanced mode (with the -e key).
  • M — sort by time of last modification: descending or ascending. ⚠️ Only available in advanced mode (with the -e key).
  • q — exit.
  • b — opening the console in the current directory (after exiting, it will return to ncdu).
  • ? — view brief help.

Scanning all content of the hosting account with saving results to a file and archiving it:

  • Scanning:
    ncdu -1xo- ~ | gzip > result.gz
  • Opening the file for analysis:
    zcat result.gz | ncdu -f-

Scanning the current directory with saving results to a file and opening for analysis:

ncdu -o- | tee result.txt | ncdu -f-
Content