My mnemonic to remember tar commands on Linux

, updated (originally posted )

A webcomic depicting how hard it can be to use the tar command. First panel: "Rob! You use Unix! Come quick!" Second panel: "To disarm the bomb, simply enter a valid tar command on your first try. No Googling. You have ten seconds." In the final panel, after a hesitation, Rob says "I'm so sorry."

The tar command is famous for being hard to use. What flags do you use for creating a tarball? What about extracting one?

I remember two mnemonics when doing this: create a file and extract a file.

To create a tarball, run tar -caf (“Create A File”):

tar -caf my_archive.tar.gz file1.txt file2.png

To extract a tarball, run tar -xaf (“Extract A File”):

tar -xaf my_archive.tar.gz

That’s it!

This works on my Linux machines, but may not work on other operating systems like macOS. See below for more info.

What do the flags mean?

The -c and -x flags are for creation and extraction, respectively.

The -f flag is for the tarball file.

The -a flag is the most interesting. It’s short for --auto-compress. It uses the file’s extension to compress the tarball correctly. For example, tar -caf my_archive.tgz ... will create a tarball compressed with gzip, while tar -caf my_archive.tar.bz2 ... will create a tarball compressed with bzip2.

man tar has more information about these flags.

It’s a bit trickier on macOS

This post is about Linux. Unfortunately, it can be a little harder on macOS and any other system that uses BSD Tar.

As you might imagine, the -a/--auto-compress flag is unnecessary during extraction. There’s no need to “auto-compress” when you’re decompressing!

GNU Tar ignores the flag in this case; tar -xf and tar -xaf are equivalent. If you’re using GNU Tar—and if you’re using Linux, you likely are—you’re done!

BSD Tar, which is installed by default on macOS, doesn’t allow the -a flag during extraction. That means that tar -xaf will fail, and the mnemonic won’t work.

tar -xaf my_file.tar.gz
# tar: Option -a is not permitted in mode -x

To address this, you have a few options:

Again, none of this is relevant if you’re using GNU Tar, which is probably the case if you’re using Linux.

I don’t know what to do on other operating systems like Windows or FreeBSD. Feel free to reach out if you have a suggestion.

I hope the “create a file” and “extract a file” mnemonics are useful for you!

(The comic at the top of this post is from XKCD, which is licensed under the Creative Commons Attribution-NonCommercial 2.5 license.)