Category filter

Script to move files and folders on Mac

To move a file or folder to another location on a Mac, the user will first have to navigate to the location on the Finder application and use the context menu ‘Copy’ option or press command + X to cut the file. Then open the target location and use the context menu ‘Paste’ option or press command + V to paste the file/folder to that location. This method might prove tedious for the device administrator when managing multiple endpoints and impractical when moving multiple files or folders 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 remotely move files or folders on a Mac.

Scripting language – Bash

File extension – .sh

Disclaimer:


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

Move file/folder

The mv command is used to move files. The complete file name, along with the file extension, should be entered with the command. Some of the options supported with the command are –

-f : Force overwrite on the destination path.

-i : Prompt user to confirm file overwrite.

The mv command can also be used to modify the name of a file/folder by moving the file under a different filename to the same or a different folder.

For example,
mv ‘/Users/username/Documents/file.txt’ ‘/Users/username/Documents/renamedfile.txt’

For example,
mv ‘/Users/username/Documents/Folder Name’ ‘/Users/username/Downloads/Renamed Folder’

Notes:

  • While entering the path as ‘/path’ the script will navigate the path from the root folder. To enter a path from the current directly type in the path without ‘/’.
  • 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. So, the example above can also be deployed as

    mv /Users/username/Documents/Folder /Users/username/Downloads/Renamed\ Folder

  • 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