
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 thetarcommand 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 thetarcommand 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_dirdirectory.console$ mkdir tar_dir
Switch to the new
tar_dirdirectory.console$ cd tar_dir
Create sample files such as
file.txtusing thetouchcommand.console$ touch file.txt file1.txt file2.txt file3.txt nil.txt
Create a new
.tararchive using thecoption 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.tarwith thefile.txtfiles. Within the command:-c: Creates a new archive.v: Displays thetarcommand 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.tarcontents.console$ tar -tf archive.tar
The above command lists all files in
archive.tar. The-toption enables the listing of the archive contents whilefspecifies 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.tarand pipe the output togrepto search all files with thefilesearch 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.txtExtract specific files such as
file1.txtandfile3.txtfromarchive.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_dirdirectory.console$ mkdir extract_dir
Extract the
archive.tarcontents to theextract_dirdirectory.console$ tar -xf archive.tar -C extract_dir
The above command extracts all files from
archive.tarto theextract_dirdirectory.Navigate to the
extract_dirdirectory.console$ cd extract_dir
List all files and verify that the extracted files are available.
console$ lsOutput:
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
-zoption to extract.tar.gzor.tgzGzip compressed archives. For example, run the following command to extract files fromarchive.tar.gz.console$ tar -xzf archive.tar.gz
The
-zoption enables thetarcommand to decompress Gzip archives.Use the
-joption 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
-Joption 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.