萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> macOS 教學

Mac 刪除所有 .DS_Store 檔案:終端機 find 指令遞迴清除

.DS_Store 為 Finder 儲存資料夾檢視設定(圖示位置、背景、排序等)的隱藏檔,常汙染 Git 倉庫、壓縮檔、網路分享。全盤刪除指令:

刪除指令

sudo find / -name ".DS_Store" -depth -exec rm {} \;
  • sudo:需管理員權限存取系統資料夾。
  • find /:從根目錄遞迴搜尋(含外接磁碟)。
  • -name ".DS_Store":比對檔名。
  • -depth:先處理檔案再處理目錄,避免目錄被刪導致找不到檔案。
  • -exec rm {} \;:對每個結果執行刪除。

終端機 find 刪除 DS_Store

針對性清理(建議)

僅清理特定專案/資料夾,避免誤刪系統/其他使用者設定:

# 僅清理當前專案
find ~/Projects/MyProject -name ".DS_Store" -delete

# 清理家目錄(較安全)
find ~ -name ".DS_Store" -delete

防止產生

  • 網路磁碟:defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true。
  • USB/外接磁碟:defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true。
  • 重開機生效。Git 專案建議在 .gitignore 加入 .DS_Store。
copyright © 萬盛學電腦網 all rights reserved