To make your cleanup script work without password prompts, you'll need to add a specific entry to your sudoers file. This will allow the script to execute sudo commands without requiring a password.
Here's what you need to add to your sudoers file:
username ALL=(ALL) NOPASSWD: /bin/rm, /usr/sbin/periodic
Replace username with your actual macOS username (the same one that appears in your path /Users/data/
- so likely data
).
To safely edit the sudoers file:
- Open Terminal
- Run sudo visudo (this opens the sudoers file in a safe editor)
- Navigate to the end of the file
- Press
i
to enter Insert mode, and Add the line above (with your username) - Save and exit (in vi/vim, press
ESC
, then type:wq
and press Enter) ⠀This configuration grants your user the ability to run the specific commands used in your script (rm and periodic) without a password prompt. It's more secure than giving blanket sudo access because it limits the commands that can be run without a password. If your script uses any other commands with sudo, you should add those to the NOPASSWD line as well.
Note: For cleanup to work, upgrade bash to ver. 5+ ... See: How To Upgrade your Bash Version on Mac OS?
Steps to Create the Finder Quick Action
- Open Automator (Cmd + Space, type Automator, hit Enter).
- Choose "Quick Action" as the document type.
- Set "Workflow receives current" to Files or Folders.
- Set "in" to Finder.
- Drag "Run Shell Script" from the actions panel into the workflow.
- Set:
- Shell: /bin/zsh
- Pass input: as arguments
- Replace default script with the
usb-cleanup-script.txt
- Save the workflow as "USB Cleanup & Eject".
- Go to System Settings → Privacy & Security → Full Disk Access, then add Automator and Finder. Navigate to and select both:
- /System/Library/CoreServices/Finder.app
- /Applications/Automator.app
- Try it: Right-click a USB drive → Quick Actions → USB Cleanup & Eject.
- Full Disk Access to bash. (Be careful!! First try giving FDA to Terminal, instead)
- To get location of bash and fswatch, in terminal do:
which bash
andwhich fswatch
.