2.11.14. Disk space analysis with ncdu

Utility ncdu (NCurses Disk Usage) allows you to analyze disk space interactively. It displays data in a pseudo-graphical interface and allows you to quickly navigate a tree-like directory structure. Available by default on all regular and businesshosting.

For start connect to your hosting account via SSH and run the command:


Once started, ncdu scans the disk space for the directory tree, starting with the current one. You can interrupt scanning by pressing q.

At startup, you can explicitly set what to scan:

  • All disk space of the hosting account, regardless of the current directory:
    ncdu ~
  • The contents of only a specific directory:
    ncdu ~/way/to/catalog

The utility can be run with various keys:

  • –exclude=path/to/directory - exclude the specified directory from scanning.
  • -o result.txt - run a scan and save the results to the specified file.
  • -f result.txt - instead of scanning, open the results from the specified file. Note By default, the utility considers the file to be irrelevant to the current file system, so the keys for rescanning, deleting files / directories, and opening the console will be disabled.
  • -e — extended scanning. Additionally scanned: time of changes of files and directories, access rights, information about the owner. Attention When working with scan results from a file, the key must be used both when saving and opening.
  • –exclude template — exclude from scanning files and directories that match the template (they will be displayed, but will not be taken into account in statistics). The key can be specified several times.
  • -X file, –exclude-from file — exclude from scanning files and directories that match the templates listed in the specified file (each template on a new line).
  • -L, –follow-symlinks — when scanning, follow the symlinks and take into account in statistics the sizes of files and directories to which they point.
  • -r — prohibition of deleting files and directories from the utility (read—only mode).
  • -rr — prohibition of opening the console in the current directory.
  • –confirm-quit — require confirmation upon exit.

A complete list of keys can be found in the help by running the command:

man ncdu

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

Information is displayed in several columns:

  • The first column can display special designations, including:
    • e Is an empty directory.
    • @ - the item is not a file or directory (eg symlink, socket, etc.).
    • < — file / directory is excluded from statistics (see. thekeys).
    • ! - error while reading the directory.
    • . — an error occurred while reading a subdirectory (the size may not be correct).
  • Sizes and units. They can be output in one of two formats:
    • Using the prefix 2 as powers of 1024 (KiB, MiB, etc.).
    • Using the 10 prefix as powers of 1000 (KB, MB, etc.).
  • Count — number of characters # shows which of the files / subdirectories in the current directory is taking up more space.
  • File / directory name.

Below is information about the current directory:

  • The amount of disk space used.
  • The actual size of the directory contents.
  • The number of elements (inodes).
  • , , j, k — moving up and down the list.
  • , l, Enter — go to the selected directory.
  • , h — return to the parent directory.
  • a — size display mode: real size or occupied disk space. Note The information on the disk is stored in blocks, so the occupied space may be larger than the actual file size.
  • n - sort by name: alphabetically or in reverse order.
  • s — sorting by size: descending or ascending.
  • e — enable / disable display of hidden elements (files and directories with a dot at the beginning or excluded from scanning).
  • g — mode of displaying the occupied place: graph, percent, both options, do not display. The percentage refers to the current directory, the graph refers to the largest item in the current directory.
  • i — information about the selected object (name, full path, type, occupied disk space and real size). In extended mode (with key -e) additionally displays the rights and the date of change.
  • r - rescanning the current directory.
  • d — delete 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): descending or ascending.
  • t - enable / disable display of directories before files: directories are displayed at the beginning of the list or together with files.
  • m - enable / disable display of the time of the last modification of files / directories (mtime). Attention! Available only in advanced mode (with key -e).
  • M - sorting by the time of the last change: descending or ascending. Attention! Available only in advanced mode (with key -e).
  • q - output.
  • b - opening the console in the current directory (after exiting, there will be a return to ncdu).
  • ? - viewing brief help.

Scanning the entire contents of the hosting account, saving the results to a file and archiving it:

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

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

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