macOS possesses a number of Terminal commands to enable compressing files, thereby freeing disk space, requiring less storage capacity, and decreasing email attachment size, among other benefits. Just open Terminal, found within macOS’ Utilities subdirectory within the Applications folder, to leverage two popular commands: bzip2 and zip.

For the purpose of the following examples, assume you wish to compress a 10.1MB file titled Product Shot.jpeg. Also assume the file is located within a Documents subdirectory titled Marketing. For testing purposes, I included 27.2MB of additional files within the same Marketing subdirectory to test compression using a larger variety of file formats, including documents, spreadsheets, photos, and PDFs.

Begin by opening Terminal. (Note: If you use Terminal frequently, or you want to encourage yourself to use it more often, place the Terminal icon in your Mac’s Dock.) With Terminal open, enter cd documents/marketing to navigate to the Documents directory’s Marketing subdirectory.

SEE: Research: Apple’s Growing Role in the Enterprise (Tech Pro Research)

How to use the bzip2 macOS file compression command

To compress a file using bzip2 (which dates to the mid 1990s and uses a compression algorithm named after inventors Michael Burrows and David Wheeler), open Terminal and enter the following command:

bzip2 "Product Shot.jpeg"

macOS will compress the file and create a new file. In this case, bzip2 creates a new file titled Product Shot.jpeg.bz2. Note that the bzip2 command subsequently deletes the original file.

Using the test photograph, bzip2 didn’t provide significant compression. The new resulting file was only slightly smaller at 9.8MB (vs. the original 10.1MB). However, the same command performed on an Apple Keynote presentation reduced the file size substantially from 27.8MB to just 9.1MB following compression.

To decompress bzip2 files using the macOS Terminal, use the bunzip2 command. For example, to decompress the test photo file, enter the following command:

bunzip2 "Product Shot.jpeg.bz2"

macOS processes the command and restores the uncompressed file. The bunzip2 command also deletes the compressed file (Product Shot.jpeg.bz2 in this case) in the process.

SEE: Apple’s first employee: The remarkable odyssey of Bill Fernandez (TechRepublic)

How to use the zip macOS file compression command

The zip format (which dates to the late 1980s and is now commonly associated with Windows) is also natively supported within macOS. The zip command is among the most frequently used for compressing files. The command also supports compressing folders containing multiple files.

The following command compresses the entire test Marketing directory described earlier:

zip Marketing.zip Marketing

The zip Marketing.zip portion of the command specifies the name for the resulting archive file. The remaining Marketing statement specifies the directory to compress.

To zip a single file within a new compressed archive titled Photo.zip, such as the Product Shot.jpeg within the Marketing directory, enter the following command:

zip Photo.zip "Product Shot.jpeg"

Unlike many compression commands, including bzip2, the zip command does not delete the original file (or files) upon creating the new compressed archive. So, if you’re seeking to reduce disk space or eliminate redundancy, you’ll need to manually delete the original files if they are no longer required. Just be sure to double-check the archive to confirm the required files are present within the package.

Using zip, the 10.1MB photo was compressed only slightly, resulting in a 9.9MB archive. A second test, run on the directory containing 27.2MB of photos, spreadsheets, documents, and PDF files, resulted in much more significant compression and an archive just 16.1MB in size. Using the 27.8MB Apple Keynote presentation test file mentioned earlier, zip compressed the resulting archive file to 10.4MB.

To uncompress zip files using macOS’ Terminal, use the unzip command. Using the test photo from earlier, the corresponding command looks like this:

unzip Photo.zip

The zip command subsequently unpacks the archive. The archive’s individual file or files are placed within the directory in which the archive was present.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays