Tar for Humans

tar is a command used to convert a file or a group of files into an archive. It can also do other stuff with those archives, like listing the files inside the archive, extracting files, etc.

To put it simply, at some point in your life as a linux/unix user/developer, you will need to use tar, either to compress some files and send them to someone or to extract the files someone sent to you.

My plan for this guide is not only to give you the commands you need to use, but to actually explain those commands, so you don’t have to reread this (or any other) guide again. With that in mind, we will be using GNU-style options, or long-form options. Why? Is it obvious what this option does?

All user commands are prepended with ‘$’ and all sudo commands are prepended with ‘#’. Commands that start with ‘$ #’ are still user commands, but they are comments, they are not to be pasted

$ tar -tavf example.tar.gz

No? That is why. :)

Using tar

For the purposes of this guide, I created a demo directory (in my $HOME) and copied the content of the go-build directory into it:

$ cd
$ mkdir demo
$ cp -r .cache/go-build/* demo/
$ cd demo/
$ ls -l

and this is the output:

$ ls -l
total 8
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 00
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 01
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 02
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 03
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 04
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 05
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 06
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 07
drwxr-xr-x 1 luka luka   0 Aug 23 09:39 08

Actual otput is bigger, I only copied the first 10 lines of it.

Now to create an archive containing all those files, we can run this:

$ tar --create --verbose --auto-compress --file go-build.tar.gz ./*

This will:

  1. create an archive (--create)
  2. print a list of files that are being archived (--verbose)
  3. determine the compression method (program) based on the archive name extension (--auto-compress), in this case, it would be gzip.
  4. name an archive go-build.tar.gz (--file <name>)
  5. put all the files from the current directory in the archive (./*)

Now that you have an archive, you can do anything with it, like - list the content of the archive:

$ tar --list --verbose --file go-build.tar.gz
$ # or
$ tar --list --file go-build.tar.gz

Using --verbose options will report file permissions and ownership alongside of file names. Now that you have verified (in a way, at least) that all your files are archived, you should probably generate a sum of that file, so you can later verify that it is indeed that file.

$ sha256sum go-build.tar.gz > go-build.tar.gz.sha256
$ # and later you can check the integrity like so:
$ sha256sum --check go-build.tar.gz.sha256
$ # it should print `go-build.tar.gz: OK`

And finally, when you decide to extract files from the archive, you can use this command:

$ tar --extract --verbose --auto-compress --file go-build.tar.gz
$ # this will extract the archive in the current working directory
$ # in the case you want to extract it in another directory, you can do it like so:
$ mkdir go-build && tar --extract --verbose --auto-compress --directory go-build/ --file go-build.tar.gz

And that’s about it. Now you know how to use tar to handle all (or at least 99%) of your day-to-day tasks. Of course, there are other options (many such options), some of them including (but not limited to):

You also have the option to: update files, diff the archive and the filesystem, concatenate tar files to an archive, exclude specific files by name or pattern (or even from .gitignore and other vcs tools), follow hard and soft links and archive the files they point/refer to, extract files to stdout, etc.

Closing notes

This is it for today. I hope you find this guide useful. As per usual, if you find any errors, send me a mail at luka [at] ljudi [dot] org. Cheers!

Previous I'm feeling lucky! Next