Tar Command Tutorial With Examples
Linux tar is very popular tools used to compress and extract files and folders. Tar is used for a long time and its primary usage intention was using for tape archiving. Tar supports primarily tar format or extension which is used to create one file for all directories and files. But through the history tar have integrated compression protocol support like gz, bzip. Tar is generally used with these popular compression protocols to compress files and folder. Typically .tar, .tar.gz and .tar.bz extensions are used to name compressed files. In this tutorial we will look in details how to use tar command for different situations and operations.
Create Tar Archive
As we stated before tar archive will create a single file for all directories and files. This is very useful for compression, transfer, backup etc. Tar is generally used for transfer or backup operations too. In this example we will create our desktop directory as tar file.
$ tar -cvf tmux.tar tmux-2.2
Here we have created a tmux.tar which provides all files and folders in tmux-2.2
Verbosity or Debug
To get details of operations verbose option is should be specified. In previous example we are all ready using verbose option with -v
Create Tar Archive With Gzip Compression
Gzip is popular and good compression algorithm used for a lot of different purposes. For example browsers and web servers uses gzip compression algorithm too. Tar and gzip can be used together with just single command like below.
$ tar cvfz tmux.tar.gz tmux-2.2
Create Tar Archive With Bzip2 Compression
Bzip2 is alternative to the gzip compression algorithm. Bzip2 popular especially in Linux, Unix and Bsd platforms. It can be used the same as gzip.
$ tar cvfj tmux.tar.bz2 tmux-2.2
Decompress and Untar Tar Archive
Up to now we have compressed our files and directories. What about decompressing them? We will decompress all contents or the archives to the current working directory like below. Main parameter used to extract a tar archive is x
$ tar xvf tmux.tar
Decompress or Untar tar.gz Archive
Gzip compressed tar archives are similar while decompressing. We can add gzip parameter to the normal extract tar command like below.
$ tar xvfz tmux.tar.gz
Decompress or Untar tar.bz2 Archive
Same as gzip and just add j parameter to specify bzip extracting.
$ tar xvfj tmux.tar.bz2
List Content Of Tar Archive
A tar archive generally holds a lot of files and folders and listing files of a tar archive is easy as compressing files and directories. We will provide
-tvf option like below to list
tmux.tar file contents.
$ tar -tvf tmux.tar
List Content Of Tar.Gz Archive
tar.gz contents can be listed like below. It is the same as tar because. Tar detects the compression methods automatically. So we do not need to specify the compression algorithm specifically.
$ tar -tvf tmux.tar.gz
List Content Of Tar.Bz2 Archive
The same as tar and gz formats. We do not specify compression algorithm specifically. It is detected automatically.
$ tar -tvf tmux.tar.bz2
Extract Single File From Tar Archive
One of the most wanted way to extract file from archive is extract only single file. We will extract single file by given its name to the tar like below. We will use
-xvf option and provide the
tar file and the file we want to extract.
In this example we will extract
tmux-2.2/hooks.c file from tar file named
$ tar -xvf tmux.tar tmux-2.2/hooks.c
Extract Single File From Tar.Gz Archive
Extracting single file from a tar.gz archive is very similar to the previous example. We will add Gzip parameter which is
z . We will use
-zxvf option like below.
$ tar -zxvf tmux.tar.gz tmux-2.2/hooks.c
Extract Single File From Tar.Bz2 Archive
Extracting single file for Bzip2 archives is similar with tar and gzip. We will use
-jxvf option like below.
$ tar -jxvf tmux.tar.bz2 tmux-2.2/hooks.c
Extract Multiple File From Tar Archive
It can be also extracted multiple files from a tar archive. The files those will be extracted will be listed by splitting with spaces.
$ tar -xvf tmux.tar "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"
Extract Multiple File From Tar.Gz Archive
We can extract multiple files from a gzip archive like a tar archive. We will just put file names we want to extract at the end of the command.
In this example we will extract file
$ tar -zxvf tmux.tar.gz "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"
Extract Multiple File From Tar.Bz2 Archive
Multiple files of a bzip2 archive can be extracted similar to the tar and gzip. We will just provide bzip2 parameter with
$ tar -jxvf tmux.tar.bz2 "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"
Wildcards are used to specify multiple files with a similar name. We just set fixed part of names and then put wildcard for variable part like below. Wildcard is expressed with
$ tar -zxvf tmux.tar.gz --wildcards '*.txt'
Add Files and Directories To Tar Archive
After creating tar archive we may need to add extra files to the existing tar archive. For this operation we will use r for append or add operation.
$ tar -rvf tmux.tar test1/
In this example we have added directory named test1 . Adding file is the same.
Add Files and Directories To Tar.Gz Archive
Tar command currently do not have add capability for Gzip files.
Add Files and Directories To Tar.Bz2 Archive
Tar command currently do not have add capability for Bzip2 files.
Verify Tar Archive
Tar archives can be corrupted during transmission or there may be some media disk related issues. This situation can be checked by verifying tar archive files.
Get Size Of Tar Archive
The size of the content can be get with empirical ways like counting words. Below the result is in KB . The given size is the total size after uncompressing whole content
$ tar -czf - tmux.tar | wc -c
Get Size Of Tar.Gz Archive
Size of the tar.gz archive can be listed like below. But keep in mind that this is not exact size. It is an approximation .
$ tar -czf - tmux.tar.gz | wc -c
Get Size Of Tar.Bz2 Archive
Size of the tar.bz archive can be listed like below. But keep in mind that this is not exact size. It is an approximation .
$ tar -czf - tmux.tar.bz2 | wc -c