in

How to compress files and folders in terminal

Since macOS is based on Unix there are a number of ways to compress files and folders within the filing system using Unix based application code, below are a few options using the Terminal or command line interface (cli). The default command line application interface in macOS is the Terminal and is stored in /Applications/Utilities.

File and folder compression saves on file size and ensures the contents are captured and delivered or stored as one monolithic file. A compressed file which contains files and folders is generally referred to as an archive. Here are some built-in compression applications you can use including zip, tar, gz, bz2, gz and dmg.

ZIP – Cross Platform

First up is ZIP one of the most commonly used compression techniques used across all platforms

To compress

zip -r archive_name.zip folder_to_compress

To extract

unzip archive_name.zip

If you want to make a zip without those invisible Mac resource files such as “_MACOSX” or “._Filename” and .ds store files, use the “-X” option in the command so:

zip -r -X archive_name.zip folder_to_compress

TAR.GZ – Cross Platform

Second up is TAR, an old favorite on Unix/Linux – you add the GZ for the compression – compresses tighter than zip

To compress

tar -zcvf archive_name.tar.gz folder_to_compress

To extract

tar -zxvf archive_name.tar.gz

TAR.BZ2 – Cross Platform

A variation on TAR GZ but with better compression than both tar.gz and zip.

To compress

tar -jcvf archive_name.tar.bz2 folder_to_compress

To extract

tar -jxvf archive_name.tar.bz2

GZ

Without the tar

To extract

gunzip archivename.gz

DMG – macOS Only

This one is macOSnative only – for a GUI interface use /Applications/Utilities/Disk Utility – for command line use:

To create

hdiutil create -format UDZO -srcfolder folder_to_compress archive_name.dmg

To mount

hdiutil attach archive_name.dmg

To view

ls -lah /Volumes/archive_name/

To Eject

hdiutil eject /Volumes/archive_name/

You can also use a number of different formats for creating a .dmg

  • UDZO – Compressed image (default)
  • UDRO – Read-only image
  • UDBZ – Better compressed image
  • UDRW – Read/Write image
  • UDTO – DVD disk image

That’s the low down, the more common compression packages available will typically be covered in one of the above.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

Force SSL “HTTPS” in Codeigniter

Why Having a Short or Branded URL is Important