磁碟快滿了,卻不知道空間被誰吃掉?du 逐層追下去很累,ncdu 提供了一個帶有進度條與方向鍵操作的互動式介面,能很快把「兇手目錄」找出來。
ncdu 不在 CentOS 的官方 media 源裡,需要先掛上 EPEL:
[root@wangjianxiong ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm warning: /var/tmp/rpm-tmp.DWv51E: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY Preparing... ########################################### [100%] 1:epel-release ########################################### [100%] [root@wangjianxiong ~]# ls /etc/yum.repos.d/ epel.repo epel-testing.repo media.repo
出現 NOKEY 的警告只是提示尚未匯入公鑰,安裝過程中 yum 會自動補上,不必理會。
[root@wangjianxiong ~]# yum -y install ncdu ---> Package ncdu.x86_64 0:1.11-1.el6 will be installed Dependencies Resolved Installing: ncdu x86_64 1.11-1.el6 epel 43 k Transaction Summary Install 1 Package(s) Total download size: 43 k Installed size: 76 k Downloading Packages: ncdu-1.11-1.el6.x86_64.rpm | 43 kB 00:00 Importing GPG key 0x0608B895: Userid : EPEL (6) <epel@fedoraproject.org> Package: epel-release-6-8.noarch (installed) Running Transaction Installing : ncdu-1.11-1.el6.x86_64 1/1 Verifying : ncdu-1.11-1.el6.x86_64 1/1 Installed: ncdu.x86_64 0:1.11-1.el6 Complete!
如果下載時遇到憑證相關的錯誤訊息,yum 會自動換一個鏡像站重試,通常不影響結果。
[root@wangjianxiong ~]# ncdu / ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help --- / --------------------------------------------------------- 4.2 GiB [##########] /media 2.9 GiB [###### ] /usr 174.3 MiB [ ] /var 148.0 MiB [ ] /lib 36.5 MiB [ ] /etc 25.6 MiB [ ] /lib64 23.8 MiB [ ] /boot 13.8 MiB [ ] /sbin 7.2 MiB [ ] /bin 1.4 MiB [ ] /root 392.0 KiB [ ] /dev 68.0 KiB [ ] /tmp e 16.0 KiB [ ] /lost+found Total disk usage: 7.5 GiB Apparent size: 7.5 GiB Items: 163855
條狀圖越長代表佔用越多。用上下鍵移動、Enter 進入子目錄、左鍵返回上層,可以直接在畫面裡一路往下挖;游標處的 e 表示該目錄目前沒有可讀權限而略過,. 則是正在計算中。
掃描根目錄會跑一陣子,實務上比較常直接指定可疑路徑,例如 ncdu /var 查日誌是否爆量,或 ncdu /home 看使用者資料。刪除檔案前請務必確認,ncdu 的刪除操作不會進入回收機制。