Sub topic 13
Where iCloud Drive lives on disk and how to reach it from Terminal
This chapter assumes you can open Terminal, move around with cd and ls, and already have iCloud Drive turned on in System Settings. It covers where macOS keeps your iCloud Drive files and what you need to know before you touch them from the command line.
The folder behind the Finder sidebar
Finder shows iCloud Drive as a single sidebar item. On disk it is an ordinary folder inside your Library:
~/Library/Mobile Documents/com~apple~CloudDocsTwo details in that path cause most mistakes:
Mobile Documentscontains a space, so the path must be quoted in Terminal.com~apple~CloudDocsis the folder that holds what you see as "iCloud Drive". Its siblings inMobile Documentsare the private folders of individual apps, such ascom~apple~Pages.
If you turned on Desktop and Documents syncing, those two folders also live inside com~apple~CloudDocs. Your home-folder Desktop and Documents then point to them.
Listing and moving files
List the top level of iCloud Drive:
ls -la "$HOME/Library/Mobile Documents/com~apple~CloudDocs"Move a file into iCloud Drive. The sync service notices the new file, uploads it, and offers it on your other devices:
mv ~/Downloads/report.pdf "$HOME/Library/Mobile Documents/com~apple~CloudDocs/"The trailing slash tells mv that the destination is a folder. Without it, a typo in the name would rename the file instead of failing.
A shorter way to work is to store the path in a shell variable or alias. For example, add this line to ~/.zshrc:
alias icd='cd "$HOME/Library/Mobile Documents/com~apple~CloudDocs"'Placeholders and offline files
If Optimize Mac Storage is on, some files are only placeholders. In Terminal, a file that exists only in the cloud appears as a hidden stub named like .report.pdf.icloud. Use ls -a to see these stubs. A tool that reads the stub gets no document contents.
To fetch the real file before a script uses it, run brctl download on it:
brctl download "$HOME/Library/Mobile Documents/com~apple~CloudDocs/report.pdf"Opening the file in Finder, or choosing Download Now, has the same effect.
Keeping a folder out of sync
Some folders should not sync, such as node_modules or build output. iCloud Drive skips any file or folder whose name ends in .nosync. Rename the folder, then add a symbolic link under the original name so tools still find it:
mv node_modules node_modules.nosync
ln -s node_modules.nosync node_modulesA symbolic link is a small file that points to another path. The link itself is tiny and syncs, but the large folder it points to does not.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such file or directory` | The path was not quoted, so the space in `Mobile Documents` split it | Wrap the path in double quotes |
| File is a few bytes or named `.name.icloud` | The file is a cloud-only placeholder | Run `brctl download` or use Download Now in Finder |
| `ls` shows nothing in `Library` | You are in the wrong user account, or iCloud Drive is off | Check `echo $HOME` and System Settings |
| Terminal gets "Operation not permitted" | Terminal lacks access to the folder | Allow Terminal under Privacy & Security, Files and Folders or Full Disk Access |
Exercise
You run cd ~/Library/Mobile Documents/com~apple~CloudDocs and get an error. What is wrong, and what is the corrected command?
Show answer
The space in Mobile Documents splits the path into two arguments. Quote it: cd "$HOME/Library/Mobile Documents/com~apple~CloudDocs".
Exercise
You renamed build to build.nosync inside iCloud Drive, but your tools now say build is missing. What do you add?
Show answer
Create a symbolic link under the original name: ln -s build.nosync build.