macOS 清除 ._*、.DS_Store 隐藏垃圾文件

2021-05-11

众所周知,macOS 会自作聪明地生成一些隐藏的垃圾文件。

想出这个主意的人真是个鬼才,反正只要表面好看就行,全然不顾那些希望高度把控系统的强迫症患者,更别说泄露隐私的风险了。

垃圾文件主要是 ._*.DS_Store,对于前者,可以用 macOS 自带的 dot_clean 工具清除(看来苹果自己也知道这是垃圾文件啊),对于后者,只能用 find 命令查找删除了。

为此写了一个简单的脚本:

macos-clean-dumb-dotfiles/clean
#!/usr/bin/env sh

if [ "$#" -eq 0 ]; then
  echo "usage: clean path ..." >&2
  exit 2
fi

for path in "[email protected]"; do
  /usr/bin/find "$path" -name .DS_Store -type f -delete &
  pids="$pids $!"
  /usr/sbin/dot_clean "$path" &
  pids="$pids $!"
done

sc=0
for p in $pids; do
  wait "$p" || sc=$((sc + 1))
done
if [ $sc -ne 0 ]; then exit 1; fi

只需在 clean 后加一个或多个路径,即可递归地清除这些路径下的垃圾文件,例如 clean /Users /Volumes

要在 macOS 上设置定时任务,还得使用蛋疼的 launchd。假设脚本路径为 /Users/username/.local/bin/clean

每周日 20:30 清除整个系统:

macos-clean-dumb-dotfiles/clean-system.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>clean-system</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/username/.local/bin/clean</string>
		<string>/</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Weekday</key>
		<integer>0</integer>
		<key>Hour</key>
		<integer>20</integer>
		<key>Minute</key>
		<integer>30</integer>
	</dict>
</dict>
</plist>

除周日外每天 20:30 清除用户目录和挂载目录:

macos-clean-dumb-dotfiles/clean-users.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>clean-users</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/username/.local/bin/clean</string>
		<string>/Users</string>
		<string>/Volumes</string>
	</array>
	<key>StartCalendarInterval</key>
	<array>
		<dict>
			<key>Weekday</key>
			<integer>1</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>2</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>3</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>4</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>5</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Weekday</key>
			<integer>6</integer>
			<key>Hour</key>
			<integer>20</integer>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
	</array>
</dict>
</plist>

把以上 plist 文件放在 /Library/LaunchDaemons 下,然后:

sudo launchctl load /Library/LaunchDaemons/clean-system.plist
sudo launchctl load /Library/LaunchDaemons/clean-users.plist
macOSShell
知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

Y 站、K 站下载器 moebooru-crawler

KMP 算法中的 next 数组