Working with archives is something that I find myself doing when I am tweaking with things in my Linux environment or installing some tool that is not from a package manager. Every single time I do this, I find that I need to look up tutorials on how to work tar - I would have thought by now that I would be able to remember it off the top of my head, but no, I need to look up examples every single time.
With that in mind, here is my very quick, high level look at the tar command.
To create an archive (uncompressed), run the following command:
tar -cvf userhome.tar /home/user
This will create (c) a tar file (f) with the name 'userhome.tar' with the contents of the /home/user directory. This file will not be compressed. The output of the command is also verbose (v) and will show each and every file which is being added to the archive.
In order to create a compressed archive, we have a few options of the different switches which can be used:
--auto-compress- with this siwtch, the tar utlity will determine what compression method to use based on the file extension (.tar.gz or .tar.bz2 are a couple of exampls using gzip and bzip2, respectively)
--bzip2- pass through bzip2 compression
--ungzip- pass through gzip compression
There are other methods of compression available, but these are probably the most common that you will encounter.
Create archive examples:
tar -czvf userhome.tar.gz /home/user- creates a tar file compressed with gzip called userhome.tar.gz containing the contents of the /home/user directory
tar -cjvf documents.tar.bz2 ./Documents- creates a tar file compressed with bzip2 called documents.tar.bz2 containing the contents of the Documents directory.
tar -cavf config.tar.gz ./.config- this would use the gzip compression due the '-a' switch being provided which triggers tar to select the compression method based on the file extension which, in this case, is 'gz'.
To extract an archive we simply change out the create (-c) switch and replace it with the eXtract (-x) switch.
tar -xvf userhome.tar
This will extract (x) a tar file (f) with the name 'userhome.tar' to the current working direcotry. The output of the command is also verbose (v) and will show each and every file which is being added to the archive.
In order to extract a compressed archive, we can use the same compression switches as we did when creating an archive (-a, -j, -z).
Extract archive examples:
tar -xjvf documents.tar.bz2extracts documents.tar.bz2 to the current directory using bzip2
tar -xzvf userhome.tar.gz- extracts userhome.tar.gz to the current directory using gzip
tar -xavf congig.tar.gz- extracts config.tar.gz to the current directory using autodetect (in this case gzip to do the .gz file extension)
If desired, you can also extract to another directory by adding the '-C' switch, for example:
tar -xzvf userhome.tar.gz -C /home/user2 would extract to /home/user2 instead of the current working directory.
As with (to the best of my knownledge) every Linux command has something called "man pages". This is a document which you can use to read about all the different options that a given command supports along with its syntax. So if you want to read more about the tar command, run the command
man tar in the terminal or alternatively you can do a web search for "man tar" and you will likely find an HTML page showing the same information as you would find in the terminal.