Oct 18, 2023 6 min read

Tar Command in Linux (Create and Extract Archives)

Use tar command in linux with our step-by-step tutorial. It is used for creating archives to store files on magnetic tape.

Tar Command in Linuxtr
Table of Contents

Introduction

Before we discuss Tar command in Linux (Create and Extract Archives), let's first understand-What is Tar Command ?

Tar was created with the intention of creating archives to store files on magnetic tape, hence the name "Tape ARchive."

By transforming a set of files into an archive, the tar command creates tar files. It can also extract tar archives, show a list of the files in the archive, add new files to an existing archive, and do a variety of other tasks.

In this tutorial, you will use the tar command to extract, list, and build tar archives. We will also address a few FAQs on Tar Command in Linux (Create and Extract Archives).

tar Command Syntax

There are two versions of tar: BSD tar and GNU tar, each with its own set of features. GNU tar is pre-installed on most Linux computers by default.

The tar command has the following general syntax:

tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
  • OPERATION - There is only one operation argument that can be used.

--create (-c) - Creating a new tar archive is the most commonly utilized operation.

--extract  (-x) - Extract an archive's complete contents or single or several files.

--list  (-t) - Shows a list of all the files in the archive.

  • OPTIONS - The following are the most commonly utilized operations:

    --verbose (-v) - Display the files that the tar command is processing.

    --file=archive=name (-f archive-name)- The archive filename is specified.
  • ARCHIVE_NAME - The archive's name.
  • FILE_NAME(s) - A list of filenames to extract from the archive, separated by spaces. If you don't offer any information, the full archive will be extracted.

You can use the long or short form of the tar operations and options when running tar commands. Long forms are easier to read, whereas short ones are quicker to type. A double dash is used to prefix the long-form options (--). A single dash (-) precedes the short-form alternatives, which can be removed.

Create Tar Archive

Gzip, bzip2, lzip, lzma, lzop, xz, and compress are just a few of the compression algorithms Tar supports. It is a standard practice to append the compressor suffix to the archive file name when producing compressed tar archives. An archive that has been compressed with gzip, for example, should be titled archive.tar.gz.
Use the -c option followed by -f and the archive name to generate a tar archive.

For instance, to make an archive named archive.tar from the files file1, file2, and file3, use the following command:

tar -cf archive.tar file1 file2 file3

Using the long-form options, this is the equivalent command:

tar --create --file=archive.tar file1 file2 file3

The contents of one or more directories or files can be used to generate archives. Unless the --no-recursion option is given, directories are archived recursively by default.

The following example creates a user_backup.tar archive in the /home/user directory:

tar -cf backup.tar /home/user

If you wish to see the files that are being processed, use the -v option.

Creating Tar Gz Archive

The most often used algorithm for compressing tar files is Gzip. When using gzip to compress tar archives, the archive name should end in either tar.gz or tgz.

The -z option instructs tar to compress the archive as it is produced using the gzip method. To make a tar.gz archive from a set of files, for example, run the following command:

tar -czf archive.tar.gz file1 file2

Creating Tar Bz2 Archive

Bzip2 is another widely used technique for compressing tar files. The archive name should end with either tar.bz2 or tbz when using bzip2.

Invoke tar with the -j option to compress the archive using the bzip2 method. The command below produces a tar.bz2 archive from the specified files:

tar -cjf archive.tar.bz2 file1 file2

List Tar Archives

The tar command lists the contents of a tar archive without extracting it when used with the --list (-t) option.

The following command displays the contents of the archive.tar file:

tar -tf archive.tar
file1
file2
file3

Use the --verbose (-v) option to acquire more information such as the file owner, file size, and timestamp:

tar -tvf archive.tar
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file1
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file2
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file3

Extract Tar Archives

In Linux, most archived files are compressed and archived using the tar or tar.gz formats. It's crucial to know how to extract these files from the command line.

Use the --extract (-x) option followed by the archive name to extract a tar archive:

tar -xf archive.tar

The -v option is commonly used to print the names of the files being extracted.

tar -xvf archive.tar

Extract Tar Archive in a Different Directory

By default, tar extracts the contents of the archive into the current working directory. To extract archive files in a specific directory, use the --directory (-C) option:

To extract the contents of an archive to the /opt/files directory, for example, use:

tar -xf archive.tar -C /opt/files

Extract Tar Gz and Tar Bz2 Archives

You don't need to supply a decompression option when extracting compressed archives like tar.gz or tar.bz2. The command is identical to that used to extract a tar archive:

tar -xf archive.tar.gz
tar -xf archive.tar.bz2

Extract Specific Files from a Tar Archive

You may just need to extract a few files from an archive rather than the entire archive.

Append a space-separated list of file names to be extracted after the archive name to extract a specific file(s) from a tar archive:

tar -xf archive.tar file1 file2

When extracting files, you must provide the same names and path as produced by --list, (-t).

It's the same as extracting files from an archive to extract one or more directories:

tar -xf archive.tar dir1 dir2

If you try to extract a file that doesn't exist, you'll get an error message that looks like this:

tar -xf archive.tar README
Output

tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

Extract Files from a Tar Archive using Wildcard

Use the --wildcards switch to extract files from an archive based on a wildcard pattern, and quote the pattern to prevent the shell from interpreting it.

To extract files with names ending in .js (JavaScript files), for example, you can use:

tar -xf archive.tar --wildcards '*.js'

Add Files to Existing Tar Archive

Use the --append (-r) function to add files or directories to an existing tar archive.

To add a file named newfile to archive.tar, for example, type:

tar -rvf archive.tar newfile

Remove Files from a Tar Archive

To delete files from an archive, use the --delete operation.

The example below demonstrates how to remove the file file1 from archive.tar:

tar --delete -f archive.tar file1

FAQs on Tar Command in Linux

How does the tar command work?

The tar command creates an archive by concatenating files together. When extracting, it reads the archive and restores the files and directories to their original form.

How do I use the tar command to create an archive?

To create an archive using tar, specify the archive's name followed by the files or directories you want to include. For example, tar -cf archive.tar file1 file2 dir1 will create an archive called archive.tar containing file1, file2, and dir1.

Can tar compress archives?

No, the tar command alone does not compress archives. However, it can work in conjunction with other compression tools like gzip or bzip2 to compress the archive file.

How do I compress an archive using tar?

To compress an archive using tar, make use of the appropriate compression tool with the -z option for gzip compression or the -j option for bzip2 compression. For example, tar -czf archive.tar.gz file1 file2 dir1 will create a compressed archive using gzip.

Can I extract a compressed archive directly using tar?

Yes, tar can automatically detect and decompress certain compressed archives. For example, tar -xf archive.tar.gz will automatically extract a gzip-compressed archive.

How do I view the contents of an archive without extracting it with tar?

To view the contents of an archive, use the -t option with tar. For example, tar -tf archive.tar will display a list of files and directories within archive.tar.

Can I specify an output directory for extraction using tar?

Yes, you can use the -C option followed by the directory path to specify an output directory for extraction. For example, tar -xf archive.tar -C /path/to/directory will extract the archive to the specified directory.

Conclusion

The tar command is most commonly used to build and extract tar archives. Use the tar -xf command followed by the archive name to extract an archive, and tar -czf followed by the archive name and the files and folders you wish to add to the archive to create a new one.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.