When transferring files and backing up data from one computer to another, the smaller the file and the fewest number of files speeds up the process enormously.
This article concentrates on compressing and uncompressing files and folders using the command line into, and from, different formats.
The three main ways of compressing files and folders are tar, tar.gz and tar.bz2.
Each has uses different compression methods and produce different sized results. The most common you will find are tar.gz and tar.bz2.
Although not often used on it's own, tar (which comes from the words 'tape archive' or 'tape archiving'), does not actually compress files or folders but rather puts them into one file.
This is very handy when transferring or backing-up a directory as it produces one easy to manipulate file rather than an unwieldy folder full of them.
To tar files is very simple:
tar cvf dest.tar file.txt file2.txt
As you can see, the destination file is named first and anything named after that is placed into the tar file.
Tarring folders is done in exactly the same way:
tar cvf dest.tar myfolder/
Naturally, once transferred you will want to untar the file or folder like this:
tar xvf dest.tar
You may also wonder what is in the tar file so a quick list of files would be helpful. No worries:
tar tvf dest.tar
You may have notice the pattern in the commands: compressing uses the 'c' option, extracting uses the 'x' option and listing uses the 't' option.
That's all well and good but what if the tar file is several MB or even GB in size?
That's where compressing the archive comes into play. Once created, the tar file can be compressed to a smaller size.
One compression method uses gzip. So to tar a folder and then compress it using gzip we would use:
tar cvfz dest.tar.gz myfolder/
Again, the destination file, ending in tar.gz, is named first and the files and folders to be compressed are then named.
Uncompressing a gzip file is just as simple:
tar xvfz dest.tar.gz
And to have a quick peek inside the file without uncompressing the whole thing:
tar tvzf dest.tar.gz
The third method again uses tar to place all the files and folders into one large file but uses the bzip2 compressions utility instead of gzip.
Although not an absolute, bzip2 will usually produce a smaller file than when using gzip.
I am sure you are getting the hang of it, but to compress a folder, we first tar it and then, using bzip2, compress it:
tar cvjf dest.tar.bz2 myfolder/
To extract it:
tar xvjf dest.tar.bz2
And to have a little look inside:
tar tvjf dest.tar.bz2
Extracting individual files
I've left this useful addition to the end as it is easily applied to all three methods shown above.
The situation is commonly encountered: you have a compressed backup, perhaps several MB or GB in size, but you only want one file from it.
No problem. Simply name the file you want to extract after the initial command like so:
tar xvjf dest.tar.bz2 textfile.txt
As you can see, it uses exactly the same method of extraction used before, but instead of uncompressing the whole lot, it just uncompresses the named file - in this example I wanted textfile.txt to be uncompressed.
In all three methods (tar, tar.gz and tar.bz2), just name the specific file, or files, you want extracting after the standard uncompress command.
Some more advanced techniques are discussed in next article such as adding and deleting files from existing archives and excluding files when creating archives.