NCDU: Complete Guide to Interactive Disk Usage Analysis

Master NCDU, the powerful command-line disk usage analyzer with interactive interface. Learn installation, usage, and advanced features for optimal storage management.

NCDU: Comprehensive Disk Usage Analysis Tool

Introduction

NCDU (NCurses Disk Usage) is a powerful command-line disk usage analyzer that provides an interactive interface for exploring directory structures and identifying space-consuming files and folders. Unlike traditional disk usage tools such as du or df, ncdu offers a user-friendly curses-based interface that allows users to navigate through directories interactively and perform actions on files and folders directly from the interface.

The tool is particularly valuable for system administrators, developers, and users who need to analyze disk usage patterns, identify large files, clean up storage space, and maintain optimal disk utilization across various systems.

Installation Methods

Ubuntu/Debian Systems

`bash sudo apt update sudo apt install ncdu `

Red Hat/CentOS/Fedora Systems

`bash

For newer versions with dnf

sudo dnf install ncdu

For older versions with yum

sudo yum install ncdu `

macOS Installation

`bash

Using Homebrew

brew install ncdu

Using MacPorts

sudo port install ncdu `

Arch Linux

`bash sudo pacman -S ncdu `

Building from Source

`bash wget https://dev.yorhel.nl/download/ncdu-2.3.tar.gz tar -xzf ncdu-2.3.tar.gz cd ncdu-2.3 ./configure make sudo make install `

Basic Usage and Syntax

The basic syntax for ncdu follows this pattern:

`bash ncdu [OPTIONS] [DIRECTORY] `

Simple Usage Examples

`bash

Analyze current directory

ncdu

Analyze specific directory

ncdu /home/user/Documents

Analyze root filesystem

sudo ncdu /

Analyze with specific options

ncdu -x /var/log `

Command-Line Options and Parameters

| Option | Long Form | Description | |--------|-----------|-------------| | -h | --help | Display help information and exit | | -V | --version | Show version information | | -x | --one-file-system | Stay within single filesystem | | -e | --extended | Show extended information | | -r | --read-only | Read-only mode, disable file deletion | | -o FILE | --output=FILE | Export results to file | | -f FILE | --file=FILE | Import results from file | | -0 | --null | Use null separator for filenames | | -1 | --one-file-system | Do not cross filesystem boundaries | | -2 | --exclude-kernfs | Exclude kernel filesystems | | --si | --si | Use SI units (powers of 1000) | | --disk-usage | --disk-usage | Show apparent size instead of disk usage |

Detailed Option Explanations

#### Filesystem Boundary Control `bash

Stay within single filesystem (useful for mounted drives)

ncdu -x /home

Exclude kernel filesystems (proc, sys, dev)

ncdu --exclude-kernfs / `

#### Export and Import Functionality `bash

Export scan results to file

ncdu -o /tmp/disk_usage.txt /home

Import previously saved results

ncdu -f /tmp/disk_usage.txt `

#### Read-Only Mode `bash

Prevent accidental file deletion

ncdu -r /important/directory `

Interactive Interface Navigation

Key Bindings and Controls

| Key | Action | |-----|--------| | | Navigate up/down through items | | | Enter/exit directories | | Enter | Enter selected directory | | n | Sort by name | | s | Sort by size | | C | Sort by items count | | M | Sort by modification time | | d | Delete selected item | | t | Toggle between apparent size and disk usage | | g | Show percentage and/or graph | | a | Toggle between apparent size and disk usage | | c | Show item counts | | m | Show modification time | | e | Show/hide hidden files | | i | Show information about selected item | | r | Recalculate sizes | | b | Spawn shell in current directory | | q | Quit ncdu | | ? | Show help screen |

Navigation Examples

`bash

Basic navigation workflow

ncdu /var/log

Use arrow keys to navigate

Press Enter to enter directories

Press 'd' to delete files (if not in read-only mode)

Press 'q' to quit

`

Advanced Usage Scenarios

System Administration Tasks

#### Analyzing System Directories `bash

Analyze system logs

sudo ncdu /var/log

Check temporary files

ncdu /tmp

Examine user home directories

sudo ncdu /home

Analyze package cache

ncdu /var/cache `

#### Server Maintenance `bash

Check web server directories

sudo ncdu /var/www

Analyze database directories

sudo ncdu /var/lib/mysql

Check mail directories

sudo ncdu /var/mail `

Development Environment Analysis

`bash

Analyze project directories

ncdu ~/projects

Check node_modules sizes

ncdu ~/projects/web-app/node_modules

Examine build artifacts

ncdu ~/projects/app/build `

Practical Examples and Use Cases

Example 1: Cleaning Up Docker Images

`bash

Analyze Docker directory

sudo ncdu /var/lib/docker

Navigate to find large unused images

Use 'd' key to delete unnecessary files

`

Example 2: Log File Management

`bash

Check log file sizes

sudo ncdu /var/log

Identify large log files

Navigate to specific application logs

Delete or archive old logs as needed

`

Example 3: Home Directory Cleanup

`bash

Analyze user home directory

ncdu ~

Common large directories to check:

~/.cache

~/Downloads

~/.local/share

~/Documents

`

Example 4: Development Project Analysis

`bash

Analyze entire development workspace

ncdu ~/development

Check for large dependencies

ncdu ~/development/project/node_modules ncdu ~/development/project/vendor

Identify build artifacts

ncdu ~/development/project/dist ncdu ~/development/project/build `

Export and Import Functionality

Exporting Scan Results

`bash

Export current directory analysis

ncdu -o ~/disk_analysis_$(date +%Y%m%d).txt /home/user

Export with specific format

ncdu -1 -o /tmp/filesystem_usage.txt / `

Importing Saved Results

`bash

Import previously saved analysis

ncdu -f ~/disk_analysis_20231201.txt

This allows offline analysis of disk usage data

`

Batch Processing Example

`bash #!/bin/bash

Script to generate daily disk usage reports

DATE=$(date +%Y%m%d) REPORT_DIR="/var/reports/disk_usage"

Create report directory

mkdir -p $REPORT_DIR

Generate reports for key directories

sudo ncdu -o "$REPORT_DIR/root_$DATE.txt" / sudo ncdu -o "$REPORT_DIR/home_$DATE.txt" /home sudo ncdu -o "$REPORT_DIR/var_$DATE.txt" /var sudo ncdu -o "$REPORT_DIR/usr_$DATE.txt" /usr

echo "Disk usage reports generated in $REPORT_DIR" `

Performance Optimization and Best Practices

Scanning Large Filesystems

`bash

For very large filesystems, exclude unnecessary directories

ncdu --exclude /proc --exclude /sys --exclude /dev /

Use single filesystem option for mounted drives

ncdu -x /home

For network filesystems, consider using local analysis

`

Memory Usage Considerations

| Filesystem Size | Expected RAM Usage | Recommendation | |----------------|-------------------|----------------| | < 1 GB | < 10 MB | Standard usage | | 1-10 GB | 10-100 MB | Monitor memory | | 10-100 GB | 100 MB - 1 GB | Consider exclusions | | > 100 GB | > 1 GB | Use targeted scans |

Best Practices for System Analysis

1. Run with appropriate privileges `bash # Use sudo for system directories sudo ncdu /var # Regular user for home directories ncdu ~ `

2. Exclude unnecessary filesystems `bash # Exclude virtual filesystems ncdu --exclude-kernfs / `

3. Use read-only mode for critical systems `bash # Prevent accidental deletion ncdu -r /production/data `

Integration with System Monitoring

Combining with Other Tools

`bash

Use with find for specific file types

find /home -name "*.log" -size +100M -exec ls -lh {} \; ncdu /home

Combine with df for filesystem overview

df -h ncdu /

Use with du for comparison

du -sh /var/log/* ncdu /var/log `

Automated Monitoring Script

`bash #!/bin/bash

Automated disk usage monitoring script

THRESHOLD=80 MOUNT_POINT="/"

Get current disk usage percentage

USAGE=$(df "$MOUNT_POINT" | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "Disk usage is ${USAGE}% - Running detailed analysis" # Generate detailed report ncdu -o "/tmp/emergency_disk_analysis_$(date +%s).txt" "$MOUNT_POINT" # Send alert (customize as needed) echo "Disk usage critical: ${USAGE}%" | mail -s "Disk Alert" admin@example.com fi `

Troubleshooting Common Issues

Permission Denied Errors

`bash

Issue: Cannot access system directories

Solution: Use sudo

sudo ncdu /var/log

Issue: Mixed permissions in directory tree

Solution: Run as root or use appropriate user

sudo ncdu /home `

Memory Issues with Large Filesystems

`bash

Issue: ncdu consumes too much memory

Solution: Use exclusions and targeted scanning

ncdu -x --exclude /proc --exclude /sys /

Alternative: Scan subdirectories separately

ncdu /var/log ncdu /var/cache ncdu /var/lib `

Slow Scanning Performance

`bash

Issue: Scanning takes too long

Solution: Exclude network mounts and virtual filesystems

ncdu --exclude-kernfs -x /

Issue: Network filesystem scanning

Solution: Use local analysis or exclude network mounts

ncdu --exclude /mnt/network_drive / `

Comparison with Alternative Tools

NCDU vs DU

| Feature | NCDU | DU | |---------|------|-----| | Interface | Interactive curses | Command-line output | | Navigation | Real-time browsing | Static output | | File operations | Delete from interface | External commands needed | | Memory usage | Higher (stores tree) | Lower (streaming) | | Export capability | Yes | Limited | | Sorting options | Multiple interactive | Command-line only |

NCDU vs Graphical Tools

| Aspect | NCDU | Baobab/WinDirStat | |--------|------|------------------| | Resource usage | Low | Higher | | Remote access | SSH compatible | GUI required | | Automation | Scriptable | Limited | | Server usage | Ideal | Not suitable | | Visual representation | Text-based | Graphical |

Security Considerations

Safe Usage Practices

`bash

Always use read-only mode on production systems

ncdu -r /production/data

Verify before deletion

ncdu shows confirmation prompts before deletion

Use appropriate user privileges

Don't run as root unless necessary

ncdu ~/documents # Good sudo ncdu /system # Only when needed `

File Deletion Safety

`bash

ncdu provides multiple confirmation levels:

1. Initial 'd' key press

2. Confirmation prompt

3. Progress indicator for large operations

To completely disable deletion:

ncdu -r /important/directory `

Advanced Configuration and Customization

Environment Variables

`bash

Set default options

export NCDU_OPTS="-x --exclude-kernfs"

Custom color scheme (if supported)

export NCDU_COLOR=1 `

Creating Wrapper Scripts

`bash #!/bin/bash

Custom ncdu wrapper script

Default options

DEFAULT_OPTS="-x --exclude-kernfs"

Check if directory argument provided

if [ $# -eq 0 ]; then DIRECTORY="." else DIRECTORY="$1" fi

Run ncdu with default options

ncdu $DEFAULT_OPTS "$DIRECTORY" `

Conclusion

NCDU represents a powerful and efficient solution for disk usage analysis, combining the accessibility of command-line tools with an intuitive interactive interface. Its ability to provide real-time navigation through filesystem hierarchies, coupled with built-in file management capabilities, makes it an indispensable tool for system administrators, developers, and power users.

The tool's strength lies in its simplicity and effectiveness. Unlike complex graphical alternatives, ncdu operates efficiently in resource-constrained environments while providing comprehensive functionality for disk usage analysis. Its export and import capabilities enable automated monitoring and reporting, while the interactive interface facilitates immediate action on identified issues.

Whether used for routine system maintenance, emergency disk space recovery, or development environment optimization, ncdu provides the necessary tools and flexibility to manage disk usage effectively across diverse computing environments. The combination of powerful analysis capabilities, safety features, and efficient operation makes ncdu a valuable addition to any system administrator's toolkit.

Regular use of ncdu as part of system maintenance routines can prevent disk space issues, optimize storage utilization, and maintain system performance. Its integration capabilities with other system tools and scripting environments further enhance its utility in automated monitoring and maintenance workflows.

Tags

  • Command Line
  • disk-usage
  • ncdu
  • storage-analysis
  • system-tools

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

NCDU: Complete Guide to Interactive Disk Usage Analysis