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
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.
1 2 3 4 5 |
#!/bin/sh touch ‘path to file/filename with extension’ printf 'Required Content' > ‘path to file/filename with extension’ cd path to file zip –r ‘filename.zip’ ‘filename with extension’ --password ‘zip file password’ |
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:
1 2 3 |
#!/bin/sh cd path to file zip –r ‘filename.zip’ ‘filename with extension’ --password ‘zip file password’ |