磁盘使用分析工具ncdu 使用

17 min read

安装

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 <options> <directory>

查看当前目录磁盘使用,直接:

ncdu
# 按 Q 键退出

查看指定目录磁盘使用:

ncdu /  #根目录,全盘
ncdu /home
# 按 Q 键退出

命令帮助:

ncdu -h
ncdu <options> <directory>

  -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

详细帮助请查看:man ncdu

ncdu 用法

进入 ncdu 后:

按 ?调出帮助对话框,按 1 显示快捷键(默认),按 2 显示格式说明,按 3 显示版本信息

常用快捷键:

n :按文件名进行排序(再按为切换升降序)
s :按文件大小进行排序(默认,再按为切换升降序)
r :重新计算大小
g :显示百分比

d: 删除

按 q 退出。

┌───ncdu help─────────────────1:Keys───2:Format───3:About──┐
│                                                          │
│       up, k  Move cursor up                              │
│     down, j  Move cursor down                            │
│ right/enter  Open selected directory                     │
│  left, <, h  Open parent directory                       │
│           n  Sort by name (ascending/descending)         │
│           s  Sort by size (ascending/descending)         │
│           C  Sort by items (ascending/descending)        │
│           d  Delete selected file or directory           │
│           t  Toggle dirs before files when sorting       │
│           g  Show percentage and/or graph                │
│                        -- more --                        │
│                                         Press q to close │
└──────────────────────────────────────────────────────────┘