Friday, October 30, 2020

15 Practical examples of tar command

Must read

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...
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.
- Advertisement -

Tar stands for Tape archive.

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

- Advertisement -

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:

# tar -cvf linuxconcept-14-09-19.tar /home/linuxconcept/
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

- Advertisement -

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:

# tar cvzf Linuxconcept-14-09-19.tar.gz /home/linuxconcept
OR
# tar cvzf Linuxconcept-14-09-19.tgz /home/linuxconcept

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.

- Advertisement -

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

# tar cvfj Linuxconcept-org.tar.bz2 /home/linuxconcept
OR
# tar cvfj Linuxconcept-org.tar.tbz /home/linuxconcept
OR 
# tar cvfj Linuxconcept-org.tar.tb2 /home/linuxconcept

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:

# tar -xvf linuxconcept-14-09-19.tar

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:

# tar -xvf linuxconcept-14-09-19.tar -C /home/linuxconcept/videos/

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:

# tar -xvf linuxconcept-14-09-19.tar.gz
# tar -xvf linuxconcept-14-09-19.tar.gz -C /home/linuxconcept/videos/

Extract tar.bz2 archive file

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

# tar -xvf linuxconcept-14-09-19.tar.bz2

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:

# tar -tvf linuxconcept.tar

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:

# tar -tvf linuxconcept.tar.gz

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:

# tar -tvf linuxconcept.tar.bz2

Extract Single file from tar.gz file

You can extract a single file from the tar archive file by using the following command:

# tar -xvf linuxconcept.tar satish.sh
OR
# tar --extract --file=linuxconcept.tar satish.sh

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:

# tar -jxvf linuxconcept.tar.bz2 satish.sh
OR
# tar --extract --file=linuxconcept.tar.bz2 satish.sh

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:

# tar -xvf linuxconcept-14-09-19.tar "file 1" "file 2" 

# tar –zxvf linuxconcept-14-09-19.tar.gz "file 1" "file 2" 

# tar -jxvf linuxconcept.tar.bz2 "file 1" "file 2"

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:

# tar -rvf linuxconcept-14-09-19.tar xyz.txt

# tar -rvf linuxconcept-14-09-19.tar php

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:

# tar -rvf linuxconcept-14-09-19.tar.gz xyz.txt

# tar -rvf linuxconcept.tar.bz2 xyz.txt

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:

# tar -czf - linuxconcept-14-09-19.tar | wc -c
12820480

# tar -czf - linuxconcept-14-09-19.tar.gz | wc -c
112640

# tar -czf - linuxconcept.tar.bz2 | wc -c
20480
- Advertisement -
- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Latest article

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...

How to Reset the MySQL Root Password

Do you want to reset the MySQL root password? Sometimes we forget the root password and not able to log in on...