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, they have to 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 is tedious for the device administrator when managing multiple endpoints and impractical when moving multiple files or folders or doing a batch operation. On the other hand, using a script to automate this process is easy, efficient, and effective. This doc includes a collection of shell scripts that you can run from the Terminal app to move files or folders on a Mac.
Device admins can remotely run scripts on Macs managed with Hexnode using the Execute Custom Script action.
Scripting Language – Bash
File extension – .sh
Move file/folder
1 2 |
#!/bin/bash mv ‘current path/FileName’ ‘path to target folder/FileName’ |
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.
Rename file/folder
You may also use the mv
command to rename a file/folder by moving the file under a different filename to the same folder.
Example –
mv ‘path to file/FileName’ ‘path to file/NewFileName’
mv ‘path to folder/FolderName’ ‘path to folder/NewFolderName