Directory

為什麼刪除條目後具有大量條目的目錄不會縮小大小?

  • January 17, 2019

關閉投票通知:雖然連結的副本提出了相同的問題,但評論和唯一答案並未提供來自權威來源的答案。Thomas Dickey 對我的問題的回答實際上提供了我一直在尋找的答案。因此,建議的副本沒有幫助,但是托馬斯在這裡的回答是正確的。

根據G-Man在此 PerlDuck 的評論中對超級使用者問題和個人帳戶的回答,具有大量條目的目錄的大小超過 4096 字節(可以在輸出中看到),但是一旦刪除了條目 -數字永遠不會下降。ls -l

問題是“為什麼”?是否由於ext4文件系統配置為保留目錄元數據?顯然刪除目錄並重新創建它不是一個解決方案,因為它會刪除原始 inode 並創建一個新的。可以做些什麼來手動減少數字?

引用開發人員的話(在 linux 核心執行緒中,ext3/ext4 目錄在刪除大量文件後不會縮小):

On Thu, May 14, 2009 at 08:45:38PM -0400, Timo Sirainen wrote:
>
> I was rather thinking something that I could run while the system was  
> fully operational. Otherwise just moving the files to a temp directory +
> rmdir() + rename() would have been fine too.
>
> I just tested that xfs, jfs and reiserfs all shrink the directories  
> immediately. Is it more difficult to implement for ext* or has no one  
> else found this to be a problem?

It's probably fairest to say no one has thought it worth the effort.
It would require some fancy games to swap out block locations in the
extent trees (life would be easier with non-extent-using inodes), and
in the case of htree, we would have to keep track of the index block
so we could remove it from the htree index.  So it's all doable, if a
bit tricky in terms of the technical details; it's just that the
people who could do it have been busy enough with other things.

It's hasn't been considered high priority because most of the time
directories don't go from holding thousands of files down to a small
handful.  

                                               - Ted

引用自:https://unix.stackexchange.com/questions/495176