ncdu: Interactive Disk Usage Analyzer

ncdu: Interactive Disk Usage Analyzer

What you'll be able to do

  • Launch `ncdu` and reach the largest directories with the arrow keys alone
  • Explain the `s`, `n`, and `C` sort toggles and the `d` deletion flow
  • Choose between `-r` (read-only) and `-x` (one filesystem only)

Prerequisites (read these first)

What You'll Learn

  • How to use ncdu to navigate large directories interactively
  • How to find big files and delete them safely on the spot
  • How to switch from hammering du to a keystroke-only investigation
  • How to scan a remote server's disk over ssh

Target Audience: Linux beginners. Anyone tired of chasing space with du.

Words used in this article

  • Interactive: a style where you keep working from one screen with key presses, instead of retyping commands.
  • DirectoryA container that organizes files. Same idea as a "folder" on Windows or macOS.: the same thing Windows and macOS call a folder.
  • Scan: visiting the target files one by one to measure them. Also called "walking" the tree.
  • Filesystem: the structure that divides a disk for use. Read it as "a unit such as / or /home whose space is counted on its own".
  • Mount: attaching a disk to a directory so you can use it.
  • Standard output / standard input: where a command writes its result, and the opening through which a command receives input. Section 7 uses both.

ncdu can also be used purely for inspection. Launch it with -r and the delete function is disabled outright. Always start your practice with -r.

Note that ncdu is not part of the virtual terminalAn interactive program that reads the commands you type and runs them. on this site. Try it on a real Linux machine or WSL. That is exactly why you should begin with -r.

Introduction: Lina's "du Spam" Incident

Lina: Linny-senpai, the disk filled up, so I am hunting big directories with du.
Lina: But it is run du, cd deeper, run du again, cd again. This is taking forever.
Linny-senpai: That is the classic "du spam". Everyone goes through it once.
Linny-senpai: There is a dedicated tool called ncdu that does all of it with just the arrow keys.
Lina: Just arrow keys? No cd, no du?
Linny-senpai: Right. ncdu lets you walk around the screen like a file manager. You drill into the biggest directories and can even delete them right there.

The Short Answer

  • ncdu = NCurses Disk Usage. A tool that shows du results in an interactive screen
  • Move with arrow keys, delete in place with d, sort by size with s
  • Just install it with sudo apt install ncdu and launch ncdu /

1. What Is ncdu?

Conclusion: ncdu shows du results in a full-screen interactive view, so you can drill into the biggest paths with arrow keys.

Lina: What does ncdu stand for?
Linny-senpai: It stands for NCurses Disk Usage. NCurses is a library for building full-screen terminal screens. A library is a collection of programs you can reuse as parts.
Linny-senpai: ncdu uses that library to display du-style results on screen.
Lina: So it is like a beefed-up du?
Linny-senpai: That is a fair mental model. du just prints numbers.
Linny-senpai: ncdu sorts them biggest first. You press Enter to go in and the left arrow to come back. That is how you reach the space hog without getting lost.

ncdu vs du (in a nutshell)

  • du → run the command once and get a list of numbers. Then you cd and re-run it yourself
  • ncdu → scan once, then just walk around the screen to drill down

2. Install and Launch

Conclusion: ncdu is often not installed by default. Install it via apt / dnf, then launch with ncdu PATH.

Lina: I want to try it right now. How do I install it?
Linny-senpai: The package is named ncdu on every distribution. Let's start with the install.

Install

# Ubuntu / Debian family
$ sudo apt install ncdu

# RHEL / Fedora family
$ sudo dnf install ncdu

# Older CentOS, etc.
$ sudo yum install ncdu

Launch It

# Scan the current directory
$ ncdu

# Scan a specific path
$ ncdu /var

# Scan the whole root (sudo recommended)
$ sudo ncdu /
Lina: I ran ncdu / and it showed "Scanning..." and made me wait.
Linny-senpai: That is expected. On launch, ncdu scans everything under the given pathA string that describes the location of a file or directory. once.
Linny-senpai: Just like du, a large target takes a moment. When the scan finishes, it switches to the list view.

Use sudo for System-Wide Scans

Under / there are directories a normal user cannot read. Without sudo, those parts fail with "PermissionThe read / write / execute access rules set on a file or directory. denied" and go uncounted. The totals then come out smaller than reality.

For system investigation, sudo ncdu / is the baseline.

Conclusion: Up/down arrows select an item, Enter (or right arrow) enters a directory, left arrow goes back up, and q quits.

Linny-senpai: Once the scan finishes, you get a screen like this. The key point is that items are sorted biggest first.
--- /var ------------------------------------------------------
    1.2 GiB [##########] /log
  512.0 MiB [####      ] /cache
   24.0 MiB [          ] /lib
    4.0 KiB [          ] /games

How to Read It

  • The number on the left: the usage of that directory or file
  • [#### ]: a bar showing its share of the parent
  • The / before a name: the mark for a directory
  • The top --- /var ---: where you currently are

Core Key Bindings

Movement Keys (learn just these first)

Key Action
/ Select an item (move up/down)
Enter or Enter the selected directory
Go back up to the parent
r Rescan the current location
q Quit ncdu
? Show help (key list)

The r key inside the screen and the -r launch option are different things. The r key rescans. The -r option disables deletion.

Lina: So I move the cursor over the list and press Enter to go inside. It really is like a file manager.
Linny-senpai: It is. No cd and no du, just arrow keys to dive into the biggest directory. That is what makes ncdu pleasant.

4. Sorting and Display Toggles

Conclusion: s sorts by size, n by name, C by item count, and g toggles the percentage/graph display.

Lina: Can I change the sort order?
Linny-senpai: You can. The default is biggest first. You can switch it based on what you are after.

Sort and Display Keys

Key Action
s Sort by size (default)
n Sort by name
C Sort by item count (capital C)
g Toggle the percentage / graph bar display
a Toggle apparent size vs. actual disk usage

When to Use a (apparent size)

By default ncdu shows the size actually occupied on disk. Press a to switch to the file's apparent size.

The two diverge for sparse files and for large numbers of tiny files. Knowing the difference saves confusion.

5. Deleting on the Spot

Conclusion: Select an item and press d. A confirmation promptA symbol (like $ or #) shown when the shell is waiting for your input. appears, so you skip the round trip of finding with du and removing with rm.

Lina: I found the big directory. Do I quit ncdu and run rm to delete it?
Linny-senpai: No need. Inside ncdu, select the item you want gone and press d.
Linny-senpai: It asks for confirmation before deleting. So the whole "find with du, remove with rm" round trip disappears.

Deletion Steps

  1. Select the file or directory with /
  2. Press d
  3. A confirmation screen appears. The choices are yes, no, and don't ask me again
  4. Move between them with / and confirm with Enter. Press q to abort

Pre-Delete Checklist

  • Is the file truly unneeded (a log, a cache, or a temp file)?
  • Is a running service using it?
  • For large logs, emptying the file in place is sometimes safer than deleting it (truncate -s 0 logfile)

One note on the second point. Deleting a file a service still has open does not return the space. For that open-file problem, see Understanding du vs df.

6. Options That Matter in Practice

Conclusion: -x stays within one filesystem, -r is read-only, and -o / -f export and reuse a scan.

Linny-senpai: Knowing a few launch options makes your investigation more accurate and safer.

Common Launch Options

Option Meaning
-x Count only the same filesystem (don't cross mounts)
-r Launch in read-only mode (disables d deletion)
-o FILE Export the scan result to a file
-f FILE Import an exported result and display it
--exclude PATTERN Exclude matching paths from the count
Lina: Isn't -x the one that came up with du?
Linny-senpai: Good memory. It is the same as du -x.
Linny-senpai: When /home is a separate partition, it keeps you from double-counting across the boundary. That helps when you want to compare against df.

6-1. Lina Gets Stuck: d Almost Removed the Wrong Row

Lina: I was looking at /var and wanted to clear a big cache. I pressed d, and the name in the confirmation prompt was not the one I expected. That was a scare.
--- /var ------------------------------------------------------
    1.2 GiB [##########] /log
  512.0 MiB [####      ] /cache
   24.0 MiB [          ] /lib
Lina: I meant to delete /cache. The prompt said /log. My cursor was one row higher.
Linny-senpai: Good catch. In ncdu, d acts on the row that is currently selected. Where the cursor sits decides everything.
Lina: The list is sorted biggest first, so the top rows hold the most data. Almost deleting one is frightening.
Linny-senpai: That is why you should start with -r. The d key does nothing at all, which makes it ideal for practice.
Lina: That makes sense. I will read the name in the prompt out loud before pressing Enter.
# Practise in a state where deletion cannot happen
$ ncdu -r ~

Use -r When Safety Comes First

To prevent an accidental d keystroke, launch with sudo ncdu -r /. With -r the delete feature is disabled.

That makes it investigation-only, which is what you want when you are just looking at a production server.

7. Scanning a Remote Server's Disk

Conclusion: Open a remote scan result in your local ncdu so you can investigate without operating interactively on the server (the remote side still needs ncdu).

Lina: I want to check a server's disk, but working on that server's screen is awkward.
Linny-senpai: Sharp observation. ncdu can export and import scan results.
Linny-senpai: Combined with ssh, that lets you investigate from your own screen.

Pattern A: Scan Remotely, View Locally

# Run only the du-style scan remotely and pipe the result into your local ncdu
$ ssh user@server ncdu -o- / | ncdu -f-

How to Read It

  • ncdu -o-: writes the scan result to standard output (- means stdout)
  • ncdu -f-: reads the result from standard input and displays it
  • The | between them: pipes the remote output into your local ncdu

One caveat. Running -o still requires ncdu on the remote side. If it is not installed there, install it on the remote machine first.

Pattern B: Save the Result and Carry It Home

Instead of streaming it straight through, you save the result to a file first. A saved result can be reviewed later.

# On the server, save the result (gzip keeps it small)
$ ncdu -o- / | gzip > scan.gz

# Transfer and load it locally
$ scp user@server:scan.gz .
$ zcat scan.gz | ncdu -f-
Lina: So I can dump a scan to a file. If I save one periodically, I can trace when the usage grew.
Linny-senpai: That is a good instinct. Keep the exported files in date order. They become a ready-made trail for investigating capacity growth.

8. Mini Exercises: Try It on Your Box

Conclusion: Three drills — find your largest home directory, toggle sort modes, and launch read-only — to lock in the controls.

Lina: I have the knowledge now. I want to try it by hand.
Linny-senpai: Good, I prepared three exercises. Start on your own machine, not on production. With -r on, no deletion can happen.

Exercise 1: Find the largest directory directly under your home.

Show Hint 1 (Direction)

Launch it with the place you want to inspect. The list is sorted biggest first by default.

So the top row already answers the question.

Show Hint 2 (Command name)

Use ncdu. Your home directory can be written as ~. Add -r for safety.

Enter goes in, the left arrow goes back, and q quits.

Show Answer
$ ncdu -r ~
--- /home/user -------------------------------------------------
    2.5 GiB [##########] /Videos
  512.0 MiB [##        ] /Downloads
   64.0 MiB [          ] /Documents

The top row is the largest directory. In this example it is /Videos.

Press Enter to go inside and keep tracing the larger contents. The left arrow takes you one level back.

Exercise 2: Toggle the list between size order and name order.

Show Hint 1 (Direction)

A single key press switches it. You do not need to retype the command.

Each key is the first letter of an English word.

Show Hint 2 (Command name)

Size order is s (size). Name order is n (name). Alternate between them in the list view.

Show Answer

In the list view, alternate between s and n. The order changes instantly.

Press the same key again to flip between ascending and descending.

Exercise 3: Launch in a state where the delete key does nothing, and prove it to yourself.

Show Hint 1 (Direction)

A launch option disables the delete feature outright. The idea is to open it read-only.

The first letter of "read-only" is the clue.

Show Hint 2 (Command name)

Use ncdu -r. Point it at /tmp. After it opens, press d and watch what happens.

Show Answer
$ ncdu -r /tmp

Launching with -r disables the d key. No delete prompt appears.

Instead, the bottom of the screen shows File deletion disabled in read-only mode. That message is the proof -r is in effect.

Practise in this mode until you are comfortable.

9. Common Pitfalls

Conclusion: Watch out for missing sudo shrinking the numbers, an accidental d deleting the wrong thing, and aiming at / instead of your home.

Three Common Mistakes

  1. Scanning / without sudo → unreadable directories are skipped, so totals come out smaller than reality
  2. Spotting something big and pressing d at once → it has no effect when a service is using the file. Confirm what it is first
  3. Operating on production without -r → an accidental d deletes something. If you are just looking, always use -r

Safe Habits

  • For system scans, use sudo ncdu -x / (it counts only the same filesystem)
  • On production servers, launch with -r (read-only) by default
  • Confirm name, size, and timestamp before deleting. When in doubt, do not

10. Review

Lina: Let me sum up. ncdu scans once, and after that the arrow keys do all the drilling.
Linny-senpai: Right. Enter goes in and the left arrow comes back. Those two are enough.
Lina: And d acts on the selected row. I almost removed /log by accident.
Linny-senpai: Well remembered. Launch with -r until you are comfortable. That alone prevents nearly every accident.

Today's 3-Line Summary

  1. ncdu shows du results in an interactive screen, so the arrow keys do all the drilling
  2. s sorts by size and n sorts by name. d deletes the row that is selected
  3. Add -r (read-only) for practice and for inspection. Add sudo and -x for system scans

Next Reading

Share this article

Next steps