Tutorials  /  Linux Basics

tar Command in Linux: Extract Files & Unpack Archives

Ccentron Redaktion · March 2025 ·8 min read ·Linux Basics, Tutorial

Tar, short for Tape Archive, is a built-in Linux utility designed for archiving and managing files. It consolidates multiple files into a single archive with a .tar extension. The tar command enables users to create, compress, and extract archives in different formats, ensuring an efficient and structured way to handle large data sets.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

This guide will walk you through the process of extracting files using the tar command in Linux. You will learn how to extract compressed and uncompressed archive formats, including .tar.gz, .tar.xz, and .tar.bz2, to retrieve multiple files.

Syntax of the tar Command

The general syntax for using the tar command is:

Console
$ tar [options] [archive-file] [file or directory to extract]

In this command:

  • [options]: Specifies various command-line options that define how the tar command behaves.
  • [archive-file]: Refers to the name of the archive file, which may have extensions like .tar, .tar.gz, or .tar.bz2 based on the compression format.
  • [file or directory to extract]: Indicates the specific files or directories to extract from the archive. Multiple files or directories can be specified within the command.

Options for Extracting Files with tar

The tar command includes various options that facilitate file extraction in Linux. Below are some of the commonly used ones:

  • -c: Creates a new archive.
  • -x: Extracts files from an archive.
  • -f: Specifies the name of the target archive.
  • -v: Enables verbose mode, displaying progress details during execution.
  • -t: Lists the contents of an archive without extracting them.
  • -z: Activates Gzip compression.
  • -j: Activates Bzip2 compression.
  • -J: Activates XZ compression.

Creating Archives Using the tar Command

The -c option in the tar command enables you to generate archives on your Linux system. Follow the steps below to create an archive using tar with sample files that you can later extract.

Step 1: Navigate to Your Home Directory

Start by switching to your user’s home directory:

Console
$ cd

Step 2: Create a New Directory

Now, create a directory named tar_dir:

Console
$ mkdir tar_dir

Step 3: Switch to the New Directory

Move into the newly created tar_dir directory:

Console
$ cd tar_dir

Step 4: Generate Sample Files

Use the touch command to create multiple sample text files:

Console
$ touch file.txt file1.txt file2.txt file3.txt nil.txt

Step 5: Create a Tar Archive

Now, use the -c option to create a new .tar archive containing all the sample files:

Console
$ tar -cvf archive.tar file.txt file1.txt file2.txt file3.txt nil.txt

The command above creates an archive file named archive.tar that includes all specified files. Here's what each option does:

  • -c: Generates a new archive.
  • -v: Displays the progress of the tar command.
  • -f: Defines the name of the archive file.
  • file.txt file1.txt file2.txt file3.txt nil.txt: Lists the files to be included in the archive.

Listing Archive Contents Using the tar Command

Before extracting files from an archive, you can first review its contents using the -t option in the tar command. This helps you confirm the files stored in the archive and identify the specific ones you need to extract. Follow the steps below to list the contents of archive.tar.

Step 1: Display the Archive Contents

To see a list of all files within archive.tar, run the following command:

Console
$ tar -tf archive.tar

This command does the following:

  • -t: Lists the contents of the archive.
  • -f: Specifies the archive file name.

Example Output:

file.txt
file1.txt
file2.txt
file3.txt
nil.txt

Step 2: Search for Specific Files in an Archive

If you need to locate a specific file within the archive, you can combine tar with grep. The grep command filters the archive contents to find matching filenames without extracting the entire archive.

Use the following command to search for files containing the term "file":

Console
$ tar -tf archive.tar | grep "file"

Example Output:

file.txt
file1.txt
file2.txt
file3.txt

Extracting Files Using the tar Command

The -x option in the tar command allows you to extract all files from an archive or retrieve specific files as needed. Follow the steps below to extract files from archive.tar, which you previously created.

Step 1: Extract All Files from an Archive

To extract all files stored in archive.tar, run the following command:

Console
$ tar -xvf archive.tar

Example Output:

file.txt
file1.txt
file2.txt
file3.txt
nil.txt

Step 2: Extract Specific Files from an Archive

If you only need to extract specific files, such as file1.txt and file3.txt, use the following command:

Console
$ tar -xvf archive.tar file1.txt file3.txt

Extracting Files to a Specific Directory Using the tar Command

By default, the tar command extracts files into the current working directory. However, you can specify a different directory using the -C option. Follow the steps below to extract files from archive.tar into a designated directory.

Step 1: Create a Directory for Extraction

First, create a new directory named extract_dir to store the extracted files:

Console
$ mkdir extract_dir

Step 2: Extract Files to the New Directory

Use the following command to extract all files from archive.tar into extract_dir:

Console
$ tar -xf archive.tar -C extract_dir

This command does the following:

  • -x: Extracts files from the archive.
  • -f: Specifies the archive file.
  • -C extract_dir: Extracts the contents into the extract_dir directory instead of the current directory.

Step 3: Navigate to the Extracted Files

Once extraction is complete, move into the target directory:

Console
$ cd extract_dir

Step 4: Verify the Extracted Files

Run the ls command to list the extracted files and confirm their presence:

Console
$ ls

Example Output:

file1.txt  file2.txt  file3.txt  file.txt  nil.txt

Extracting Files from Compressed Archives Using the tar Command

Compressed archives help reduce file size, making storage and transfer more efficient. The tar command allows you to extract files from compressed archives by specifying the compression method. Follow the steps below to extract files from compressed formats such as .tar.gz, .tar.xz, and .tar.bz2.

Extracting Gzip Compressed Archives (.tar.gz or .tgz)

Use the -z option with the tar command to extract files from Gzip-compressed archives. Run the following command:

Console
$ tar -xzf archive.tar.gz

The -z option tells tar to handle Gzip-compressed archives.

Extracting Bzip2 Compressed Archives (.tar.bz2)

To extract files from a Bzip2-compressed archive, use the -j option:

Console
$ tar -xjf archive.tar.bz2

Extracting XZ Compressed Archives (.tar.xz)

For extracting files from an XZ-compressed archive, use the -J option:

Console
$ tar -xJf archive.tar.xz

Conclusion

In this guide, you have learned how to use the tar command to create and extract compressed archives efficiently. Using tar, you can organize, compress, and extract files for better file management and faster data transfer.

For additional options and details, run the following command to access the tar manual:

Console
$ man tar
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren