Archiving and Compressing Files on Mac/Linux

May 10, 2022   • linux

Here’s how you can to compress and archive files on a Linux server or on your Mac.

gzip file compresses the file. To keep the original file, pass the -k flag.

gunzip uncompresses a .gz file.

However, gzip doesn’t create archives of files, i.e. it doesn’t pack multiple files and dirs into a single file. For that, use the tar command.

tar command creates an archive.

tar cfv archive.tar file1 file2

The c option requires creating the archive, the v option requests the verbose operation, and the f option takes an argument that sets the name of the archive to operate upon.

The following command instructs tar to store all files from the directory /etc into the archive file etc.tar, verbosely listing the files being archived:

tar cfv etc.tar /etc

To unpack a .tar file, use the x flag, which operates tar in extract mode.

tar xvf archive.tar

To unpack a compressed archive, first uncompress the file and then unpack.

gunzip file.tar.gz
tar xvf file.tar

Or, use the shortcut with the z option that does the same.

tar zxvf file.tar.gz

Hope this helps.