How To Extract Files Using Tar in Linux
![How To Extract Files Using Tar in Linux header image](https://i.postimg.cc/FHfsBGvb/1160.png)
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:
$ tar [options] [archive-file] [file or directory to extract]
Within the above command:
[options]
: Includes command line options that specify and modify thetar
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 thetar
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.
Switch to your user's home directory.
console$ cd
Create a sample
tar_dir
directory.console$ mkdir tar_dir
Switch to the new
tar_dir
directory.console$ cd tar_dir
Create sample files such as
file.txt
using thetouch
command.console$ touch file.txt file1.txt file2.txt file3.txt nil.txt
Create a new
.tar
archive using thec
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 thefile.txt
files. Within the command:-c
: Creates a new archive.v
: Displays thetar
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 whilef
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 togrep
to search all files with thefile
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.
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
Extract specific files such as
file1.txt
andfile3.txt
fromarchive.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.
Create a new
extract_dir
directory.console$ mkdir extract_dir
Extract the
archive.tar
contents to theextract_dir
directory.console$ tar -xf archive.tar -C extract_dir
The above command extracts all files from
archive.tar
to theextract_dir
directory.Navigate to the
extract_dir
directory.console$ cd extract_dir
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.
Use the
-z
option to extract.tar.gz
or.tgz
Gzip compressed archives. For example, run the following command to extract files fromarchive.tar.gz
.console$ tar -xzf archive.tar.gz
The
-z
option enables thetar
command to decompress Gzip archives.Use the
-j
option to extract files from Bzip2 compressed archives. For example, run the following command to extract files fromarchive.tar.bz2
.console$ tar -xjf archive.tar.bz2
Use the
-J
option to extract files from XZ compressed archives. For example, run the following command to extract files fromarchive.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.