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: 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: And I want to create a tar archive containing all three files, it can be done as: I can specify only specific files to archive as well, like:

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: 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:

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: This works for a gz compressed archive as: Or even for a bz2 compressed archive as:

List tar Contents

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

Untar Specific Files

Just one file can be extracted from a tar or tar.gz or tar.bz2 archive by specifying file name as: Similarly, you can specify multiple file names separated by space to extract them together in one go.

Untar using Wildcard

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

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): 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): 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: 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:

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.

Exclude Files

Excluding specific files can be a requirement while creating tar archives. This can be achieved with –exclude flag. 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: Similarly for bz2 archive:

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:

Summary 👨‍💻

tar is a useful utility on Unix/Linux systems for a 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.

15 tar Command Examples for Sysadmin and Developers - 8315 tar Command Examples for Sysadmin and Developers - 9815 tar Command Examples for Sysadmin and Developers - 6515 tar Command Examples for Sysadmin and Developers - 9815 tar Command Examples for Sysadmin and Developers - 9215 tar Command Examples for Sysadmin and Developers - 34