Archiving files and folders on macOS … the harder but useful way
macOS offers a few alternatives to compressing and archiving files and folders apart from just right-clicking on them and hitting compress. While the easy way is good and viable it does not let choose where to compress to which, especially when trying to archive files to free up space, consumes even more space on an already full drive.
All of the methods below require you to use the terminal on your Mac. If you’re uncomfortable using the Terminal you should be careful with the commands below, not to zip your entire system at once.
Note: In the following code-snippets you will find the keywords myarchive.xyz as well as myfolder
of course you will have to replace these placeholders with your actual filenames and -paths.
Compress into .zip
zip, who doesn’t know it, and that makes it nice as it is a format supported on Mac, Windows and Linux.
So if you want to share files between operating systems this one might be for you
To compress your folder into a .zip
zip -r myarchive.zip myfolder
To open the .zip in the current location
Compress into .tar
tar is a bit more special as it is (by default) supported on Mac and Linux only.
The .tar.gz format is quite common and offers a better compression over .zip
.tar.bz2 offers an even higher compression than .tar.gz
To compress your folder into a .tar.gz
tar -zcvf myarchive.tar.gz myfolder
To open the .tar.gz in the current location
tar -zxvf myarchive.tar.gz
To compress your folder into a .tar.bz2
tar -jcvf myarchive.tar.bz2 myfolder
To open the .tar.bz2 in the current location
tar -jxvf myarchive.tar.bz2
Compress into .dmg
.dmg is the Apple Disk Image format and therefore should only be used if you stay on macOS.
.dmg is also used frequently when installing new applications and has the advantage of giving you a GUI through the Disk Utility App on your Mac
Images support different formats, allowing your, for example, to create read only images
UDZO : A simple compressed image (default for -format)
UDRO : A simple read-only image
UDBZ : An image with higher compression
UDRW : A simple Read-Write-Image
UDTO : A DVD-Disk-Image
To create an image
hdiutil create -format UDZO -srcfolder myfolder myarchive.dmg
To mount the image
hdiutil attach myarchive.dmg
To view the contents of the image after mounting
ls -lah /Volumes/myarchive/
To unmount the image
hdiutil eject /Volumes/myarchive/