How to Check File and Directory Size in Linux: du and ncdu Guide

This guide demonstrates how to use two user-friendly, command-line tools for analyzing disk space usage.

These tools will help you understand how much space your files and directories are consuming, whether they’re on your computer or a remote server. These tools provide a simple way to troubleshoot low disk space and are critical for any Linux system administrator. Let’s begin!

Using the Linux `du` Command to Check Disk Usage

The `du` (disk usage) command is a standard Linux utility that estimates file space usage. It’s a powerful tool for tracking down which files and directories are the biggest space hogs on your system. It is a quick and simple way to perform a Linux disk usage check.

To find out the size of a specific directory, use the command below. Make sure to replace `the_file_path` with the actual path to the directory you’re interested in. This command provides a direct way to how to check directory size in Linux from the command line.

du -sh the_file_path

Command Breakdown:

  • `du` (disk usage) estimates the disk space used by `the_file_path`. It’s your go-to tool for checking disk usage on a per-file or per-directory basis.
  • The options `-sh` do the following:
    `-s`, `–summarize`: Shows only the total size for each directory you specify. It simplifies the output by hiding individual file sizes within the directory.
    `-h`, `–human-readable`: Presents the sizes in a user-friendly format (e.g., 10K, 234M, 2G), making it easy to read and understand. This option is very useful for quick size assessments.

check directory size in linux

To check the sizes of multiple directories at once and also see a combined total, use the following command. This is useful for getting a summary view of how multiple locations contribute to total disk usage.
du -sch                                      
Where:
`-c`, `–total`: Adds a grand total line at the end, showing the combined size of all specified directories.

Total folder size in Linux

If you only want to see the size of a directory *without* including its subdirectories, use the following option. This gives the size of the directory’s immediate contents only.

-S, –separate-dirs

Linux directory size exclude subdirectories

Read: How to Show File Sizes in Megabytes (MB) on Linux

Using `ncdu` for Interactive Disk Usage Analysis

`ncdu` (NCurses Disk Usage) is a powerful, interactive tool that helps you quickly see which directories are using the most disk space. This fast and simple C program is a disk usage analyzer, displaying files and directories that consume significant space on both local and remote systems. It provides a visual and interactive alternative to the `du` command.

Installing `ncdu` on Your Linux System

`ncdu` is easily installed from the default package repositories of most major Linux distributions.

On Arch Linux, Manjaro Linux, and Antergos, install `ncdu` with:
sudo pacman -S ncdu

On RHEL, Scientific Linux, and CentOS, use this command:
sudo yum install ncdu

On Fedora, install it like this:
sudo dnf install ncdu

On SUSE and openSUSE, the command is:
sudo zypper in ncdu

On Ubuntu and similar distributions, use this command:
sudo apt-get install ncdu

ncdu installation process

Once installed, launch `ncdu` by typing the following command:
ncdu

This opens the `ncdu` interface, as shown below:
ncdu main interface

`ncdu` will immediately start analyzing your current directory, displaying disk usage data with the largest items listed first (in descending order). This makes it easy to quickly identify large files.

Read: 4 Ways to Find Large Files on Linux and Free Up Disk Space

As shown at the top of the screen in the image above, you can navigate using the up/down arrow keys, or the ‘k’ (up) and ‘j’ (down) keys. The help menu, accessed by pressing ‘?’, is displayed below:
ncdu navigation keys

Select a file or directory and press the “i” key to view its details, like this:
ncdu item information

Press “i” again to hide the details window.

To explore the contents of a selected directory, press ENTER or the right arrow key. Here, we are viewing the files within the “/.cache” directory:
ncdu directory contents

To go back to the parent directory or the previous screen, press the left arrow key. You can navigate as deep into the directory structure as needed.

To analyze a specific directory directly, provide its path when starting `ncdu`, as shown here:
ncdu analyze specific directory

To analyze the entire root directory (“/”), run the following command. *Be aware that scanning the root directory can take significant time.*
sudo ncdu -x /

ncdu root directory scan
The `-x` option is important: it tells `ncdu` to only consider files and directories on the same filesystem as the starting directory. This prevents it from scanning mounted devices with different filesystems, keeping the analysis focused.

`ncdu` Quiet Mode: Reducing Update Frequency

By default, `ncdu` updates its display ten times per second while scanning. When analyzing a remote system, this frequent updating can use more network bandwidth. Quiet mode reduces the update frequency to once every two seconds.

To use quiet mode, start `ncdu` with the `-q` option:
ncdu -q

Exporting `ncdu` Analysis Reports

You can save `ncdu`’s analysis results to a file for later review or sharing. The following command exports the results of scanning the root directory and compresses them using gzip:

ncdu -1xo- / | gzip > analysis.gz

ncdu export example

For example, to export the analysis of the `/.cache` directory:
ncdu export cache directory

Viewing `ncdu` Exported Reports

To view an exported report (like `analysis.gz` created above), use this command. This reloads the data into `ncdu` without rescanning.

zcat analysis.gz | ncdu -f-

To analyze, export, and view the report simultaneously for the current directory, use this command. It pipes the output through `tee` to both save to a file and display in `ncdu`:
ncdu -o- | tee analysis.file | ncdu -f-

To do the same but with gzip compression, use this more complex pipeline:
ncdu -o- | gzip | tee analysis.gz | gunzip | ncdu -f-

You may be interested to read: Gunzip command in Linux

Deleting Files and Directories within `ncdu`

`ncdu` allows you to delete files or directories directly from its interface. Select the item you want to remove and press “d”. You will be presented with a confirmation prompt, as shown below:

ncdu delete file prompt

For complete details on all of `ncdu`’s features, consult its manual page by running `man ncdu` in your terminal.

Conclusion: Effective Disk Space Management in Linux

`du` and `ncdu` are essential utilities for managing disk space on Linux, whether on your local machine or a remote server. These command-line tools offer a simple yet efficient approach for analyzing and managing disk space usage to find out where is your free space going. For information on alternative methods, including graphical tools, for finding large files, please see our article here.

Frequently Asked Questions (FAQ) – Linux Disk Usage with du and ncdu

This FAQ covers the most common questions about checking and managing disk space on Linux using the du and ncdu commands.

Key Questions

  • Q: How do I check disk space usage in Linux?
    • A: Use the du (disk usage) and ncdu (NCurses Disk Usage) commands. du provides quick summaries, and ncdu offers an interactive view.
  • Q: How do I find out which files and directories are taking up the most space on Linux?
    • A: Use du -h for human-readable sizes or ncdu for an interactive, size-sorted view. ncdu lists the largest items first, making it easy to find space hogs.
  • Q: How to troubleshoot low disk space on Linux?
    • A: Use du or ncdu to identify large files/directories. ncdu allows interactive deletion to free up space.
  • Q: What is the du command in Linux?
    • A: du (disk usage) estimates disk space used by files and directories.
  • Q: How do I use the du command to check the size of a directory?
    • A: Use du -sh /path/to/directory-s summarizes the total size, and -h displays it in a human-readable format (KB, MB, GB).
  • Q: What is ncdu in Linux?
    • A: ncdu (NCurses Disk Usage) is an interactive disk usage analyzer that shows you which directories/files are using the most space.
  • Q: How do I install ncdu on Linux? A: Use your distribution’s package manager:
    • Ubuntu/Debian: sudo apt-get install ncdu
    • Fedora/CentOS/RHEL: sudo yum install ncdu (or sudo dnf install ncdu)
    • Arch Linux/Manjaro: sudo pacman -S ncdu
  • Q: How do I use ncdu to analyze disk usage?
    • A: Run ncdu. It scans your current directory. Navigate with arrow keys, view details with “i”, and delete with “d”.
  • Q: How do I scan a specific directory with ncdu?
    • A: Use ncdu /path/to/directory.
  • Q: How to check directory size in Linux from the command line?
    • A: Use du -sh /path/to/directory.
  • Q: What is the command to check file size in Linux?
    • A: du -h filename (for disk usage) or ls -lh filename (for a simple listing). ncdu also shows file sizes.
  • Q: How do I check disk space on a remote Linux server?
    • A: Connect via SSH and then run du or ncdu as you would locally.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Leave a Reply