Category filter

Script to change ownership of a file or folder on Mac

The user who creates a file or folder automatically becomes the owner, and by default, no other user besides the owner can modify the file upon creation. In an organizational setting, IT administrators may find it necessary to transfer the ownership of a file or folder from one user to another, particularly when an employee departs from the organization or changes roles within it. Transferring ownership to another user ensures that other team members can take over incomplete projects, helping the team meet deadlines and finish any remaining work.

Using Hexnode’s Execute Custom Script action, IT admins can remotely change the ownership of the targeted file or folder on macOS devices by executing the below script.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Change ownership of a file or folder

For example:
chown Daniel /Users/Alex/Desktop/Dustin

The chown command is used to change the owner of a specific file/folder. Keep in mind that the username field represents the user account’s short name, which is typically the name of the home directory.

What happens at the device end?

Executing the script automatically changes the ownership of the specified file or folder to the configured user in the Sharing & Permissions setting within Get Info option on the macOS device.

No users other than the owner can modify a file/folder. The device asks for the admin credentials when other users attempt to modify the file/folder.

Notes:

  • In Bash, before inserting space while defining file or folder names, we use a backslash ‘\’ to separate the characters. This will prevent the shell interpreter from interpreting the space as a separator and assuming they were two different arguments. Hence, if the path is New Folder/myFile, we write it as New\ Folder/myFile or use single quotes as ‘New Folder/myfile’ instead.
  • The script executes only if the path for the file or folder is provided correctly. Otherwise, it throws an error upon execution.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository