How do you run a folder sync when a drive is plugged in on a Mac?
By Kayabyte · Last updated 13 November 2026
Short answer: The free way is a launchd job with the StartOnMount key, which asks macOS to run your script whenever a file system is mounted. It runs for every drive you plug in, so the script has to check which one it is. Pairfold has a setting for this on each pair, Run when a disk or share is connected, and starts that pair’s run a few seconds after the disk appears. It works while Pairfold is open in the menu bar.
Method 1: launchd StartOnMount
A launchd agent is a small property list in ~/Library/LaunchAgents. Its ProgramArguments runs your script and StartOnMount set to true starts it when a volume mounts. Your script should:
- Check that the drive you mean is mounted, for example
[ -d /Volumes/Backup ]. Two drives can have the same name, and any drive triggers the job. - Run rsync, starting with a dry run (
-n) while you test. - Write to a log file so you can see that it ran.
Caveats: macOS may ask for permission for removable volumes the first time, a script without permission fails without a visible error, and a mount can fire before the drive is ready, so some people add a short wait. We have not tested every macOS version.
Method 2: Shortcuts or Automator
Folder Actions in Automator run when files are added to a folder, not when a drive is plugged in. Shortcuts has automations, and which triggers it offers depends on your macOS version, so check Shortcuts, Automation. We have not tested a drive trigger there.
With Pairfold
Pairfold watches for disks and shares that mount and matches them to your pairs through the folders you chose, so renaming a drive does not break it: in our test a renamed exFAT drive still triggered its pair and the pair’s path was updated. In the same test an ejected and re-attached drive started its run about five seconds after it appeared. Those tests ran in a macOS 26 virtual machine with a disk image attached as a virtual USB drive.
Before running, Pairfold checks that the folder is the right one. A different disk at the same place is not treated as the same disk.
Limits: Pairfold must be running. Turn on Open Pairfold at login in its settings to have it start after a restart; it is off by default and you can switch it off in System Settings, General, Login Items. It cannot run while the app is quit.
Steps with Pairfold
- Create the pair with New Pair, using a folder on the drive.
- Open the pair’s settings and tick Run when a disk or share is connected.
- In Pairfold’s settings turn on Open Pairfold at login so it runs after a restart.
- Unplug and plug in the drive. Open the pair’s History to see the run.
Get Pairfold: see the app page for where to get it
Questions
Can macOS run a script when a USB drive is connected? Yes, a launchd job with StartOnMount does, for any volume.
Does it work with a network share? Pairfold’s setting covers disks and shares. A share runs when it is mounted, for example by Finder.
Do I have to keep the window open? No. Pairfold can sit in the menu bar with the window closed.
Related guides
- How do you sync a folder to an external drive automatically?
- Two-way sync between a Mac and an external drive
- How do you back up a folder to a NAS on a Mac?
Apple, Mac, macOS, Finder, Time Machine and iCloud are trademarks of Apple Inc. Pairfold is made by Kayabyte and is not affiliated with or endorsed by Apple.
More guides
- What is the best folder sync app for Mac?
- Carbon Copy Cloner or SuperDuper alternative: do you need a clone or a folder sync?
- Why do files copy again to an exFAT or FAT32 drive on a Mac?
- How do you mirror a folder on a Mac without deleting files?
- Pairfold vs Sync Folders Pro, ChronoSync and FreeFileSync
- How do you use rsync on a Mac to back up a folder?
- How do you sync two folders on a Mac?
- Time Machine or folder sync: which one do you need?