Category filter

Script to copy files and folders on macOS

To copy a file or folder to another location on a Mac, the user needs to navigate to the location of the file or folder on the Finder application and use the context menu option ‘Copy’ or use the shortcut command + C to copy the file. Then, they need to open the target location and use the context menu option ‘Paste’ or use the shortcut command + V to paste the file or folder. This method is tedious for the device administrator when managing multiple endpoints and impractical when handling multiple files or folders or performing a batch operation. On the other hand, administrators can optimize this process by executing the script given below to remotely copy and paste files and folders using Hexnode’s Execute Custom Script feature.

Scripting language – Bash

File extension – .sh

Disclaimer:


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

Copy file/folder

Example – cp –R Desktop/1 Desktop/2

  • The cp command copies the content of a source file to the target file.
  • The -R option allows you to copy a directory and all its subfolders.
  • If the path given ends in /, the contents in the directory are copied rather than the directory itself.
  • Replace with the path of the file/folder to be copied.
  • Replace with the path of the location where the file/folder should be pasted.
  • What happens at the device end?

    Once the script is executed, a copy of the file/folder in the specified source path is created in the specified destination path, including all subfolders and subfiles.

    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.

    • 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.
    • The commands used in the script are case sensitive.

  • Sample Script Repository