Tar for humans
tar
is an archiving utility - a command used to convert a file or a group of files into an archive. Additionally, it can also do other stuff with
those archives, like listing the files inside the archive, comparing them, etc.
At some point in your life as a Linux/Unix user, you'll need to use
tar
, either to compress files and send them to someone or to extract the files someone has
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
tar
) guide again. With that in mind, we
will be using GNU-style options (or flags), or long-option style options. Why? Is it obvious what this option does?
$ tar -tavf example.tar.gz
No? That is why. :)
Using tar
For the purposes of this guide, I've created a simple
demo
directory (in my
$HOME
) and copied the content of the go-build directory into it:
Note: 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 used to explain the purpose of the command or to provide additional information.
$ 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 use this command:
$ tar --create --verbose --auto-compress --file go-build.tar.gz ./*
This will:
- create an archive (
--create
) - print a list of files that are being archived (
--verbose
) - determine the compression method (program) based on the archive name extension
(
--auto-compress
), in this case, it would begzip
. - name an archive
go-build.tar.gz
(--file <name>
) - 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 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 most of your day-to-day tasks. Of course, there are other options (many such options), some of
them including (but not limited to):
--append
: this adds a file to the existing archive (but the archive can not be compressed); e.g.tar --append --file archive.tar file1 file2
--delete
: this removes a file from the existing archive (again, the archive can not be compressed); e.g.tar --delete --file archive.tar 'file1' 'file2'
--wildcards
: this enables wildcard usage when extracting files from the archive; e.g.tar --extract --file archive.tar.gz '*.c'
--remove-files
: this removes the archived files; e.g.tar --create --verbose --auto-comress --remove-files --file home.tar.gz /home/user
--keep-old-files
: this tellstar
to treat situations where it tries to extract a file X into a directory where another file named X already exists as error--skip-old-files
: this tellstar
to treat situations where it tries to extract a file X into a directory where another file named X already exists as non-errors, but to skip them silently
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.
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!