feat(osx): add `freespace` command to clean purgeable disk space (#8762)
Co-authored-by: Marc Cornellà <hello@mcornella.com>
Diff
1diff --git a/plugins/osx/README.md b/plugins/osx/README.md
2index ecc9327d0abf33038929fcd423ad28a4a4108dc2..50458f60e92f465ff11f9fc3dfcdeb2b934369ff 100644
3--- a/plugins/osx/README.md
4+++ b/plugins/osx/README.md
5@@ -12,27 +12,28 @@ Original author: [Sorin Ionescu](https://github.com/sorin-ionescu)
6 7 ## Commands
8 9-| Command | Description |
10-| :-------------- | :---------------------------------------------------- |
11-| `tab` | Open the current directory in a new tab |
12-| `split_tab` | Split the current terminal tab horizontally |
13-| `vsplit_tab` | Split the current terminal tab vertically |
14-| `ofd` | Open the current directory in a Finder window |
15-| `pfd` | Return the path of the frontmost Finder window |
16-| `pfs` | Return the current Finder selection |
17-| `cdf` | `cd` to the current Finder directory |
18-| `pushdf` | `pushd` to the current Finder directory |
19-| `pxd` | Return the current Xcode project directory |
20-| `cdx` | `cd` to the current Xcode project directory |
21-| `quick-look` | Quick-Look a specified file |
22-| `man-preview` | Open a specified man page in Preview app |
23-| `showfiles` | Show hidden files in Finder |
24-| `hidefiles` | Hide the hidden files in Finder |
25-| `itunes` | _DEPRECATED_. Use `music` from macOS Catalina on |
26-| `music` | Control Apple Music. Use `music -h` for usage details |
27-| `spotify` | Control Spotify and search by artist, album, track… |
28-| `rmdsstore` | Remove .DS\_Store files recursively in a directory |
29-| `btrestart` | Restart the Bluetooth daemon |
30+| Command | Description |
31+| :------------ | :------------------------------------------------------- |
32+| `tab` | Open the current directory in a new tab |
33+| `split_tab` | Split the current terminal tab horizontally |
34+| `vsplit_tab` | Split the current terminal tab vertically |
35+| `ofd` | Open the current directory in a Finder window |
36+| `pfd` | Return the path of the frontmost Finder window |
37+| `pfs` | Return the current Finder selection |
38+| `cdf` | `cd` to the current Finder directory |
39+| `pushdf` | `pushd` to the current Finder directory |
40+| `pxd` | Return the current Xcode project directory |
41+| `cdx` | `cd` to the current Xcode project directory |
42+| `quick-look` | Quick-Look a specified file |
43+| `man-preview` | Open a specified man page in Preview app |
44+| `showfiles` | Show hidden files in Finder |
45+| `hidefiles` | Hide the hidden files in Finder |
46+| `itunes` | _DEPRECATED_. Use `music` from macOS Catalina on |
47+| `music` | Control Apple Music. Use `music -h` for usage details |
48+| `spotify` | Control Spotify and search by artist, album, track… |
49+| `rmdsstore` | Remove .DS\_Store files recursively in a directory |
50+| `btrestart` | Restart the Bluetooth daemon |
51+| `freespace` | Erases purgeable disk space with 0s on the selected disk |
5253 ## Acknowledgements
5455diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh
56index 5455d7c5f78bf10c54a0872e2b40d8c2016fe902..16f16ddb699c4d93878bc9832364ca3737fbe21b 100644
57--- a/plugins/osx/osx.plugin.zsh
58+++ b/plugins/osx/osx.plugin.zsh
59@@ -233,6 +233,29 @@ function rmdsstore() {
60 find "${@:-.}" -type f -name .DS_Store -delete
61 }
6263+# Erases purgeable disk space with 0s on the selected disk
64+function freespace(){
65+ if [[ -z "$1" ]]; then
66+ echo "Usage: $0 <disk>"
67+ echo "Example: $0 /dev/disk1s1"
68+ echo
69+ echo "Possible disks:"
70+ df -h | awk 'NR == 1 || /^\/dev\/disk/'
71+ return 1
72+ fi
73+
74+ echo "Cleaning purgeable files from disk: $1 ...."
75+ diskutil secureErase freespace 0 $1
76+}
77+
78+_freespace() {
79+ local -a disks
80+ disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
81+ _describe disks disks
82+}
83+
84+compdef _freespace freespace
85+
86 # Music / iTunes control function
87 source "${0:h:A}/music"
88