Tar stands for Tape archive.

Linux tar command most widely used for backup and archive data.

It is very important to archive multiple files and directory into a single file.

It is an essential Linux command for all Linux system administrators, here you will get most of the uses of tar command by using the tar examples, which are used by system administrators and engineers in every day.

Suggested Reading: Linux sed command Structure

Examples:

Use tar command to create ‘tar’ archive file

You can create a tar archive file by using the option “-cvf,” as shown below:
The options described as:

c – is used to create a new “.tar” archive file.
v – it is use to get the output of command verbosely
f – it applies to give the file name for the archive file.

Use tar command to create tar.gz archive file

You can create a gzip archive file with extension tar.gz using the “tar” command with option “z.”

For example, here we create a tar.gz archive file for Linuxconcept home directory, as shown below:

The files with extension “tar.gz” and “.tgz” are similar.

Use tar command to create tar.bz2 archive file

You can create a “tar.bz2” archive file, also using tar command. The bz2 file compression and decompression is taking more time as compared to gzip files.

To create a highly compressed file “tar.bz2” by using tar command, you should use “j” option in the command line, as shown below:

The above-created file “tar.bz2”, “tar.tbz” and “tar.tb2” are similar.

Extract tar archive file

To extract “tar” file need to use option “x” with tar command, as shown below:
If you want to extract files in any specific directory, you can use the option “-C” to specify the directory when a file should extract, as shown below:

Extract tar.gz archive file

You can extract the “tar.gz” file with tar command just like tar file, and you can use the “-C” option if you want to extract in a specific directory, as shown below:

Extract tar.bz2 archive file

You can extract “tar.bz2” file by using the tar command, as shown below:

List out the content of tar archive file

You can get a list of the content of tar archive file, to get a list of content just run the tar command with the “t” option as shown below:

List out the content of the tar.gz archive file

You can list out the content of the “tar.gz” archive file by using the following command:

List out the content of the tar.bz2 archive file

You can list out the content of “tar.bz2” highly archived file by using the following command:

Extract Single file from tar.gz file

You can extract a single file from the tar archive file by using the following command:
We are extracting a single file “satish.sh” from linuxconcept.tar archive file in above example.

Extract single file from tar.bz2 file

You can extract a single file from “tar.bz2” archive file by using the following command:

Extract multiple files from tar, tar.gz and tar.bz2 file

To extract multiple files from tar archive files, you can use the following commands:
You should replace “file 1” and “file 2” with your actual file name.

Adding files or directories to tar archive file

You can also add a file or directory in prebuild tar archive file using option “r,” as shown below:
In the above example, we added the “xyz.txt” file and “php” directory into “linuxconcept-14-09-10.tar” archive file.

Adding files or directories to tar.gz and tar.bz2 files

You can also add file or directory in prebuild tar.gz and tar.bz2 archive files by using the following commands:

Check the size of the tar, tar.gz and tar.bz2 archive file

You can use below example command to check the size of any archive files:

If You Like What We Do Here On LinuxConcept, You Should Consider:

Stay Connected to: Twitter | Facebook

Subscribe to our email updates: Sign Up Now

 

We are thankful for your support.

Follow me

SATISH KUMAR

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
Follow me