macOS の伝統的悪しき慣習、アクセス先のあらゆるフォルダへの” .DS_Store “ファイル撒き散らす問題を、「作らない」・「 一括削除 」・「作らせない」という3つのアプローチで、 macOS 並びにファイル共有を提供するLinux 系サーバやNAS 、Windows Server各プラットフォームにおける対処法をまとめました。
“.DS_Store”とは
macOSのファイルマネージャであるFinderが、ローカル、リモート問わず、アクセスしたフォルダ全てに、表示設定などのメタ情報をその場に書き残すのが、
1 2 |
.DS_Store ._.DS_Store |
という小さなファイルです。隠し属性が付与されているので、Finderで隠しファイルを表示するよう設定しない限り、Finder上では目にすることは有りません。さらにmacOS 10.12 Sierra以降は隠しファイルを表示する設定にしても見えないようになりましたが、もちろんターミナルでは表示されます。
1 2 3 4 |
$ ls -la Pictures/ -rw-r--r--@ 1 user staff 8196 Dec 7 12:28 .DS_Store -rw-r--r-- 1 user staff 0 Sep 12 2019 .localized drwxr-xr-x 77 user staff 2464 Mar 1 17:00 iSight |
macOSのFinderが残すこの小さなファイルに対して嫌悪感を抱いてしまうのは、WindowsやLinuxなどmacOS以外のOSからそのフォルダを開いた時でしょう。アクセスしたネットワーク共有内の全てのフォルダに対して生成されるので、あまり気持ちの良いものではありません(下図は以前、OpenWRTルータGL-AR750Sに挿したSDカードをネットワーク共有フォルダとしたもの)
但し、Windowsの場合はエクスプローラのフォルダオプションで、隠しファイルを非表示にしていれば目にすることもありません。
“.DS_Store”を生成しないようにする
ローカルストレージ内はともかく、ネットワークストレージへのアクセス時に、macOSのFinderが .DS_Store を生成しないように設定することは可能です。ターミナルから、次のコマンドでネットワークリソースへの生成を抑制するオプションを有効にし、既存のFinderのプロセスを落としてリロードさせるか、システムへログインし直せば設定は反映されます。
1 2 3 4 5 6 |
$ defaults read com.apple.desktopservices DSDontWriteNetworkStores false $ defaults write com.apple.desktopservices DSDontWriteNetworkStores true $ killall Finder $ defaults read com.apple.desktopservices DSDontWriteNetworkStores true |
これで今後はネットワーク共有に対して生成されることは無くなりましたが、既に存在する .DS_Store は自分で削除しなくてはなりません。そこでネットワーク共有を提供する各プラットフォームに応じた、一括削除スクリプトを考察してみます。
次のような構造のテストフォルダを用意し、その中にある .DS_Store を再帰的に一括削除することが今回の命題です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ tree -a test/ test/ ├── .DS_Store ├── ._.DS_Store ├── subfolder1 │ ├── .DS_Store │ ├── ._.DS_Store │ ├── memo.txt │ └── subsubfolder │ ├── .DS_Store │ └── ._.DS_Store └── subfolder2 ├── .DS_Store └── ._.DS_Store |
macOS・Linux系OSでの一括削除
セキュリティ上の視点から、macOS上の .DS_Store を一括削除する方法とその定期的な実行について、Adobeから次のKBが公開されています。
macOSや一般的なLinuxでは、 find コマンドの -delete オプションを使えば次のように簡単に再帰的な削除が可能です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ find test/ -name *.DS_Store -print -delete test/._.DS_Store test/subfolder1/._.DS_Store test/subfolder1/.DS_Store test/subfolder1/subsubfolder/._.DS_Store test/subfolder1/subsubfolder/.DS_Store test/.DS_Store test/subfolder2/._.DS_Store test/subfolder2/.DS_Store $ tree -a test/ test/ ├── subfolder1 │ ├── memo.txt │ └── subsubfolder └── subfolder2 |
OpenWRTなどの組み込み系OSでは、BusyBoxに収められている find コマンドが古いことが多く、 -delete オプションをサポートしていない場合は、AdobeのKBにならい、 find から rm に渡して一括削除できます。
1 2 3 4 5 6 7 8 9 10 11 12 |
find: unrecognized: -delete BusyBox v1.28.3 () multi-call binary. # find test/ -name *.DS_Store -print -depth -exec rm {} \; test/subfolder1/subsubfolder/.DS_Store test/subfolder1/subsubfolder/._.DS_Store test/subfolder1/.DS_Store test/subfolder1/._.DS_Store test/subfolder2/.DS_Store test/subfolder2/._.DS_Store test/.DS_Store test/._.DS_Store |
これらのコマンドをcronへ登録して定期実行することで、 .DS_Store を自動削除することができます。
Windows上での一括削除:DOS
Windows Serverが提供するネットワーク共有フォルダ内に点在する .DS_Store を一括削除したい場合、MS-DOSでは DEL コマンドにより隠し属性の付いたファイルの再帰的な一括削除が可能です。
1 2 3 4 5 6 7 8 9 |
C:\> del /f /s /a:H "C:\test\*.DS_Store" Deleted file - C:\test\.DS_Store Deleted file - C:\test\._.DS_Store Deleted file - C:\test\subfolder1\.DS_Store Deleted file - C:\test\subfolder1\._.DS_Store Deleted file - C:\test\subfolder1\subsubfolder\.DS_Store Deleted file - C:\test\subfolder1\subsubfolder\._.DS_Store Deleted file - C:\test\subfolder2\.DS_Store Deleted file - C:\test\subfolder2\._.DS_Store |
Windows上での一括削除:PowerShell
PowerShellではいくつかの手法がありますが、例えばこのようにして指定フォルダ以下を一括削除することが可能です。
1 2 3 4 5 6 7 8 9 |
PS C:\> dir C:\test\* -include "*.DS_Store" -Recurse -Force | Remove-Item -Verbose -Force VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder1\subsubfolder\.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder1\subsubfolder\._.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder1\.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder1\._.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder2\.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\subfolder2\._.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\.DS_Store". VERBOSE: Performing the operation "Remove File" on target "C:\test\._.DS_Store". |
Windows Serverのタスクスケジューラにこれらのコマンドを登録して定期実行することで、Windows環境での自動削除が実現されます。
ここまで .DS-Store をmacOSが作らない、作った .DS_Store の一括削除について考察してきましたが、次のページではネットワークファイル共有側で .DS_Store の作成を拒否する方法を考えてみます。