Sub topic 14
Excluding folders with the .nosync suffix
This chapter assumes you can open Terminal, move around folders with cd, and know that iCloud Drive lives in a folder under your home directory (covered in the previous chapter). You should also know the difference between a file and a folder.
Why exclude a folder
iCloud Drive uploads everything inside it. Some folders are a poor fit for that:
- Dependency folders such as
node_modulescan hold tens of thousands of small files. Syncing them is slow and can stall other uploads. - Build output and caches change constantly and can be rebuilt at any time.
- Tools that write many temporary files can trigger conflicts between devices.
You do not need these folders on your other devices. You only need them to stay in place on this Mac, so the project keeps working.
How the .nosync suffix works
iCloud Drive skips any file or folder whose name ends in .nosync. The item stays on your disk, but it is not uploaded.
Renaming has a side effect. Many tools look for the original name, such as node_modules, and will not find node_modules.nosync. The usual fix is a symbolic link. A symbolic link (or symlink) is a small file that points to another path. A tool that opens the link is sent to the real folder.
The pattern has two steps:
- Rename the folder so it ends in
.nosync. - Create a symlink with the original name that points to the renamed folder.
The symlink itself still syncs, but it is tiny. Only the link is uploaded, not the folder contents.
Worked example
Suppose a web project is in iCloud Drive and contains a node_modules folder. Move into the project folder first, then run the commands:
cd "$HOME/Library/Mobile Documents/com~apple~CloudDocs/my-project"
mv node_modules node_modules.nosync
ln -s node_modules.nosync node_modules
ls -laThe ln -s command takes the target first, then the link name. In the ls -la output, the link appears as node_modules -> node_modules.nosync. Tools such as npm and your editor now use the folder as before.
To confirm the exclusion worked, look at the folder in Finder. The node_modules.nosync folder is shown with a grayed-out or dimmed appearance on some macOS versions, and it never gets a cloud or upload icon.
Common errors
- Wrong order in
ln -s. If you writeln -s node_modules node_modules.nosync, you create a link inside the existing folder, not the one you wanted. Check withls -la. - Renaming before the tool stops. If a dev server or watcher is running, quit it first. Otherwise it may recreate
node_modulesas a real folder next to the renamed one. - Expecting other devices to have the folder. Your iPhone, iPad, or another Mac gets the symlink, but the target does not exist there, so the link is broken. Run
npm installon each Mac that needs its own copy. - Putting the suffix on the link. The
.nosyncsuffix belongs on the real folder. The symlink keeps the original name.
When to move the project instead
Excluding one folder is a patch. If a project is full of generated files, or you work with Git, a simpler option is to keep the project outside iCloud Drive, for example in ~/Developer, and back it up with Git or Time Machine. Use .nosync when you want the source files to sync but not the rebuildable parts.
Exercise
A project in iCloud Drive has a cache folder you do not want to sync, and your build tool expects to find cache. Write the two commands you would run inside the project folder.
Show answer
mv cache cache.nosync
ln -s cache.nosync cache
The first command renames the real folder so iCloud Drive skips it. The second creates a symlink named cache that points to cache.nosync, so the build tool still finds it.
Exercise
You set up the symlink on your Mac. On your second Mac, the project opens but node_modules is a broken link. Why, and what do you do?
Show answer
Only the symlink synced. The node_modules.nosync folder it points to was never uploaded, so it does not exist on the second Mac. Run the install command (for example npm install) on that Mac to create the folder there.