Linux tar command to compress and extract files

Filed Under: UNIX/Linux

In this guide, we look at the Linux Tar command, Tar, short for Tape Archive, is a commonly used command that is used for created compressed files, known as tarball files which are easily portable from one disk to another. Furthermore, the command can be used to uncompress these archived files and further make some modifications. In this guide, we will dive and see tar command examples and see how to compress and extract files using the tar command.

Creating a Tar Archive file

To create a tar archive of a directory , use the syntax below

# tar -cvf tarball_name.tar  /path/to/directory

For example, the command below creates a tarball called james.tar from the home directory /home/james

# tar -cvf james.tar  /home/james/

Sample output

create a tarball file

Let’s take a look at the options


c - Used for creating a new .tar file
v - Verbosely outputs the creation of the .tar file
f - Defines the file name of the archive file

Creating a tar.gz archive file

To create a tar.gz file , use the -z option. the command below creates a tar.gz tarball from the /home/james/ directory as shown.

# tar -cvzf james.tar.gz  /home/james/

Sample output

Create A Tar Gz Archive

Creating a tar.bz2 archive file

The bz2 option is used to compress a more highly compressed tar file which is of lesser file size compared to gzip compression. However, it takes a longer time to achieve compression. To create a bz2 archive , use the -j option as shown in the example below

# tar -cvjf james.tar.bz2  /home/james/

OR

# tar -cvjf james.tar.tbz  /home/james/

OR

# tar -cvjf james.tar.tb2  /home/james/

Sample output

Create Bz2 File

Untar tar archive file

To uncompress or untar a .tar file , use the x option for extracting as shown

# tar -xvf james.tar

Sample output

Extract Tar File

To extract the file to a different directory, use the -C to specify the path to the directory

# tar -xvf james.tar  -C /opt

Sample output

Extract To A Specified Directory

Uncompressing a tar.gz file

To extract or uncompress a tar.gz file, use the -x option as shown

# tar -xvf james.tar.gz

Sample output

Extract A Tar Gz File

To extract it to a different directory, use the -C flag as shown in the previous example

Uncompressing a tar.bz2 file

Again the command for extracting the tar.bz2 takes the same approach as the 2 previous examples.

# tar -xvf james.tar.bz2

Sample output

Extract Tar Bz2

Listing contents of an archive file

if you want to list contents of a file before extracting the archive , use the -t option

To list contents of a tar file, run the command

# tar -tvf james.tar

Sample output

List Content In A Tar File

To list contents of a tar.bz2 file execute

# tar -tvf james.tar.bz2

Sample output

List Content In Tar Bz2 File

Likewise, for a tar.gz file, the command will be

# tar -tvf james.tar.gz

Sample output

List Content In A Tar Gz File

Adding a file or directory to a Tar archive

To add a file or a directory to a pre-existing archive, use the -r option as shown

# tar -rvf root.tar install.sh

In the above example, we have added the file install.sh to the tarball root.tar

Sample output

Add A File To An Archive

To add a directory, use the same syntax. in the example below, we are adding the directory data to the tarball root.tar

# tar -rvf root.tar data

Sample output

Add Directory To A Tar File

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

Similarly, to add a file or directory in a tar.gz or tar.bz2 file, use the -r option as shown

# tar -rvf root.tar.gz install.sh

Sample output

Add A File To A Tar Gz File

Likewise, for a tar.bz2 file, you will execute

# tar -rvf root.tar.bz2 install.sh

Sample output

Add A File To A Tar Bz2 File

Check the size of a tar , tar.gz and tar.bz2 files

To check the size of your tarball file, use the syntax as shown below

# tar -czf root.tar | wc -c

For a tar.gz file, this will be

# tar -czf root.tar.gz | wc -c

And finally, for a tar.bz2 file, run

# tar -czf root.tar.bz2 | wc -c

Sample output

Check Size Of Compressed Archives

This wraps up this tutorial. Give it a try and feel free to leave your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages