Linux磁碟空間佔滿問題快速排雷
反手一個 df
先看是否是真滿了(參考 df(1) - Linux man page )
需要注意,如果磁碟空間未滿,但是仍然報 No space left on device
,需要執行 df -i
排查inode
$ df -h 檔案系統容量已用可用 已用% 掛載點 udev47G047G0% /dev tmpfs9.4G620M8.8G7% /run /dev/sda21.1T1.1T0G100% / tmpfs47G8.7M47G1% /dev/shm tmpfs5.0M4.0K5.0M1% /run/lock tmpfs47G047G0% /sys/fs/cgroup /dev/sda1511M1.2M510M1% /boot/efi tmpfs9.4G108K9.4G1% /run/user/1000 tmpfs9.4G40K9.4G1% /run/user/0 $ df -i 檔案系統Inode 已用(I)可用(I) 已用(I)% 掛載點 udev12277823525 122772981% /dev tmpfs122852551368 122838871% /run /dev/sda271041024 2250210 687908144% / tmpfs1228525583 122851721% /dev/shm tmpfs122852555 122852501% /run/lock tmpfs1228525517 122852381% /sys/fs/cgroup /dev/sda1000- /boot/efi tmpfs1228525544 122852111% /run/user/1000 tmpfs1228525515 122852401% /run/user/0
通過 df -h
只能看出磁碟滿了,但是看不出每個資料夾的大小,所以需要使用 du -ahd1
,如果檔案不是很多,很大,一般速度還能接受,但是今天執行相當慢,所以 Ctrl+C
中止。
此處簡單說明一下 -ahd1
的意思(可以通過 man du
或者 du --help
自行查閱幫助文件,參考 du(1) - Linux man page )
$ du --help 用法:du [選項]... [檔案]... 或:du [選項]... --files0-from=F -a, --allwrite counts for all files, not just directories (所有檔案,不止目錄) -h, --human-readableprint sizes in human readable format (e.g., 1K 234M 2G) (易讀單位,會損失精度) -d, --max-depth=Nprint the total for a directory (or file, with --all) (只掃描一層目錄) only if it is N or fewer levels below the command line argument;--max-depth=0 is the same as --summarize
$ du -ahd1 / 1.6M/dev 4.0K/mnt 4.0K/lib64 455M/boot 0/sys 0/vmlinuz.old 689G/data 370M/tmp 15M/etc 8.0K/media du: 無法訪問'/proc/24390/task/24390/fd/4': 沒有那個檔案或目錄 du: 無法訪問'/proc/24390/task/24390/fdinfo/4': 沒有那個檔案或目錄 du: 無法訪問'/proc/24390/fd/3': 沒有那個檔案或目錄 du: 無法訪問'/proc/24390/fdinfo/3': 沒有那個檔案或目錄 ^C # 慢的要死,Ctrl+C 終止
如果被刪除的檔案 df -h
快滿了,而 du -ahd1
卻很小,往往是檔案被刪除,而檔案控制代碼沒釋放導致的,祭出 lsof | grep deleted
,解決辦法,要麼kill掉pid,釋放控制代碼(治本),要麼就 > /path/to/deleted/file
把內容覆蓋掉(治標)。當然還有別的玩法,比如,不小心 rm -rf /
了,先彆著急跑路,萬一 lsof | grep deleted
還存在的,都還有救,約等於windows下的回收站的作用。 參考 lsof(8) - Linux man page
$ lsof -i | grep deleted # 當然也不快
ncdu
針對 du -d1
大檔案場景下的龜速表現,有人開發了ncdu,以ubuntu為例
# 從APT安裝(版本較舊目前是v1.11) $ sudo apt-get update $ sudo apt-get install -y ncdu # 從原始碼編譯安裝(此處是1.14版本) $ sudo apt-get install -y libncurses5-dev # 如果不安裝會報 configure: error: required header file not found $ wget https://dev.yorhel.nl/download/ncdu-1.14.tar.gz $ tar zxf ncdu-1.14.tar.gz $ cd ncdu-1.14 $ ./configure --prefix=/usr $ make && make install
參考官方文件 Ncdu Manual
額外
通過man查詢命令時,手冊中會帶有數字(例如 du(1)
, lsof(8)
),這代表的是手冊的不同部分,可以通過 man man
或者 Linux man pages 來檢視
MANUAL SECTIONS The standard sections of the manual include: 1User Commands 2System Calls 3C Library Functions 4Devices and Special Files 5File Formats and Conventions 6Games et. al. 7Miscellanea 8System Administration tools and Daemons Distributions customize the manual section to their specifics, which often include additional sections.
參考資料
- What do the numbers in a man page mean?
- df(1) - Linux man page
- du(1) - Linux man page
- lsof(8) - Linux man page
- Ncdu Manual
招聘小廣告
山東濟南的小夥伴歡迎投簡歷啊 加入我們 , 一起搞事情。
長期招聘,Java程式設計師,大資料工程師,運維工程師。
謝謝支援
支付寶
微信