How to customize the ncdu tool in Linux?

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial will guide you through the process of customizing the ncdu (NCurses Disk Usage) tool in the Linux operating system. ncdu is a powerful command-line utility that provides an interactive way to analyze disk usage, making it an essential tool for Linux users. By the end of this article, you will learn how to tailor ncdu to your specific needs and leverage its features to optimize your file management workflow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/watch("`Command Repeating`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/watch -.-> lab-414499{{"`How to customize the ncdu tool in Linux?`"}} linux/top -.-> lab-414499{{"`How to customize the ncdu tool in Linux?`"}} linux/free -.-> lab-414499{{"`How to customize the ncdu tool in Linux?`"}} linux/df -.-> lab-414499{{"`How to customize the ncdu tool in Linux?`"}} linux/du -.-> lab-414499{{"`How to customize the ncdu tool in Linux?`"}} end

Introduction to ncdu

What is ncdu?

ncdu (Ncurses Disk Usage) is a command-line tool for Linux that provides an interactive way to analyze disk usage. It is a fast and efficient alternative to the traditional du command, offering a more user-friendly interface for visualizing and navigating through directory structures.

Key Features of ncdu

  • Interactive Interface: ncdu provides a ncurses-based (a text-based user interface library) interface that allows users to navigate through directories, view file and directory sizes, and perform various actions.
  • Efficient Disk Scanning: ncdu scans the file system quickly, making it suitable for analyzing large directories or entire disk volumes.
  • Sorting and Filtering: Users can sort the directory contents by size, name, or other attributes, and filter the results to focus on specific files or directories.
  • Deletion and Exclusion: ncdu allows users to delete files or directories directly from the interface and exclude specific paths from the analysis.
  • Portable and Lightweight: ncdu is a standalone executable, making it easy to use on various Linux distributions without the need for additional dependencies.

Installing and Running ncdu

To install ncdu on your Ubuntu 22.04 system, you can use the following command in the terminal:

sudo apt-get install ncdu

Once installed, you can run ncdu by simply typing the following command in the terminal:

ncdu

This will launch the ncdu interface, allowing you to navigate through your file system and analyze disk usage.

Customizing ncdu Settings and Preferences

Accessing the ncdu Configuration

ncdu allows users to customize various settings and preferences to suit their needs. To access the configuration, you can press the ? key while in the ncdu interface, which will display the available commands and options.

Configuring ncdu Preferences

Some of the key preferences you can customize in ncdu include:

  • Sort Order: You can change the default sort order of the directory contents by pressing the s key and selecting the desired sorting criteria (size, name, etc.).
  • Display Units: You can toggle the display units between bytes, kilobytes, megabytes, and gigabytes by pressing the u key.
  • Exclude Directories: You can exclude specific directories from the analysis by pressing the e key and entering the path to exclude.
  • Colorization: You can enable or disable the colorization of the directory contents by pressing the c key.
  • Scan Depth: You can set the maximum depth for the directory scan by pressing the d key and entering the desired value.

Here's an example of how you can change the sort order in ncdu on Ubuntu 22.04:

## Launch ncdu
ncdu

## Press 's' to access the sort options
## Select the desired sort order (e.g., sort by size)

Saving and Loading Preferences

ncdu allows you to save your custom settings and preferences to a configuration file. To do this, press the w key while in the ncdu interface, and the current settings will be saved to the ~/.config/ncdu/ncdu.conf file.

You can also load a previously saved configuration by pressing the l key and selecting the appropriate file.

Practical ncdu Usage Scenarios

Analyzing Disk Space Usage

One of the primary use cases for ncdu is to analyze disk space usage on your system. You can launch ncdu and navigate through your file system to identify the directories and files consuming the most disk space. This can be particularly useful when you need to free up space on a constrained storage volume.

## Launch ncdu and navigate to the desired directory
ncdu /

Identifying and Deleting Unnecessary Files

With ncdu, you can easily identify and delete unnecessary files or directories that are taking up valuable disk space. Simply navigate to the relevant directory, select the files or folders you want to delete, and press the d key to delete them.

## Launch ncdu and navigate to the directory containing unnecessary files
ncdu /path/to/directory
## Press 'd' to delete selected files or directories

Excluding Directories from the Analysis

If you have certain directories that you want to exclude from the disk usage analysis, you can use the e key in ncdu to add them to the exclusion list. This can be useful when you want to focus on specific areas of your file system or exclude temporary or system-generated files and directories.

## Launch ncdu
ncdu
## Press 'e' to add directories to the exclusion list

Saving and Restoring Preferences

As mentioned earlier, ncdu allows you to save your custom settings and preferences to a configuration file. This can be particularly useful if you have a specific set of preferences that you want to use consistently across different systems or sessions.

## Launch ncdu
ncdu
## Press 'w' to save the current settings to the configuration file
## Press 'l' to load a previously saved configuration

By leveraging these practical usage scenarios, you can effectively use ncdu to optimize disk space, identify and remove unnecessary files, and customize the tool to suit your specific needs.

Summary

In this comprehensive guide, you have learned how to customize the ncdu tool in Linux. By exploring the various settings and preferences, as well as practical usage scenarios, you can now optimize ncdu to suit your specific file management requirements. Whether you need to analyze disk usage, identify space-hogging files, or streamline your file management tasks, the customized ncdu tool will become an invaluable asset in your Linux toolbox.

Other Linux Tutorials you may like