Category filter

Script to create password-protected ZIP file on macOS devices

Zip files offer a convenient way to transfer data from one system to another. You can upload large files to the server or send files over the internet efficiently by compressing the files down to a smaller size. Additionally, you can protect your .zip files with a password to control who can unpack the contents. This doc includes shell scripts that you can run using Hexnode’s Execute Custom Script action to create a password-protected ZIP file on macOS devices remotely.

Scripting Language – Bash

File extension – .sh

Disclaimer:


The Sample Scripts provided below are adapted from third-party Open-Source sites.

Create a password-protected ZIP file

The given script creates a file, adds content to the created file, and compresses it into a zip file with password protection.

For example, the following script creates a file called ‘filename.txt’ and adds the content ‘Hello world’ to the file. Then, the file is compressed to create a zip file ‘filename.zip’ with the password ‘1234’ in the path ‘/Users/Shared’.

#!/bin/sh

touch /Users/Shared/filename.txt

printf 'Hello world\n' > /Users/Shared/filename.txt

cd /Users/Shared

zip -r filename.zip filename.txt --password 1234

Convert a file to a password-protected ZIP file

To convert an existing file on macOS device to a ZIP file with password protection, run the following script:

Notes:

  • While using the zip command, ensure that the name of the zip file you specify and the name of the original file are the same.
  • For file names with spaces between words, either eliminate spaces beforehand, surround the file name with quotes or include a “/” after each word while retaining the space following the slash.
  • 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