How to use Tar Command in Linux with examples
The tar command stands for tape achieve, which is the most commonly used tape drive backup command used by the Linux/Unix system. It allows for you to quickly access a collection of files and placed them into a highly compressed archive file commonly called tarball, or tar, gzip, and bzip in Linux. The algorithm used for compressing of .tar.gz and .tar.bz2 are gzip or bzip algorithms respectively.
1) Extract a tar.gz archive
The following command shell helps to extract tar files out a tar.gz archive.
# tar -xvzf bigfile.tar.gz
x – Extract files
v – Verbose, print the file names as they are extracted one by one
z – The file is a “gzipped” file
f – Use the following tar archive for the operation
2) Extract files to a specific directory or path
We can extract the files into a specified directory by using the parameter “-C”.
# tar -xvzf bigfile.tar.gz -C /folder/subfolder/
The tar command doesn’t create any files or folders.
3) Extract a single file
To extract a single file out of an archive just add the file name after the command like this
# tar -xz -f Music.tar.gz “./new/one.mp3”
To extract more than one file out of an archive
#tar -xv -f Music.tar.gz “./new/two.mp3” “./new/three.mp3”
4) Extract multiple files using wildcards
To extract a group of files from archive
# tar -xv -f Music.tar.gz –wildcards “*.mp3”
5) List and search contents of the tar archive
To list out the tar archive without extracting, we can use ‘-t’
# tar -tz -f abc.tar.gz
For better viewing we can use less command or grep the pipe output for searching a file.
# tar -tvz -f Music.tar.gz | grep two.mp3
The verbose option “v” will provide more details about each file.
For tar.bz2/bzip files we should use “j” verbose option.
6) Create a tar/tar.gz archive
Using tar command, we can create tar archive using a directory, by adding all files in an archive or an entire directory.
# tar -cvf Videos.tar ./Latest/
The above command does not provide compression, but allows you to merge multiple files into one.
For the compression we could use the “z” or “j” option for gzip or bzip respectively.
# tar -cvzf Videos.tar ./Latest/
The extension names does not have much more importance. What really matter is “.tar.gz” which is commonly used with gzip compression and .”tar.bz2″ and “.tbz”.They are commonly used extensions for bzip compressed files.
7) Permission before adding files
The option “w” allows tar asking for permission for archiving each file. The files which are replied by are only archived and with no reply will be considered as “No”.
# tar -czw -f hugefie.tar.gz ./Videos/*
The result of the above executed command is, which can be viewed by listing:
# tar -t -f hugefie.tar.gz
8) Add files to existing archives
By using the “r” option it adds files to existing archives, without the creation of a new one.
# tar -rv -f Book.tar pageone.txt
The main thing to be aware of is this option works only with normal archives, and not with the compressed archives.
9) Add files to compressed archives (tar.gz/tar.bz2)
It was mentioned earlier that it is not possible to add files to a compressed archive. However, it can be done by performing a simple step. Use the gunzip command to uncompress the archive, add the file to the archive and finally compress it.
# gunzip Institution.tar.gz
# tar -rf Institution.tar ./College/Engineering/top.ppt
# gzip Institution.tar
If you need any further assistance please reach our support department.