in

15 tar Command Examples for Sysadmin and Developers


tar is one of the popular file archiving formats available in Unix and Linux-based systems.

The name itself is derived from Tape ARchive as it was developed for writing sequential data on tape devices. It is also referred to as tarball sometimes. By default, tar only archives the files without compression but using some portions. We can use different compression techniques to get a compressed output. tar utility is usually included in most Linux distributions by default, and the format itself is supported across other operating systems, including Windows and macOS, via different tools and utilities.

We’ll be covering some of the common examples and usage of the tar command and its supported flags in this article. So let’s get started.

Create tar Archive

To create a simple uncompressed archive, the syntax for tar command is:

$ tar cvf <tar-file-name> <files-to-archive>

Here flags c stands for creation, v for verbose output and f for specifying the tar archive file name. By convention, specify the tar file name with .tar extension. Files to be archived can be specified with wildcards or as single or multiple file names/paths.

As an example, I’ve three files in my directory:

$ ls -l
total 12
-rw-r--r-- 1 abhisheknair abhisheknair 13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 24 Sep 12 20:08 file3.txt
$

And I want to create a tar archive containing all three files, it can be done as:

$ tar cvf archive.tar *
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
$

I can specify only specific files to archive as well, like:

$ tar cvf archive1.tar file1.txt file2.txt
file1.txt
file2.txt
$ ls -l archive1.tar
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive1.tar
$

Create Compressed Archive (GZ)

tar not only allows archiving of files but compressing them as well to save space. One of the popular compression formats is gunzip, usually represented by extension .gz after .tar or as tgz. We can use z flag to specify we need the files to be compressed using gunzip. Here’s an example:

$ tar cvzf archive.tar.gz file*
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar archive.tar.gz
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair   188 Sep 12 20:21 archive.tar.gz
$

You can observe that size of both archive files is substantially different even though both contain the same three files. This is due to the use of compression using z flag.

Create Compressed Archive (BZ2)

tar supports several other compression formats. One of them is bz2 or bzip2 which is represented by extension tar.bz2 or sometimes as tbz2. It may give you a smaller archive size but in turn consumes more CPU, so the process of compressing/decompressing could be slower than gz archive.

Example:

$ tar cvjf archive.tar.bz2 file*
file1.txt
file2.txt
file3.txt
$ ls -l archive.tar archive.tar.gz archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 12 20:15 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair   212 Sep 12 20:25 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair   188 Sep 12 20:21 archive.tar.gz
$ file archive.tar*
archive.tar:     POSIX tar archive (GNU)
archive.tar.bz2: bzip2 compressed data, block size = 900k
archive.tar.gz:  gzip compressed data, from Unix, original size modulo 2^32 10240
$

Untar All Files

A tar archive (whether compressed or uncompressed) can be extracted simply by using the x option. Below examples will clarify its usage:

$ tar xvf archive.tar
file1.txt
file2.txt
file3.txt
$ ls -l
total 24
-rw-r--r-- 1 abhisheknair abhisheknair 10240 Sep 19 18:25 archive.tar
-rw-r--r-- 1 abhisheknair abhisheknair    13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair    19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair    24 Sep 12 20:08 file3.txt
$

This works for a gz compressed archive as:

$ tar xvf archive.tar.gz
file1.txt
file2.txt
file3.txt
$ ls -l
total 16
-rw-r--r-- 1 abhisheknair abhisheknair 188 Sep 19 18:27 archive.tar.gz
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Or even for a bz2 compressed archive as:

$ tar xvf archive.tar.bz2
file1.txt
file2.txt
file3.txt
$ ls -l
total 16
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

List tar Contents

To list the contents of a tar archive, you can use t flag as shown below:

$ tar tvf archive.tar.bz2
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$

Untar Specific Files

Just one file can be extracted from a tar or tar.gz or tar.bz2 archive by specifying file name as:

$ tar xvf archive.tar.bz2 file1.txt
file1.txt
$ ls -l
total 8
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
$

Similarly, you can specify multiple file names separated by space to extract them together in one go.

$ tar xvf archive.tar.bz2 file1.txt file3.txt
file1.txt
file3.txt
$ ls -l
total 12
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Untar using Wildcard

To extract one or more files using a wildcard PATTERN, use --wildcards flag:

$ tar xvf archive.tar.bz2 --wildcards "file*"
file1.txt
file2.txt
file3.txt
$ ls -l
total 16
-rw-r--r-- 1 abhisheknair abhisheknair 212 Sep 19 18:31 archive.tar.bz2
-rw-r--r-- 1 abhisheknair abhisheknair  13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair  19 Sep 12 20:08 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair  24 Sep 12 20:08 file3.txt
$

Add Files to Archive

New files can be added/appended to existing uncompressed tarballs by using r or --append flag with new file names or wildcard pattern (remember this only works with uncompressed .tar files and not with tar.gz or tar.bz2 compressed formats):

$ tar rvf archive.tar file-new*
file-new.txt
file-new2.txt
$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new2.txt
$

You can observe that listing contents of archive.tar again shows the two newly added files.

Delete Files from Archive

Removing specific files from a tar archive is possible using --delete flag as shown below (compare the tar listing pre and post deletion of files):

$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new.txt
-rw-r--r-- abhisheknair/abhisheknair  9 2021-09-19 19:10 file-new2.txt
$ tar --delete -f archive.tar file-new.txt file-new2.txt
$ tar tvf archive.tar
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
-rw-r--r-- abhisheknair/abhisheknair 15 2021-09-19 18:59 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 10 2021-09-19 18:58 file4.txt
$

Again this works only for uncompressed tarballs and will fail for compressed archive formats.

Create with Verify

While creating uncompressed tar files, you can verify the contents of the archive using W flag as:

$ tar cvfW archive.tar file*.txt
file1.txt
file2.txt
file3.txt
Verify file1.txt
Verify file2.txt
Verify file3.txt
$

This can’t be used with compression flags though you can compress the created tar file later using gzip or other tools.

Extract tar to Folder

If you want to extract your tarball contents to a specific folder instead of the current directory, use -C flag with the directory path as shown below:

$ tar xvf archive.tar -C new-directory/
file1.txt
file2.txt
file3.txt
file2.txt
file4.txt
$ ls -l new-directory/
total 16
-rw-r--r-- 1 abhisheknair abhisheknair 13 Sep 12 20:08 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 15 Sep 19 18:59 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 24 Sep 12 20:08 file3.txt
-rw-r--r-- 1 abhisheknair abhisheknair 10 Sep 19 18:58 file4.txt
$

Use diff Flag

You can use --diff or d flag to find any changes between the files in the tar archive and the ones in the filesystem. Here’s an example that runs the diff once when the file inside the tar and outside was the same. Post updating the file, it was run once again to show the difference in output.

$ tar dvf archive.tar file4.txt
file4.txt
$
$ echo newline > file4.txt
$
$ tar dvf archive.tar file4.txt
file4.txt
file4.txt: Mod time differs
file4.txt: Size differs
$

Exclude Files

Excluding specific files can be a requirement while creating tar archives. This can be achieved with --exclude flag.

$ tar --exclude="dir/file2.txt" --exclude="dir/file-new*.txt" -cvzf archive.tar.gz dir/
dir/
dir/file1.txt
dir/file3.txt
$ ls -l dir
total 24
-rw-r--r-- 1 abhisheknair abhisheknair 9 Sep 19 19:10 file-new.txt
-rw-r--r-- 1 abhisheknair abhisheknair 9 Sep 19 19:10 file-new2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 5 Sep 19 19:20 file-new3.txt
-rw-r--r-- 1 abhisheknair abhisheknair 5 Sep 19 19:27 file1.txt
-rw-r--r-- 1 abhisheknair abhisheknair 6 Sep 19 19:27 file2.txt
-rw-r--r-- 1 abhisheknair abhisheknair 8 Sep 19 19:27 file3.txt
$ tar tvf archive.tar.gz
drwxr-xr-x abhisheknair/abhisheknair 0 2021-09-19 19:30 dir/
-rw-r--r-- abhisheknair/abhisheknair 5 2021-09-19 19:27 dir/file1.txt
-rw-r--r-- abhisheknair/abhisheknair 8 2021-09-19 19:27 dir/file3.txt
$

As you can note from the above output, we can specify the --exclude flag multiple times to specify multiple file names or patterns in AND condition. Note that out of the six files in the dir in the above example, only two files satisfied the condition to be included in archive.at.gz.

View tar Content Size

We can get the size of the contents of a compressed tar archive using the below command:

$ tar tvf archive.tar.gz
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$ tar -xzf archive.tar.gz --to-stdout|wc -c
56
$

Similarly for bz2 archive:

$ tar tvf archive.tar.bz2
-rw-r--r-- abhisheknair/abhisheknair 13 2021-09-12 20:08 file1.txt
-rw-r--r-- abhisheknair/abhisheknair 19 2021-09-12 20:08 file2.txt
-rw-r--r-- abhisheknair/abhisheknair 24 2021-09-12 20:08 file3.txt
$ tar -xjf archive.tar.bz2 --to-stdout|wc -c
56
$

Preserve Permissions

By defaults, tar command preserves permission of files and directories it is archiving though you can explicitly specify the same using -p flag or --preserve-permissions as shown below:

$ tar cvpzf archive.tar.gz *.txt
file1.txt
file2.txt
file3.txt
$

Summary 👨‍💻

tar is a useful utility on Unix/Linux systems for long and was primarily used in archiving and backup tasks. The utility has evolved with many options over time. It can be used for simple to complex tasks, given you know the features it offers. This article covered some of the basic operations you can do with tar command and showcases how it can help you in your daily system administration tasks.

Refer to its man page man tar or use tar --help or tar --usage command to get further details.



Source: https://geekflare.com/tar-commands-for-sysadmin-and-developers/

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

A facial recognition doorbell system using a Raspberry Pi

User Simulation for Rapid Outage Mitigation