How to use the tar command

I've been doing a lot in the command line lately, so I've gotten to use the tar command quite a bit. Here are a few useful tips that helped me get started.

I’ve been doing a lot in the command line lately, so I’ve gotten to use the tar command quite a bit.

Here are a few useful tips that helped me get started:

Basic Usage

The tar command is a compressor/decompressor which generally works out of the box on Mac and Linux machines.

The basic usage is as follows:


$ tar -options file1 file2

Compression

To compress a file, here’s my go-to usage:


$ tar -czvf files.tar.gz ./files/

Let’s look at each of these parts:

  • Options: -czvf
    • c: This means we want to compress (vs x for extract).
    • z: This says we want to use the gz compression technique.
    • v: This means we want to get a verbose output of what is being compressed (great for ensuring everything gets in there like you want it).
    • f: This means we're compressing a folder and its contents into a file.
  • files.tar.gz: This is the destination file. Don't forget the .tar.gz extension!
  • ./files/: This is the source folder we want to compress.

It’s as simple as that!

Decompression

To decompress, just use the x option instead of the c option:


$ tar -xzvf files.tar.gz ./

That’s not so bad, is it?

Believe