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:

  1. 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.
  2. Run rsync, starting with a dry run (-n) while you test.
  3. 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

  1. Create the pair with New Pair, using a folder on the drive.
  2. Open the pair’s settings and tick Run when a disk or share is connected.
  3. In Pairfold’s settings turn on Open Pairfold at login so it runs after a restart.
  4. 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.

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.