
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.
tar Command SyntaxBelow is the basic tar command syntax:
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 OptionsThe 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.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.
Create a sample tar_dir directory.
Switch to the new tar_dir directory.
Create sample files such as file.txt using the touch command.
Create a new .tar archive using the c option with all files.
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.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.
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:
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.
Your output should be similar to the one below:
tar CommandYou 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.
Your output should be similar to the one below:
Extract specific files such as file1.txt and file3.txt from archive.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.
Extract the archive.tar contents to the extract_dir directory.
The above command extracts all files from archive.tar to the extract_dir directory.
Navigate to the extract_dir directory.
List all files and verify that the extracted files are available.
Output:
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 from archive.tar.gz.
The -z option enables the tar 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 from archive.tar.bz2.
Use the -J option to extract files from XZ compressed archives. For example, run the following command to extract files from archive.tar.xz.
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.
0 Comments
Be the first to comment and share your perspective with the community.