Category filter

Script to create files and folders for Mac

To create a folder on Mac, the user will have to navigate to the location on the Finder application and click on the context menu option to create a new folder on the location. The user may then rename the folder manually. To create a file, they may have to follow more sophisticated methods. This becomes a tedious process for the device administrator when managing multiple endpoints. This is also impractical when creating multiple files or folders at a single location or performing a batch operation. On the other hand, using a script to automate this process is easy, efficient and effective. This doc includes shell scripts that you can run using Hexnode’s Execute Custom Script action to create files or folders on a Mac remotely.

Scripting language – Bash

File extension – .sh

Disclaimer:


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

Create a file

The ‘touch’ command is used to create new files, empty files, update file access, and modify the date of a file.

Example – touch /Users/Username/Documents/TestDocument.txt

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.

Create a directory

The mkdir command, which stands for ‘make directory’ is used to create one or more new directories at once and set permissions for the files.

Example – mkdir /Users/Username/Documents/TestDocument

Notes:

  • Ensure that the path of the file/folder specified in the script is present on the device.
  • The commands used in this script are case sensitive.
  • 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