Linux 刪了日誌,磁碟為什麼還是滿的?看懂開啟中的檔案 / Why Deleting a Linux Log May Not Free Disk Space
刪掉的是什麼? / What was actually removed? 假設你在 Linux 伺服器刪掉一個很大的舊日誌,目錄裡已經找不到它,磁碟用量卻幾乎沒變。先別急著再刪一輪:檔名消失,不一定代表資料已經釋放。本文聚焦一種常見原因,也就是仍有程序開著那個檔案,不把所有空間問題都歸到同一個答案。 Imagine deleting a large old log on a Linux server. It disappears from the directory, yet disk usage barely changes. Resist deleting more files: a missing name does not necessarily mean the data has been released. This article focuses on one common cause—a process still has the file open—not a universal explanation for every space problem. 依 Linux 系統呼叫手冊,unlink 移除的是檔案系統中的一個名稱。只有當它是最後一個硬連結,而且沒有程序開著檔案,佔用的空間才會供重新使用。硬連結可以理解成同一份檔案的另一個名字;刪掉其中一個名字,並不是刪掉其他名字。[1] According to the Linux system-call manual, unlink removes a name from the filesystem. The space becomes reusable when that was the last hard link and no process has the file open. A hard link is another name for the same file; removing one name does not remove the others. [1] 程序開啟檔案後,會取得檔案描述符,也就是用來操作已開啟檔案的編號。即使最後一個名字被移除,只要仍有描述符指向它,檔案就會繼續存在,直到最後一個相關描述符關閉。因此,目錄中的名稱與程序手上的開啟狀態,要分成兩條線來看。[1][2...