I'm using the following to count the number of local iCloud drives of all local users on a machine.
!/bin/bash
MobileDocsCount=ls -l /Users/*/Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g'
echo "<result>$MobileDocsCount</result>"
Thanks @macbofh
Looks good but for some reason it's not giving me any output on a Mac that definitely has drive setup.
Any ideas?


Two important characters got lost after the copy and paste of my script :)
#!/bin/bash
MobileDocsCount=`ls -l /Users/*/Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g'`
echo "<result>$MobileDocsCount</result>"

Ah Ha! Seems to have done the trick..
Thank you.
Your extension works perfectly in 10.12 and older. In 10.13 the Mobile Documents folder is standard and visible.
Here's an updated version:
#!/bin/bash
MobileDocsCount=$(ls -l "/Users/*/Library/Mobile Documents" | grep "com~apple~CloudDocs" | wc -l | sed 's/ //g')
echo "<result>${MobileDocsCount}</result>"
The suggestion iMathjis made not working for me on High Sierra. It is working if running from terminal, but I get 0 in the JAMF pro
Small edit to make this script work in Mojave (I haven't tested previous OS versions):
#!/bin/bash
MobileDocsCount=$(ls -l /Users/*/Library/Mobile Documents | grep "com~apple~CloudDocs" | wc -l | sed 's/ //g')
echo "<result>${MobileDocsCount}</result>"
I expanded this because if the Mobile Documents folder exists it doesn't mean iCloud Drive is active. If it is active and you disable it, it removes the folders from within the Mobile Documents folder, but leaves the folder. So you have to consider if the Mobile Documents folder exists, and if it does, does it contain any other folders:
!/bin/bash
MobileDocsCount=ls -l /Users//Library/"Mobile Documents"/ | wc -l | sed 's/ //g'
MobileDocs=ls -l /Users//Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g'
if [ $MobileDocs != 0 ]
then
if [ $MobileDocsCount != 0 ]
then
echo "<result>Enabled</result>"
else
echo "<result>Disabled</result>"
fi
else echo "<result>Disabled</result>"
fi
The following script is used in our environment but provides a unique output: "2". It is believed that 0 would mean that the folder and its contents were present, while 1 would mean that the folder and its contents were not present. However, I am getting an output "2". Can anyone provide some insight?
#!/bin/bash
MobileDocsCount=`ls -l /Users/*/Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g'`
echo "<result>$MobileDocsCount</result>"
The following script is used in our environment but provides a unique output: "2". It is believed that 0 would mean that the folder and its contents were present, while 1 would mean that the folder and its contents were not present. However, I am getting an output "2". Can anyone provide some insight?
#!/bin/bash
MobileDocsCount=`ls -l /Users/*/Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g'`
echo "<result>$MobileDocsCount</result>"
Hey @fernandez_payen,
If you are getting 2 as the output of that command, that means there is more than one folder in /Users/*/Library/ that contains the words "Mobile Documents".
This could mean that multiple users on the device have that folder present or there are multiple folders in one user's ~/Library/ that contains the words "Mobile Documents". eg. ~/Library/"Mobile Documents"/ and ~/Library/"New Mobile Documents"/.
The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input. This means it is not reporting if the command succeeded or failed but is actually returning a count of how many occurrences of "Mobile Documents" was found
I have been testing this on Monterey and this is the EA we are using. Note that we a looking at less than or equal to 1 for $MobileDocsCount. This is because on Monterey when you ls -l a folder it will return a single line stating total 0.
Additionally we are a 1:1 deployment so this may not work for a 1:many.
#!/bin/zsh
MobileDocsCount=$(ls -l /Users/*/Library/"Mobile Documents"/ | wc -l | sed 's/ //g')
MobileDocs=$(ls -l /Users/*/Library/ | grep "Mobile Documents" | wc -l | sed 's/ //g')
if [ $MobileDocs != 0 ]; then
if [ $MobileDocsCount -le 1 ]; then
echo "<result>Disabled</result>"
else
echo "<result>Enabled</result>"
fi
else
echo "<result>Disabled</result>"
fi