Basic tar usage
Tar is widely used as a packaging tool in conjunction with gzip or bzip2 to create compressed archives, also called “compressed tarballs”. This is the first thing you have to get used to when switching from windows, and it is not much harder than extracting a zip file. There are many packaging tools like “xarchiver” that handle these. However, personally i think it is alot easier and faster to extract the package with the terminal utility. It is faster and pretty easy to get used to.
To extract a tarball, simply open up a terminal and navigate to the directory containing the archive.
Then simply use the following command:
tar -xvf archive.tar.gz
Tar will automaticlly decompress the file based on the extension of the file. You also may come across .tgz files, which is basicly the same, and tar will recognize it.Similarly, to create a tarball, issue the following command:
tar -czvf archive.tar.gz directory/
This tells tar to “-c”reate “-z”ipped “-v”erbose “-f”ile archive.tar.gz
One thing important to mention is the ability to create more instances of one file within an archive. Because tar actually stands for “tape archive”, so it stores files in that fashion. Files are appended to the end of the “tape”. So you can even make backups with snapshots with various “backup levels”. To get more information about those, it would be best to check out the “info” pages as the documentation is quite large and very clear. We will focus on a more simple method in backing up data and that by simply appending the same directories to the end of our archive.
Lets create a simple archive:
tar -cvf archive.tar dir/
Now we append the same directory again:
tar -rf archive.tar dir/
Check the content of the file with:
tar -tf archive.tar
You will notice that the whole structure is listed twice. To extract just once of them, we specify the “—occurrence=NUMBER”
tar -xvf archive.tar —occurence=1 dir/
You have to specify the directory you wish to extract, you may extract multiple directories as well with this method. It has only one disadvantage, you cannot compress the archive or else this method wont work. Thus the archive will just grow with duplicate entries, even of files that were not modified at all.
If you wish to just append the files that were modified since the last change, issue the following:
tar -ruf archive.tar dir/
Tar will look into the modified times and append the ones that changed. However, personally I prefer to make a complete backup because I mostly modify all files (during code creation, compilation, documentation). So this might be a nice way to make copies of your files instead of having a bunch of “file~”s.
So thats basiclly how to use tar, in the very simplistic way. You will most likely extract alot of those when downloading source-code or just simple software you wish to install. But most of the installing is done by package managers so manually install packages only when necessary or for the sheer fun of compiling from source.
