-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy path_clean-dsstore.sh
More file actions
executable file
Β·98 lines (86 loc) Β· 2.27 KB
/
_clean-dsstore.sh
File metadata and controls
executable file
Β·98 lines (86 loc) Β· 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# Recursively remove stray macOS .DS_Store files.
# Usage:
# ./scripts/_clean-dsstore.sh [TARGET_DIR] [--dry-run] [--silent]
#
# Options:
# TARGET_DIR Optional scan root. Defaults to the current directory.
# --dry-run Print matches without deleting them.
# --silent Suppress normal progress output. Errors still go to stderr.
#
# Notes:
# - Excludes selected macOS system folders from the search.
# - Quits and restarts Finder only if it was running before the cleanup.
# === Default settings ===
TARGET_DIR="."
DRY_RUN=false
SILENT=false
log_info() {
if [ "$SILENT" = false ]; then
printf '%s\n' "$1"
fi
}
log_error() {
printf '%s\n' "$1" >&2
}
# === Parse arguments ===
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--silent) SILENT=true ;;
--*)
log_error "Unknown option: $arg"
log_error "Usage: ./scripts/_clean-dsstore.sh [TARGET_DIR] [--dry-run] [--silent]"
exit 1
;;
*)
TARGET_DIR="$arg"
;;
esac
done
COUNT=0
OS_TYPE="$(uname)"
if [[ $OS_TYPE == "Darwin" ]]; then
# === Check if Finder is running ===
FINDER_WAS_RUNNING=false
if pgrep -xq "Finder"; then
FINDER_WAS_RUNNING=true
log_info "π Quitting Finder to prevent interference..."
osascript -e 'tell application "Finder" to quit'
fi
fi
if [ ! -d "$TARGET_DIR" ]; then
log_error "Target directory does not exist: $TARGET_DIR"
exit 1
fi
log_info "π Scanning for .DS_Store files..."
# === Main find/delete loop ===
while read -r file; do
[ -z "$file" ] && continue
log_info "ποΈ Found: $file"
if [ "$DRY_RUN" = false ]; then
if rm -f "$file"; then
((COUNT++))
log_info "βοΈ Deleted"
else
log_error "Failed to delete: $file"
fi
else
((COUNT++))
fi
done < <(find "$TARGET_DIR" \
-path "*/.Spotlight-V100" -prune -o \
-path "*/.DocumentRevisions-V100" -prune -o \
-path "*/.TemporaryItems" -prune -o \
-name '.DS_Store' -type f -print)
# === Summary ===
log_info "β
Done. Processed $COUNT file(s)."
if [[ $OS_TYPE == "Darwin" ]]; then
# === Restart Finder if it was previously running ===
if [ "$FINDER_WAS_RUNNING" = true ]; then
log_info "π Restarting Finder..."
open -a Finder
else
log_info "π Finder was not running before; not restarted."
fi
fi