Onedrive path script/recreate user's Documents folder in finder sidebar


I did a test today with the below script in JSS. It creates the onedrive path structure we want for the current user. It worked, and surprisingly did not generate errors due to saved files that were open in the desktop and documents folder.

When I logged in as that user later, the Desktop link was automatically recreated in Finder sidebar for the user, but not the Documents link.

What can I add to this script at the end to force the Documents (and Desktop in case it was a fluke) links back to the user's Finder sidebar?

get current user

currentuser=$(who | grep console | awk '{print $1}')
echo $currentuser

Create onedrive structure

mkdir /Users/$currentuser/OneDrive - Alma College/
mkdir /Users/$currentuser/OneDrive - Alma College/Mac-Files/

Change ownership of that onedrive structure

chown -R $currentuser /Users/$currentuser/OneDrive - Alma College/

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

rm -r /Users/$currentuser/Documents/Microsoft User Data

Move the files

mv /Users/$currentuser/Desktop/ /Users/$currentuser/OneDrive - Alma College/Mac-Files/Desktop/
mv /Users/$currentuser/Documents/ /Users/$currentuser/OneDrive - Alma College/Mac-Files/Documents/

Delete finder prefs for user

rm /Users/$currentuser/Library/Preferences/
rm /Users/$currentuser/Library/Preferences/

Create symbolic links

ln -s /Users/$currentuser/OneDrive - Alma College/Mac-Files/Desktop/ /Users/$currentuser/Desktop

ln -s /Users/$currentuser/OneDrive - Alma College/Mac-Files/Documents /Users/$currentuser/Documents

Modify permissions on symbolic links themselves

chown -h $currentuser /Users/$currentuser/OneDrive - Alma College/Mac-Files/Desktop/ /Users/$currentuser/Desktop

chown -h $currentuser /Users/$currentuser/OneDrive - Alma College/Mac-Files/Documents /Users/$currentuser/Documents



For some reason when I copied and pasted to my post, it removed the pound signs for my comments in the script. Please ignore that.

Contributor III

@mccallister You need to use the code block tag of three consecutive backtick characters (```) before and after any script you want to display in a post, otherwise the display gets mangled.