Skip to main content

I have onedrive implemented for users that received new notebooks (manually pushing the app and our preference files), and now I need to push it out with JAMF to all the desktop machines. Below are the unix commands I have been manually entering in Terminal to create the onedrive path structure, move desktop and documents to there, and correct permission.



Question 1. What do I need to enter at the beginning of this script to read the current logged in user and substitute their username for the username of "mactest" that I have in the script?
Question 2. With the user logged in, the desktop and document folders (and files under them) may be in use. Will the script fail in that scenario?



Create onedrive structure



mkdir /Users/mactest/OneDrive - Alma College/
mkdir /Users/mactest/OneDrive - Alma College/Mac-Files/



Change ownership of that onedrive structure



chown -R mactest:"ALMANETDomain Users" /Users/mactest/OneDrive - Alma College/



Delete Microsoft User Data from Office 2011 prior to moving due to illegal characters in filenames



rm -r /Users/mactest/Documents/Microsoft User Data



Move the files



mv /Users/mactest/Desktop/ /Users/mactest/OneDrive - Alma College/Mac-Files/Desktop/
mv /Users/mactest/Documents/ /Users/mactest/OneDrive - Alma College/Mac-Files/Documents/



Delete finder prefs for user



rm /Users/mactest/Library/Preferences/com.apple.sidebarlists.plist
rm /Users/mactest/Library/Preferences/com.apple.finder.plist



Create symbolic links



ln -s /Users/mactest/OneDrive - Alma College/Mac-Files/Desktop/ /Users/mactest/Desktop



ln -s /Users/mactest/OneDrive - Alma College/Mac-Files/Documents /Users/mactest/Documents



Modify permissions on symbolic links themselves



chown -h mactest:"ALMANETDomain Users" /Users/mactest/OneDrive - Alma College/Mac-Files/Desktop/ /Users/mactest/Desktop



chown -h mactest:"ALMANETDomain Users" /Users/mactest/OneDrive - Alma College/Mac-Files/Documents /Users/mactest/Documents

Wow, I literally just posted this in another thread, but it's hopefully applicable here too:



I combined a few methods to best determine the currently logged in user, and if not available, the most likely "primary" user into this script:



#!/bin/bash

# Below is Apple's recommended way, which will supposedly only return the active user, even if multiple users are logged in. (Blank if no user is logged in.)

userMethod1="$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')"

# Below is a more common way of getting current user. May return multiple names if FUS is enabled and more than one user is logged in. ("root" if no user is logged in.)

userMethod2="$(stat -f%Su /dev/console)"

# Below is a method to determine most likely primary user based on number of logins to the console.

userMethod3=$( last -t console | awk '{print $1}' | sort | uniq -c | sort -n | awk 'END{print $NF}' )

if [ "$userMethod1" == "" ] || [ "$userMethod2" == "root" ]; then
loggedInUser="$userMethod3"
else
loggedInUser="$userMethod1"
fi


Note that Method 2 is actually extraneous if you use Method 1, but I just kept it in for reference.



You could then use $loggedInUser in place of the username in paths, like /Users/$loggedInUser/Desktop/, or you could run commands as the logged in user like sudo -u "$loggedInUser" command.


Question: Could you leave the Desktop and Documents folder where they are and just symlink (or hard link) them to the OneDrive folder structure you created? Not sure it makes any practical difference since mv within the same volume just changes the filesystem pointers and doesn't actually write new files. More curious if OneDrive would follow symlinks recursively or not.


@pcrandom OneDrive does not follow symbolic link. We were using hard link but macOS 10.13 High Sierra APFS breaks the hard link since its depreciated.



We can see the iCloud still works without symbolic link but not sure how apple implements it.



When we enable “Desktop and Documents Folders” in iCloud Drive, we can see kind of link getting created in
~/Library/Mobile Documents/com~apple~CloudDocs for Desktop and Documents.
Also the Desktop and Documents hides from Finder but Go to Folder for ~/Desktop takes to the user folder.



When we check the permissions for Desktop and Documents in ~/Library/Mobile Documents/com~apple~CloudDocs, it shows "lrwxr-xr-x@ 1
user staff 20 Oct 17 18:35 Documents -> /Users/user/Documents”. This looks like this folder is linked to /Users/user/Documents but not sure how its linked.