Everyone uses built-in compression functionality of Mac OS X with a simple right click. This is useful when you need to compress certain files and send them over email or store them using time machine. However, there are times when you need some advanced options during compression. It’s mostly required when you need to take backup out of Time Machine or you want to transfer data to some other backup server. We are going to discuss one such compression command called “tar”. The command to create a tar / gzip file from the command line is exactly same as we use in Unix.
Open the Terminal window from /Applications/Utilities/Terminal and follow the instructions below –
How to Create a Tar GZip File from the Command Line
tar -cvzf compressedfile.tar.gz foldername
The command above looks quite easy, but the flags -cvzfhave all the secret inside. Let’s understand these one by one –
- -c means create an archive
- -v means verbose mode, if specified you will see the list of files running through your screen.
- -z means gzip compression, if specified it will filter your zipped file i.e. tar files via gzip compression.
- -f means filename
In the above syntax z is not really required, you can create an archive without having to specify gzip compression. But it’s very useful, as it’s responsible for actually compressing the size of the resultant file.These two commands can also be used separately.
How to unzip or untar tar.gz files
Now it’s time to see how can we unzip or extract theme compressed files. You can always use the unarchiver app in Mac, but let’s see how it can be done from command line.
tar -xvf compressedfile.tar
We can combine these two commands similar to what we do in Unix. And the above two commands will look like –
gunzip compressedfile.tar.gz | tar -xvf compressedfile.tar
Hope that helps!