How To Extract Files Using Tar in Linux

Updated on February 14, 2025
How To Extract Files Using Tar in Linux header image

Tar, also known as Tape Archive, is a built-in Linux archiving utility that creates and manages archives by combining multiple files into a single archive file with the .tar extension. The tar command is used to create, compress, and extract archives in various formats, providing an organized and systematic way to efficiently manage large amounts of data.

This article explains how to extract files using tar Command in Linux. You will extract compressed and non-compressed archive formats, including tar.gz, tar.xz, and tar.bz2 to multiple files.

The tar Command Syntax

Below is the basic tar command syntax:

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

Within the above command:

  • [options]: Includes command line options that specify and modify the tar command functionality.
  • [archive-file]: Specifies the target archive name, such as .tar, .tar.gz, or .tar.bz2, depending on the compression method.
  • [file or directory to extract]: Specifies optional files or directories to extract from the archive. You can enter multiple files and directories to extract from the target archive.

tar Command Options

The tar command supports the following command options for extracting files in Linux.

  • c: Creates a new archive.
  • -x: Extracts files from an archive.
  • -f: Specifies the target archive.
  • -v: Verbose mode displays the tar command progress.
  • -t: Lists the archive contents.
  • -z: Enables Gzip compression.
  • -j: Enables Bzip2 compression.
  • -J: Enables XZ compression.

Create Archives Using Tar

The c option allows you to create achieves using Tar on your Linux workstation. Follow the steps below to create archives using Tar with sample files you can extract.

  1. Switch to your user's home directory.

    console
    $ cd
    
  2. Create a sample tar_dir directory.

    console
    $ mkdir tar_dir
    
  3. Switch to the new tar_dir directory.

    console
    $ cd tar_dir
    
  4. Create sample files such as file.txt using the touch command.

    console
    $ touch file.txt file1.txt file2.txt file3.txt nil.txt
    
  5. Create a new .tar archive using the c option with all files.

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

    The above command creates a new archive.tar with the file.txt files. Within the command:

    • -c: Creates a new archive.
    • v: Displays the tar command progress.
    • f: Specifies the archive filename.
    • file.txt file1.txt file2.txt file3.txt nil.txt: Specifies the files to add to the archive.

List Archive Contents Using Tar

Before extracting files from an archive, you can list its contents using the -t option with the tar command. This allows you to verify the archive contents and identify the target files to extract. Follow the steps below to list the archive.tar contents.

  • List the archive.tar contents.

    console
    $ tar -tf archive.tar
    

    The above command lists all files in archive.tar. The -t option enables the listing of the archive contents while f specifies the target archive.

    Output:

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

Search Specific Files in an Archive

You can search specific files in an archive by combining the tar command with grep. The grep command searches for specific files or patterns without extracting the entire archive. Follow the steps below to search specific files by listing archive.tar and filtering the contents using the grep command.

  • List all files in archive.tar and pipe the output to grep to search all files with the file search term.

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

    Your output should be similar to the one below:

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

Extract Files Using the tar Command

You can extract all files from an archive or specific files using the x tar command option. Follow the steps below to extract files from archive.tar you created earlier.

  1. Extract all files from archive.tar.

    console
    $ tar -xvf archive.tar
    

    Your output should be similar to the one below:

    file.txt
    file1.txt
    file2.txt
    file3.txt
    nil.txt
  2. Extract specific files such as file1.txt and file3.txt from archive.tar.

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

Extract Files to a Specific Directory Using Tar

The tar command extracts files to your working directory by default unless you specify a target directory. You can extract files from an archive to a specific directory using the -C option with the tar command. Follow the steps below to extract files from archive.tar to a specific directory.

  1. Create a new extract_dir directory.

    console
    $ mkdir extract_dir
    
  2. Extract the archive.tar contents to the extract_dir directory.

    console
    $ tar -xf archive.tar -C extract_dir
    

    The above command extracts all files from archive.tar to the extract_dir directory.

  3. Navigate to the extract_dir directory.

    console
    $ cd extract_dir
    
  4. List all files and verify that the extracted files are available.

    console
    $ ls
    

    Output:

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

Extract Files From Compressed Archives Using Tar

Compressed archives reduce the size of files for storage and transfer. The tar command allows you to extract files from compressed archives by specifying the compression type. Follow the steps below to extract files from compressed archives such as tar.gz, .tar.xz, and tar.bz2 using the tar command.

  1. Use the -z option to extract .tar.gz or .tgz Gzip compressed archives. For example, run the following command to extract files from archive.tar.gz.

    console
    $ tar -xzf archive.tar.gz
    

    The -z option enables the tar command to decompress Gzip archives.

  2. Use the -j option to extract files from Bzip2 compressed archives. For example, run the following command to extract files from archive.tar.bz2.

    console
    $  tar -xjf archive.tar.bz2
    
  3. Use the -J option to extract files from XZ compressed archives. For example, run the following command to extract files from archive.tar.xz.

    console
    $  tar -xJf archive.tar.xz
    

Conclusion

You have used the tar command to create and extract archives. You can use the tar command to create archives and compress files for faster file sharing and management. For more information and command options, run the man tar command to view the Tar manual page.