Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Analyzing Disk Usage with ncdu on Linux Systems

profile picture
Author
Faleddo
Published
2024-01-23
Time to read
5 minutes reading time

Introduction

Disk space management is a critical task for Linux system administrators. As files and directories grow in number and size, keeping track of disk usage becomes essential to maintain system performance and ensure that critical processes have enough space to operate. One of the tools that can aid in this task is ncdu (NCurses Disk Usage). This tutorial provides an in-depth look at ncdu, detailing what it is, how to install it, how to use it, and an overview of its optional parameters.

What is ncdu?

Ncdu, short for NCurses Disk Usage, is a command-line utility designed to help users and system administrators find and manage disk space usage on Linux systems. Unlike traditional disk usage tools such as du, ncdu provides an interactive interface, making it easier to navigate through directories and get a visual representation of space consumption.

The tool is built on the ncurses library, which provides a text-based graphical interface in the terminal. This allows ncdu to present a user-friendly way to explore directories, sort files and directories by size, and delete unnecessary files directly from the interface.

Step 1 - How to Install ncdu

Before you can use ncdu, you must install it on your system. Most Linux distributions include ncdu in their default repositories, making installation straightforward using the system's package manager.

  • On Debian/Ubuntu-based systems

    sudo apt update
    sudo apt install ncdu
  • On Red Hat/CentOS systems

    For Red Hat-based systems, you may need to enable the EPEL repository to find the ncdu package.

    sudo yum install epel-release
    sudo yum install ncdu
  • On Fedora systems

    sudo dnf install ncdu
  • On Arch Linux

    sudo pacman -S ncdu

After the installation is complete, you can start using ncdu to analyze disk usage.

Step 2 - How to Use Ncdu

To begin analyzing disk usage with ncdu, simply run the command followed by the path you want to investigate. If no path is provided, ncdu will analyze the current working directory.

ncdu /path/to/directory

Once you execute the command, ncdu will scan the specified directory and present an interactive interface. The interface shows a list of files and subdirectories, along with their sizes and the percentage of disk space they occupy.

You can navigate through the list using the arrow keys:

Key Description
Up/Down Move through the file list
Enter Enter a directory
n Sort by name (ascending/descending)
s Sort by size (largest/smallest)
g Toggle between showing percentages, graph, or both
d Delete a file or directory (be cautious with this operation)

Press q to quit ncdu and return to the command line.

Step 3 - Optional ncdu Parameters

ncdu offers several command-line options that can modify its behavior or alter its output. Here are some of the optional parameters that can be particularly useful:

Key Description
-x Only count files and directories on the same filesystem as the specified directory. This is useful for not including mounted drives or network filesystems.
-o <filename> Export the scanned data to a file, which can be read later with ncdu -f filename.
-r Enable read-only mode, which disables the ability to delete files from within the interface.

For a full list of options, you can refer to the ncdu man page by typing man ncdu in the terminal.

Step 4 - Advanced Usage and Tips

Beyond basic disk usage analysis, ncdu can be used in more advanced scenarios. For example, scan in remote servers, or check disk usage of another user.

  • Scan remote directories

    ncdu can also scan remote directories over SSH. Use the following syntax:

    ssh -C user@system ncdu -o- / | ./ncdu -f-

    Make sure you have SSH access to the remote host and ncdu installed on both the local and remote machines.

  • Delete Files and Directories

    Be cautious with this, but ncdu allows you to delete files and directories from within its interface. Navigate to the file or directory and press d to delete it.

Conclusion

ncdu is a powerful and user-friendly tool that can greatly simplify disk usage analysis on Linux systems. By providing an interactive interface and a variety of command-line options, it allows both novice and experienced system administrators to efficiently manage disk space. As with any system tool that can alter or delete files, it should be used with caution. Regular use of ncdu, combined with good disk management practices, can help ensure that your Linux systems run smoothly and remain free of space-related issues.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get 20€ free credit!

Valid until: 31 December 2024 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more