Sub topic 13
Where iCloud Drive lives on disk and how to reach it from Terminal
This chapter assumes you can already turn on iCloud Drive, see its files in Finder, and open Terminal to run basic commands such as ls, cd, and mv. Finder hides the folder structure behind a friendly "iCloud Drive" sidebar entry. Terminal shows the real path, and knowing that path lets you script, search, and troubleshoot.
The real location of iCloud Drive
Finder's "iCloud Drive" item is a view onto one folder inside your hidden Library folder:
~/Library/Mobile Documents/com~apple~CloudDocsThree details trip people up:
- The tildes inside the name are literal.
com~apple~CloudDocsis the folder name. Only a leading~on its own expands to your home directory. - The path contains a space. You must quote it or escape the space.
- Quotes stop
~from expanding. Inside double quotes, write$HOMEinstead of~.
"Mobile Documents" is a historical name from the early days of iCloud. It still holds the storage for everything that syncs through iCloud Drive.
Both tools work on the same local folder. When you change something there, the sync service uploads the change. When another device changes something, the service downloads it into the same folder.
Looking inside from Terminal
List the top level of your iCloud Drive:
ls -la "$HOME/Library/Mobile Documents/com~apple~CloudDocs"The first time a Terminal session touches this folder, macOS may ask whether Terminal can access files in iCloud Drive. This is a privacy permission. If you click "Don't Allow" or the prompt never appears, commands fail with Operation not permitted. You can change the setting in System Settings under Privacy & Security, in the Files and Folders section for Terminal. The same rule applies to other terminal apps, editors, and scripts launched from them.
Other containers beside CloudDocs
List the parent folder:
ls "$HOME/Library/Mobile Documents"You will see several sibling folders. Each one is a container that belongs to a single app.
| Folder name pattern | What it holds |
|---|---|
| `com~apple~CloudDocs` | The general iCloud Drive that you see in Finder |
| `com~apple~Pages`, `com~apple~Numbers`, `com~apple~Keynote` | Documents stored by Apple's own apps |
| `iCloud~com~example~appname` | Data from third-party apps that use iCloud |
In Finder, the app containers appear as app-named folders at the top level of iCloud Drive. On disk they are siblings of CloudDocs. A file you drop into the Pages container is in iCloud Drive, but it is not under com~apple~CloudDocs. If a find or backup script covers only CloudDocs, it will miss these files.
Making the path easier to use
Typing the full path is slow and error-prone. Create a symbolic link (a small file that points to another path) in your home folder:
ln -s "$HOME/Library/Mobile Documents/com~apple~CloudDocs" "$HOME/iCloud"
cd "$HOME/iCloud"After this, ~/iCloud works as a shortcut in any command. The link is a normal local file and does not sync. If you delete the link, the iCloud Drive folder itself is untouched. Before you delete anything through a link, check which path you are really working on.
To open the real folder in a Finder window from Terminal:
open "$HOME/Library/Mobile Documents/com~apple~CloudDocs"Moving files into iCloud Drive
Moving a file into the folder is enough to start syncing it:
mv ~/Downloads/report.pdf "$HOME/Library/Mobile Documents/com~apple~CloudDocs/"The trailing slash makes mv treat the destination as a folder. Without it, a typo in the destination could silently rename your file instead.
Both paths here are on your Mac's internal volume, so mv is a quick rename. The sync service then notices the new file and uploads it. Finder shows a progress icon while this happens. A move from an external drive is a copy followed by a delete, and it takes as long as the copy.
Use cp instead of mv when you want to keep the original. For folders, use cp -R, which recurses and keeps the directory structure.
Files that are only in the cloud
With Optimize Mac Storage on, macOS may remove the local contents of files you have not used for a while. The file stays in the folder as a placeholder, and the real data remains in iCloud. How a placeholder looks in Terminal depends on your macOS version:
- Older versions show a hidden stub named like
.report.pdf.icloudinstead ofreport.pdf. Plainlshides it.ls -ashows it. - Newer versions keep the real file name and mark the file as "dataless". Reading the file from a script or application makes macOS download the contents first.
To check, run ls -lhO in the folder. On macOS versions that use the dataless marker, the flags column shows dataless for files that are not stored locally.
Logical size versus disk use
A cloud-only file still reports its full size in ls, because that is the size of the content in iCloud. It uses almost no local disk space. To see real local usage, compare the two commands:
cd "$HOME/Library/Mobile Documents/com~apple~CloudDocs"
ls -lh
du -sh ./*ls -lh shows each file's logical size. du -sh shows the space actually used on your disk. A large gap between the two means many files are cloud-only.
Forcing a download or eviction
macOS includes a command called brctl that talks to the sync service. It has no manual page and Apple does not document it as a stable interface, so treat it as a convenience that may change between releases.
brctl download "$HOME/Library/Mobile Documents/com~apple~CloudDocs/report.pdf"
brctl evict "$HOME/Library/Mobile Documents/com~apple~CloudDocs/report.pdf"download asks the service to fetch the file now. evict removes the local copy and keeps the cloud copy. For everyday use, the Keep Downloaded and Remove Download commands in Finder do the same job and are the supported route. Use brctl mainly in scripts that must make sure files are present before a tool reads them.
Gotchas for scripts and tools
- Tools that scan a whole tree can trigger downloads. A backup program, a search indexer, or
grep -rover a folder with many cloud-only files may download all of them. Limit the search to folders you need. - Heavy write activity is slow to sync. Build output, caches, and version-control metadata create thousands of small changes. The sync service tries to upload each one, and you may see stalls. The next chapter covers keeping such folders out of sync with the
.nosyncsuffix. - Do not edit a file in place from two Macs at once. Terminal editors and scripts do not coordinate with the sync service. Conflicts are covered in the chapter on version history.
- Hidden files sync too. Files that start with a dot, such as
.DS_Store, are part of the folder like any other. - Do not edit the container's internal state. Leave any folder or file that you did not create yourself alone. Moving or deleting internal files can confuse the sync service.
Checking your understanding
Exercise
You run cd ~/Library/Mobile Documents/com~apple~CloudDocs and Terminal reports No such file or directory. The folder exists, and the path looks right. What is the most likely cause, and what is the fix?
Show answer
The space in "Mobile Documents" splits the path into two arguments. Quote the path or escape the space, for example cd "$HOME/Library/Mobile Documents/com~apple~CloudDocs".
Exercise
In a folder in iCloud Drive, ls -lh shows a 2 GB video, but du -sh on the same file shows almost nothing. What does this tell you, and how can you make the file fully available offline?
Show answer
The file is cloud-only. ls reports its logical size, while du reports local disk use, which is near zero. To keep a copy on this Mac, choose Download Now or Keep Downloaded in Finder, or run brctl download on the file.
Exercise
You want a script to back up everything stored in iCloud Drive, including documents created by Pages. Why is it not enough to copy com~apple~CloudDocs?
Show answer
Apple's apps and third-party apps keep their documents in sibling containers inside ~/Library/Mobile Documents, such as com~apple~Pages. These folders are not under com~apple~CloudDocs, so the script must also cover those containers.