Locking the symlink as root
, as others have mentioned, is the answer. This can all be done from a script so you don't have to enable and log into the root
account.
Here is a simple script I wrote that needs to be run as root
(with sudo
) that takes two arguments:
- Short username
- Name of your OneDrive folder (Just
OneDrive
for personal OneDrive accounts)
This script will:
- Create a
Desktop
and Documents
folder in the specified OneDrive
folder
- Copy all items from your existing
Desktop
and Documents
folders into OneDrive using rsync
- Delete your
Desktop
and Documents
folders
- Symlink
Desktop
and Documents
to OneDrive
- Lock the symlinks themselves so they aren't deleted on login/reboot
Warning:
This script doesn't do any error checking or folder existence verification. It might delete all of your files. Make sure you have a backup.
#!/bin/bash
if [[ $# -lt 2 ]] ; then
echo "Scipt requires two arguments:"
echo "Argument 1. Short username"
echo "Argument 2. Name of OneDrive folder"
exit 1
fi
shortname=$1
onedrive=$2
#########
DESKTOP
#########
Make Desktop folder in OneDrive
mkdir -p "/Users/$shortname/$onedrive/Desktop"
Copy all files from existing Desktop folder to OneDrive Desktop folder
rsync -havux --progress --stats "/Users/$shortname/Desktop" "/Users/$shortname/$onedrive/Desktop"
Remove existing Desktop folder
rm -rf "/Users/$shortname/Desktop"
Create symlink to Desktop folder in OneDrive
ln -s "/Users/$shortname/$onedrive/Desktop" "/Users/$shortname/Desktop"
Set the symlink itself (-h flag) to system locked. (User locked would be uchg)
chflags -h schg "/Users/$shortname/Desktop"
###########
DOCUMENTS
###########
Make Documents folder in OneDrive
mkdir -p "/Users/$shortname/$onedrive/Documents"
Copy all files from existing Documents folder to OneDrive Documents folder
rsync -havux --progress --stats "/Users/$shortname/Documents" "/Users/$shortname/$onedrive/Documents"
Remove existing Documents folder
rm -rf "/Users/$shortname/Documents"
Create symlink to Documents folder in OneDrive
ln -s "/Users/$shortname/$onedrive/Documents" "/Users/$shortname/Documents"
Set the symlink itself (-h flag) to system locked. (User locked would be uchg)
chflags -h schg "/Users/$shortname/Documents"