Check if iCloud Drive is enabled

al_platt
Contributor II

I've been asked to report on users who have iCloud Drive enabled.

I found an older EA on the board but that doesn't seem to work on El Cap even if I amend the file path.

This is what i'm using.

!/bin/sh

if folder exists, then iCloud Drive is enabled

iCloudDriveFolder="~/Library/Mobile Documents/com~apple~CloudDocs"

if [ -e "${iCloudDriveFolder}" ]; then echo "<result>Enabled</result>"
else echo "<result>Disabled</result>"
fi

Seems to be that the ~/Library/Mobile Documents/com~apple~CloudDocs isn't recognised using the above. I can cd to the folder and terminal sees it as a directory.

Now am i missing something or is it something else?

Thanks

Al

1 ACCEPTED SOLUTION

macbofh
New Contributor III

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>"

72cc81fbdf5845a194ba14a94f7207e4

View solution in original post

10 REPLIES 10

macbofh
New Contributor III

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>"

al_platt
Contributor II

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?

60e7d343deb24dbbb170d56a8941e956
d49a5360a66f4bd08cf86182ef76b3a3

macbofh
New Contributor III

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>"

72cc81fbdf5845a194ba14a94f7207e4

al_platt
Contributor II

Ah Ha! Seems to have done the trick..

Thank you.

iMathijs
New Contributor II

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>"

ztslil709336
New Contributor

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

nate_tallman
New Contributor

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>"

cnorrisAdmin
New Contributor III

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

fernandez_payen
New Contributor III

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