How to Create and Extract archive file using tar command

The Linux tar command stands for tape archiver, is the most widely used archiving program designed to store multiple files in a single file (an archive). This is commonly known as a collection of files.

An archive file is a composed file contains one or more files along with metadata into a single file for easier portability and storage.

File compression is an essential utility in Linux, which helps you free up disk space usage, reduce file size and share files efficiently.

tar has an integrated compression mechanism. However, you can add one more layer of compressing using the gzip or bzip2 command.

The tar command is included by default in all distributions and can be easily installed from the distribution’s official repository.

yum install tar     [RHEL 7]
dnf install tar     [RHEL 8/9, Fedora]
apt install tar     [Debian/Ubuntu]
zypper in tar       [OpenSUSE]
pacman -S tar       [Arch Linux]

tar has numerous option and we have included the most widely used options in the below table.

OptionDescription
-c, –createCreate a new archive.
-x, –extractExtract files from an archive.
-t, –listList the contents of an archive.
-p, –preservePreserve the permissions of files in an archive to restore later.
-f, –fileUse archive file instead of device.
-j, –bzip2Compress and Extract the archive through bzip2
-z, –gzipCompress and Extract the archive through gzip
–exclude=PATTERNExclude files matching PATTERN
-r, –appendAppend files to the end of an archive.
-W, –verifyVerify the archive after writing it.

In this tutorial, we’ll show you some tar command examples which are frequently used by administrator in daily operation.

Syntax:

tar [Operation Mode[ [Options] [Archive_File_Name] [File or Directory to be archived]

1) Creating tar archive file

The following example will backup the entire /etc directory files/directories to the myarchive.tar file and store the backup file in the /backup/config-backup directory.

tar -cvf /backup/config-backup/myarchive.tar /etc

2) Create and compress archive file with gzip

We can compress archive file while creating it. The following example will backup the entire /opt directory contents to the myarchive.tar.gz file with 'gzip' compression and store the backup file in the /backup/config-backup directory.

tar -zcvf /backup/config-backup/myarchive.tar.gz /opt
or
tar -zcvf /backup/config-backup/myarchive.tgz /opt

3) Create and compress archive file with bzip2

We can compress archive file while creating it. The following example will backup the entire /etc & /opt directory contents to the myarchive.tar.bz2 file with 'bzip2' compression and store the backup file in the /backup/config-backup directory.

tar -jcvf /backup/config-backup/myarchive.tar.bz2 /etc
or
tar -jcvf /backup/config-backup/myarchive.tbz2 /etc /opt
or
tar -jcvf /backup/config-backup/myarchive.tbz /etc /opt

Output:

The bzip2 utility uses better compression ratio among others. It typically compresses files to within 10% to 15% of the best available technique.

ll -h

total 72M
drwxr-xr-x 2 root root 4.0K Feb 26 05:57 ./
drwxr-xr-x 3 root root 4.0K Feb 26 05:28 ../
-rw-r--r-- 1 root root  58M Feb 26 05:33 myarchive.tar
-rw-r--r-- 1 root root 5.4M Feb 26 05:57 myarchive.tar.bz2
-rw-r--r-- 1 root root 8.3M Feb 26 05:48 myarchive.tar.gz

4) Listing the content of compressed archive file

If you want to view the content of a compressed archive file without extracting it, use the following format.

tar -tvf /backup/config-backup/myarchive.tar       [For tar]
tar -ztvf /backup/config-backup/myarchive.tar.gz   [For gzip]
tar -jtvf /backup/config-backup/myarchive.tar.bz2  [For bzip2]

Also, you can check whether a specific file or directory is available from tar file. Let’s check if the passwd file is available or not.

tar -tvf myarchive.tar etc/passwd

-rw-rw-r-- root/root 85633 2022-06-27 22:14 /etc/passwd

5) Appending files to the archive

You can easily append or add files & directory to existing archive file using '-r' option. Let’s add myfile & mydir to myarchive.tar.

tar -rvf /backup/config-backup/myarchive.tar myfile mydir

myfile
mydir

Make a Note: You won’t be able to append files or directory to an archive file with compressed by ‘gzip’ or ‘bzip2’. If you try, you will end up with below error messages.

tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

6) Extracting tar archive file

To extract an archive file, use -x option in the tar command as shown below. This will extract all the files and directories of myarchive.tar file in the current working directory.

tar -xvf myarchive.tar      [For tar]
tar -xzvf myarchive.tgz     [For gzip]
tar -xjvf myarchive.tbz2    [For bzip2]

7) Extract tar archive to the specified directory

In case if you want to extract tar file to the specified directory, invoke the -C option followed by the directory:

tar -xvf myarchive.tar -C /tmp/        [For tar]
tar -xzvf myarchive.tar.gz -C /tmp/    [For gzip]
tar -xjvf myarchive.tar.bz2 -C /tmp/   [For bzip2]

8) Extracting a specific file from tar archive

If you need to extract a specific file from the tar archive that can be done as shown below: In this example, we will extract the “/etc/sudoers” file from the archive.

tar -xvf archive.tar etc/sudoers        [For tar]
tar -xzvf archive.tar.gz etc/sudoers    [For gzip]
tar -xjvf archive.tar.bz2 etc/sudoers   [For bzip2]

To extract a file to a different directory, run:

tar -xvf archive.tar etc/sudoers -C /tmp
etc/sudoers

9) Extracting multiple file from tar archive

To extract multiple files, use the following example: We will extract “ifcfg-ens192” and “yum.conf” file from the archive.

tar -xvf archive.tar etc/sysconfig/network-scripts/ifcfg-ens192 etc/yum.conf   [For tar]
tar -xzvf archive.tar etc/sysconfig/network-scripts/ifcfg-ens192 etc/yum.conf  [For gzip]
tar -xjvf archive.tar etc/sysconfig/network-scripts/ifcfg-ens192 etc/yum.conf  [For bzip2]

10) Excluding specific directory while creating archive file

We can exclude whole directory while creating archive file using the --exclude option in the tar command. In this scenario, we will exclude the ‘/home/2daygeek/video’ directory when creating the tar archive file.

tar --exclude=/home/2daygeek/video -zcvpf /backup/site-backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek

To exclude multiple directories, run:

tar --exclude=/path/to/the/directory-1 --exclude=/path/to/the/directory-2 -zcvpf /backup/site-backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek

12) Excluding specific file type while creating archive

We can exclude specific file type while creating archive file using the --exclude option in the tar command. To exclude the file type of ‘png’ when creating a compressed tar archive file, run:

tar -zcpvf /backup/site-backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek --exclude=*.png

To exclude multiple file types, run: In this example, we will exclude ‘png’ and ‘html’ files.

tar -zcpvf /backup/site-backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek --exclude=*.png --exclude=*.html

13) Extracting specific file type from tar archive

If you want to extract a specific file type from an archive for some reason, use the --wildcards switch. To extract the ‘.png’ file from tar archive, run:

tar -xvf myarchive.tar --wildcards '*.png'

Closing Thoughts

In this tutorial, we’ve shown you how to use the tar command to create and extract archive files on Linux with 13 practical examples.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to Create and Extract archive file using tar command”

Leave a Reply

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