ncdu
Tue 23 September 2025
ncdu
https://dev.yorhel.nl/ncdu
ncdu是看目录大小(包含子文件)的命令行工具,支持鼠标点击控制。
ncdu
耐心等待一下,马上就有一个界面:
按q就可以停止。Ctrl+C、Ctrl+Z应该也可以停止。
ncdu (NCurses Disk Usage) 是一个用于 Linux 和类 unix 系统的磁盘实用程序,它提供了一种比 du 等传统命令更具交互性和用户友好性的方式来查看和分析磁盘空间使用情况。它提供了一个导航界面,允许您轻松识别哪些目录和文件占用了最多的磁盘空间。 安装
Ubuntu/Debian
sudo apt update sudo apt install ncdu
RedHat/CentOS/Fedora
sudo yum install ncdu
or
sudo dnf install ncdu
示例用法 扫描当前目录
它将列出当前目录中的所有目录和文件,并按磁盘使用量排序。最大的文件和目录将首先列出。
ncdu
image
支持的键
↑,↓ or k,j to Move →,l to enter ←,h to return g toggle graph c toggle counts a toggle average size in directory m toggle modified time u toggle human-readable format n,s,C,A,M sort by name,size,count,asize,mtime d delete file/directory v select file/directory V enter visual select mode D delete selected files/directories y copy current path to clipboard Y display current path ^L refresh screen (fix screen corruption) r recalculate file sizes ? to toggle help on and off q/ESC/^c to quit
扫描特定目录
ncdu /path/to/directory
示例
ncdu ~/Documents
ncdu 中的常用导航
描完成后,将看到一个屏幕,其中显示指定目录中的目录和文件及其磁盘使用情况。
箭头键(上/下):在目录和文件列表中上
回车/右箭头:进入选定的目录
左箭头:返回父目录
q:退出 ncdu 程序
d:删除选择的文件或目录
?:显示帮助信息
n:按名称(而不是磁盘使用情况)对输出进行排序
s:按大小对输出进行排序(默认排序方法)
排除特定目录
如果不想在扫描中包含某些目录,可以使用 -x 选项排除单独挂载的文件系统(对于排除外部驱动器或网络挂载很有用)
ncdu -x /path/to/directory
显示隐藏的文件
默认情况下,ncdu 可能不会显示隐藏文件(以点开头的文件,如 .bashrc)
ncdu -a /path/to/directory
扫描多个目录
ncdu /path/to/dir1 /path/to/dir2
排除特定文件或目录
-x 或 --exclude
ncdu --exclude /path/to/exclude /path/to/directory
保存输出
可以使用 -o(输出)标志将扫描结果保存到文件中。允许生成报告以供以后分析
ncdu -o output_file /path/to/directory
加载保存的输出文件
ncdu -f output_file
扫描家目录
ncdu ~
简介
Ncdu - NCurses Disk Usage
Ncdu 是一个带有 ncurses 接口的磁盘使用分析器。 它旨在在您没有完整图形设置可用的远程服务器上查找空间占用,但即使在常规桌面系统上它也是一个有用的工具。 Ncdu 的目标是快速、简单和易于使用,并且应该能够在安装了 ncurses 的任何最小的类 POSIX 环境中运行。
主页:https://dev.yorhel.nl/ncdu
备注:curses 是一个在 Linux/Unix 下广泛应用的图形函数库,作用是可以在终端内绘制简单的图形用户界面。curses 库产生于 BSD UNIX,但是 Linux 系统通过 ncurses 库提供这个功能。
安装
ncdu 可以在绝大多数 posix 兼容的系统中运行,几乎在所有的知名 Unix、Linux 系统中可用。
macOS:
Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install ncdu
FreeBSD:
pkg install ncdu
Ubuntu:
sudo apt install ncdu
CentOS:
Add EPEL repo
yum -y install epel-release
yum install ncdu
命令用法
ncdu
查看当前目录磁盘使用,直接:
ncdu
按 Q 键退出
查看指定目录磁盘使用
ncdu / #根目录,全盘 ncdu /home
按 Q 键退出
命令帮助: 复制代码
ncdu -h
ncdu
-h,--help This help message -q Quiet mode, refresh interval 2 seconds -v,-V,--version Print version -x Same filesystem -e Enable extended information -r Read only -o FILE Export scanned directory to FILE -f FILE Import scanned directory from FILE -0,-1,-2 UI to use when scanning (0=none,2=full ncurses) --si Use base 10 (SI) prefixes instead of base 2 --exclude PATTERN Exclude files that match PATTERN -X, --exclude-from FILE Exclude files that match any pattern in FILE -L, --follow-symlinks Follow symbolic links (excluding directories) --exclude-caches Exclude directories containing CACHEDIR.TAG --confirm-quit Confirm quitting ncdu --color SCHEME Set color scheme (off/dark)
复制代码
彩色显示:
ncdu --color dark # off/dark,默认 off ncdu --color dark /home
ncdu 用法
进入 ncdu 后:
按 ?调出帮助对话框,按 1 显示快捷键(默认),按 2 显示格式说明,按 3 显示版本信息
常用快捷键:
n :按文件名进行排序(再按为切换升降序) s :按文件大小进行排序(默认,再按为切换升降序) r :重新计算大小 g :显示百分比
按 q 退出。
一、ncdu命令命令介绍 1.1 ncdu简介
ncdu 是一个基于 ncurses 的磁盘使用情况分析工具。它可以帮助您快速地查看文件系统中哪些文件或目录占用了较多的空间,并且以易于阅读的树状视图形式展示结果。
1.2 ncdu特点
快速扫描: ncdu 提供了快速的磁盘使用情况扫描,帮助您快速识别哪些文件或目录占用了大量空间。
交互式界面: ncdu 提供了一个基于 ncurses 的交互式界面,允许您轻松浏览文件系统结构。
颜色编码: 默认情况下,ncdu 使用颜色编码来区分文件类型和目录,使得输出更加直观。
灵活的选项: ncdu 支持多种选项,比如排除某些目录、按照大小排序、以及导出和导入扫描结果。
易于操作: ncdu 提供了简单的键盘命令,如使用方向键浏览文件列表、使用 Enter 键进入子目录等。
二、本地环境介绍 2.1 本地环境规划
本次实践为个人测试环境,操作系统版本为Ubuntu 22.04.1。
hostname 本地P地址 操作系统版本 内核版本 node版本 ubuntu-001 192.168.3.251 Ubuntu 22.04.1 LTS 5.15.0-89-generic v18.0.0 2.2 本次实践介绍
1.本次实践部署环境为个人测试环境,生产环境请谨慎; 2.在Ubuntu环境下ncdu命令的安装及基本使用。
三、检查本地环境 3.1 检查本地操作系统版本
检查本地操作系统版本,当前版本为Ubuntu 22.04.1 LTS。
root@ubuntu-001:~# cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.1 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.1 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy
3.2 检查系统内核版本
检查系统内核版本,当前内核版本为5.15.0-89-generic。
root@ubuntu-001:~# uname -r 5.15.0-97-generic
3.3 检查系统镜像源
对于Ubuntu 22.04.1 LTS版本,将镜像源配置文件/etc/apt/sources.list修改内容如下。
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
3.4 更新软件列表
执行以下命令,更新软件列包表。
root@ubuntu-001:~# apt update Hit:1 https://mirrors.aliyun.com/ubuntu jammy InRelease Hit:2 https://mirrors.aliyun.com/ubuntu jammy-security InRelease Hit:3 https://mirrors.aliyun.com/ubuntu jammy-updates InRelease Hit:4 https://mirrors.aliyun.com/ubuntu jammy-backports InRelease Hit:5 https://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done 116 packages can be upgraded. Run 'apt list --upgradable' to see them.
四、安装ncdu工具 4.1 安装ncdu软件
在ubuntu系统下,直接执行以下命令,安装ncdu。
apt install ncdu -y
在这里插入图片描述 4.2 ncdu命令帮助
help帮助信息
root@ubuntu-001:~# ncdu --help
ncdu
-h,--help This help message -q Quiet mode, refresh interval 2 seconds -v,-V,--version Print version -x Same filesystem -e Enable extended information -r Read only -o FILE Export scanned directory to FILE -f FILE Import scanned directory from FILE -0,-1,-2 UI to use when scanning (0=none,2=full ncurses) --si Use base 10 (SI) prefixes instead of base 2 --exclude PATTERN Exclude files that match PATTERN -X, --exclude-from FILE Exclude files that match any pattern in FILE -L, --follow-symlinks Follow symbolic links (excluding directories) --exclude-caches Exclude directories containing CACHEDIR.TAG --exclude-kernfs Exclude Linux pseudo filesystems (procfs
,sysfs
,cgroup
,...) --confirm-quit Confirm quitting ncdu --color SCHEME Set color scheme (off/dark)
详细解释
Option Description -h, --help 显示帮助消息。 -q 安静模式,刷新间隔为 2 秒。 -v, -V, --version 打印版本信息。 -x 仅在同一文件系统内操作。 -e 启用扩展信息。 -r 读取模式,禁止修改文件。 -o FILE 将扫描的目录导出到文件 FILE。 -f FILE 从文件 FILE 导入扫描的目录。 -0, -1, -2 设置扫描时使用的用户界面(0=无,2=完整的 ncurses)。 --si 使用以 10 为基数的 SI 前缀而不是以 2 为基数的前缀。 --exclude PATTERN 排除匹配模式 PATTERN 的文件。 -X, --exclude-from FILE 从文件 FILE 中读取排除模式。 -L, --follow-symlinks 跟随符号链接(不包括目录)。 --exclude-caches 排除包含 CACHEDIR.TAG 的目录。 --exclude-kernfs 排除 Linux 的伪文件系统(如 procfs, sysfs, cgroup 等)。 --confirm-quit 在退出 ncdu 之前进行确认。 --color SCHEME 设置颜色方案(off/dark)。 五、ncdu命令的基本使用 5.1 直接启动ncdu
直接在命令行,输入ncdu命令,此时会从当前目录开始分析磁盘使用情况。
ncdu
在这里插入图片描述
ncdu界面的基本交互命令,通过这些命令你可以浏览和管理文件系统的磁盘使用情况。
键 功能 up, k 将光标向上移动 down, j 将光标向下移动 right/enter 打开选定的目录 left, <, h 打开父目录 n 按名称排序(升序/降序) s 按大小排序(升序/降序) C 按项目数排序(升序/降序) M 按修改时间排序(需启用 -e 选项) d 删除选定的文件或目录 t 在排序时切换目录在文件之前/之后 5.2 指定路径
可以在ncdu命令后面,加上需要分析的目录路径。如果需要查看某个特定目录下的磁盘使用情况,只需在命令行后面加上该目录的路径即可。
ncdu /var/log/
在这里插入图片描述 5.3 结果保存到文件
将/home目录的磁盘使用情况保存到文件 disk_usage.txt 中:
ncdu -o disk_usage.txt /home
在这里插入图片描述
查看保存文件内容
cat disk_usage.txt
在这里插入图片描述 5.4 从文件导入扫描的目录
可以使用-f选项将文件导入扫描的目录
ncdu -f disk_usage.txt
在这里插入图片描述 5.5 设置颜色方法
执行以下命令,ncdu 会扫描 /home 目录及其子目录,并以颜色高亮的方式显示每个子目录占用的空间。颜色方案会帮助你更容易地区分文件和目录,并识别哪些部分占用了大量磁盘空间。
ncdu --color dark /home
ncdu(NCurses 磁盘使用情况)是一款基于 ncurses 的磁盘空间分析工具,它以图形化的树状视图形式展示了磁盘使用情况,帮助用户直观地识别哪些文件或目录占用了大量的磁盘空间。
与传统的 du 命令相比,ncdu 提供了更为直观的交互式界面,使得管理磁盘空间变得更加容易和高效。
Category: 待整理