Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 統計目錄下子資料夾與檔案數量的 find、tree、du 指令比較

編輯:關於 Linux

需統計目錄下子資料夾與檔案總數時,find、tree、du 三種指令結果可能不同。本文實測比較並給出建議。

find 指令 (推薦)

# 所有子目錄數 (含父目錄)
find pma -type d | wc -l          # 125
find pma/ -type d | wc -l         # 125
# 僅統計子目錄 (不含父目錄)
find pma/* -type d | wc -l        # 124  ← 正確

# 所有檔案數
find pma -type f | wc -l          # 987
find pma/ -type f | wc -l         # 987
find pma/* -type f | wc -l        # 987

差異原因:pma/* 不包含父目錄本身,輸出從第一層子目錄開始。

tree 指令 (不推薦)

tree pma
# 124 directories, 984 files  ← 檔案數少 3 個

du 指令

du -ah pma/* | wc -l   # 1111
# 子目錄 124 + 檔案 987 = 1111,結果正確

結論

find 配合 wc -l 最準確;tree 可能漏計檔案;du 需扣除目錄數才能得出檔案數。建議統一使用 find。

Copyright © Windows教程網 All Rights Reserved