Extracting tar files is a basic task in Linux and Unix based systems. The tar utility is designed to consolidate multiple files into a single archive file. This archive file can be compressed using different algorithms like gzip, bzip2 and xz. After compression, the archive file can be unpacked with the untar command. This article provides a comprehensive overview of 30 useful Untar commands with examples. These commands help users become more familiar with extracting tar files.
How to Install Ansible on Ubuntu 22... How to Install Ansible in Ubuntu 22 04 LTS Jammy Jellyfish – Ansible Install
Basic Untar command
Before we get into the different Untar commands, let's start with the basic Untar command, which you can use to extract a tar file.
command
tar -xf filename.tar
explanation
The above command extracts the contents of a tar file named filename.tar into the current directory. The -x option tells tar to extract the contents of the archive, and the -f option specifies the archive filename.
Extract compressed tar files
Tar files are often compressed to reduce their size. Here are some useful Untar commands for extracting compressed tar files.
1. Extract a gzip compressed tar file
command
tar -xzf filename.tar.gz
explanation
This command extracts the contents of a gzip-compressed tar file named filename.tar.gz into the current directory. The -z option tells tar that the file is gzip compressed.
2. Extract a bzip2 compressed tar file
command
tar -xjf filename.tar.bz2
explanation
This command extracts the contents of a bzip2 compressed tar file named filename.tar.bz2 into the current directory. The -j option tells tar that the file is bzip2 compressed.
3. Extract xz compressed tar file
command
tar -xJf filename.tar.xz
explanation
This command extracts the contents of an xz-compressed tar file named filename.tar.xz into the current directory. The -J option tells tar that the file is xz-compressed.
Extract specific files from a tar archive
Sometimes you just need to extract specific files from a tarball. Here are some useful Untar commands for extracting specific files from a tarball.
4. Extracting a specific file from a tar archive
command
tar -xf filename.tar path/to/file
explanation
This command extracts a specific path/to/file from a tar archive named filename.tar into the current directory.
5. Extract all files that match a pattern
command
tar -xf Dateiname.tar --Wildcards '*.txt'
explanation
This command extracts all files matching the pattern "*.txt" from a tar archive named filename.tar into the current directory. The –wildcards option tells tar to use wildcards to match filenames.
Extracting tarballs to another directory
By default, tar extracts files into the current directory. However, you can also extract files to a different directory. Here are some useful untar commands for extracting tarballs to another directory.
6. Extract to a specific directory
command
tar -xf filename.tar -C /path/to/directory
explanation
This command extracts the contents of a tar archive named filename.tar to the /path/to/directory directory.
7. Extract to a subdirectory
command
tar -xf filename.tar -C /path/to/directory/subdirectory
explanation
This command extracts the contents of a tar archive named filename.tar to the /path/to/directory/subdirectory subdirectory.
8. Extract to a directory with a different name
command
tar -xf filename.tar --transform 's/old/new/' -C /path/to/directory
explanation
This command extracts the contents of a tar archive named filename.tar to the /path/to/directory directory, but renames all files with the string "old" in their name to "new".
Checking the integrity of the tarball
Sometimes it is important to verify that a tar archive is valid and not corrupt. Here are some useful untar commands to check the integrity of tarballs.
9. Checking the contents of a tar archive
command
tar -tf filename.tar
explanation
This command lists the contents of a tar archive named filename.tar without extracting any files.
10. Checking the integrity of a gzip compressed tar archive
command
tar -tzf filename.tar.gz
explanation
This command verifies the integrity of a gzip-compressed tar archive named filename.tar.gz without extracting any files.
11. Checking the integrity of a bzip2 compressed tar archive
command
tar -tjf filename.tar.bz2
explanation
This command verifies the integrity of a bzip2-compressed tar archive named filename.tar.bz2 without actually extracting any files.
12. Checking the integrity of an xz compressed tar archive
command
tar -tJf filename.tar.xz
explanation
This command verifies the integrity of an xz-compressed tarball named filename.tar.xz without actually extracting any files.
Updating tar archives
Sometimes you may need to add or remove files from a tarball. Here are some useful untar commands for updating tarballs.
13. Adding files to a tarball
command
tar -rf filename.tar file1 file2
explanation
This command adds file1 and file2 to an existing tar archive named filename.tar.
14. Remove files from a tar archive
command
tar -f filename.tar --path/to/delete file
explanation
This command removes a specific file named path/to/file from an existing tar archive named filename.tar.
15. Updating a tar archive
command
tar -uf filename.tar file1 file2
explanation
This command adds file1 and file2 to an existing tar archive named filename.tar, but updates all files with the same name.
Working with sparse files
Sparse files are files that contain large sections of zeros, and Tar can handle sparse files efficiently. Here are some useful unzip commands to work with sparse files.
16. Extract a sparse file
command
tar -xSf filename.tar
explanation
This command extracts a sparse file named filename.tar.
17. Creating a sparse file
command
tar -Scf filename.tar directory
explanation
This command creates a sparse file named filename.tar from the contents of a directory.
Dealing with permissions and property
When extracting tarballs, it is important to preserve file permissions and ownership. Here are some useful Untar commands for handling permissions and ownership.
18. Extracting a tarball with preserved permissions
command
tar -xpzf filename.tar.gz
explanation
This command extracts a gzip compressed tar archive named filename.tar.gz while preserving file permissions.
19. Extracting a tarball with preserved property
command
tar -xpf filename.tar --numeric-owner
explanation
This command extracts a tar archive named filename.tar while preserving file ownership. The –numeric-owner option tells tar to use numeric user and group IDs.
20. Change ownership of extracted files
command
tar -xpf filename.tar --owner=username --group=groupname
explanation
This command extracts a tar archive named filename.tar and changes ownership of the extracted files to the specified username and groupname.
Extracting multiple tarballs
Sometimes you may need to extract multiple tarballs at the same time. Here are some useful untar commands for extracting multiple tarballs.
21. Extract multiple tarballs at once
command
Cat Archive1.tar Archive2.tar | tar-x
explanation
This command extracts the contents of archive1.tar and archive2.tar.
22. Extract multiple gzip compressed tarballs at once
command
Cat Archive1.tar.gz Archive2.tar.gz | tar-xz
explanation
This command extracts the contents of archive1.tar.gz and archive2.tar.gz.
23. Extract multiple bzip2 compressed tarballs at once
command
Cat Archive1.tar.bz2 Archive2.tar.bz2 | tar-xj
explanation
This command extracts the contents of archive1.tar.bz2 and archive2.tar.bz2.
Working with large tarballs
Extracting large tarballs can be time consuming and consume a lot of system resources. Here are some useful untar commands to work with large tarballs.
24. Extracting a tarball in a background process
command
tar -xf filename.tar &
explanation
This command extracts a tar archive named filename.tar in a background process so you can continue using the terminal.
25. Extracting a tar archive with progress information
command
tar -xzf filename.tar.gz --checkpoint=10000
explanation
This command extracts a gzip compressed tar archive named filename.tar.gz that shows progress information every 10,000 records.
26. Extracting a tarball with limited disk space
command
tar -xzf filename.tar.gz --checkpoint=10000 --checkpoint-action=exec='pkill -STOP tar; sleep 1; pkill -CONT tar'
explanation
This command extracts a gzip compressed tar archive named filename.tar.gz, which limits the amount of memory used by tar. When the memory limit is reached, tar will pause for a second before continuing.
Other useful Untar commands
Here are some other useful unzip commands that you may find helpful.
27. Extract a tar archive and ignore directory components
command
tar -xf filename.tar --strip-components=1
explanation
This command extracts a tar archive named filename.tar, ignoring the first directory component.
28. Extract a tarball and exclude specific files
command
tar -xf filename.tar --exclude=path/to/file
explanation
This command extracts a tar archive named filename.tar, excluding a specific file named path/to/file.
29. Extract a tarball and show only errors
command
tar -xvf filename.tar 2>&1 >/dev/null | grep -i 'error\|warning'
explanation
This command extracts a tar archive named filename.tar and only displays errors and warnings.
30. Extract a tarball and get timestamps
command
tar -xpf filename.tar --atime-preserve
explanation
This command extracts a tar archive named filename.tar while preserving the access times of the extracted files.
Diploma
Extracting tar files is a fundamental skill for anyone working on Linux or Unix based systems. The above commands provide a solid foundation for working with tarballs of all types and sizes. Whether you're extracting specific files, maintaining permissions and ownership, or working with large archives, these commands will help you get the job done quickly and efficiently. With a little practice you will become proficient in dealing with tar archives and will be able to handle more complex tasks without any problems.
frequently asked Questions
Q: What is a tar file in Linux?
A: A tar file is an archive file format used in Linux operating systems to bundle files and directories into a single file. The .tar extension indicates that the file is a tar archive.
Q: How do I unpack a tar file on Linux?
A: You can use the tar command to extract a tar file on Linux. The basic syntax is "tar -xf filename.tar". This will extract the contents of the tar file into the current directory.
Q: What are some useful options for the tar command?
A: Some useful options for the tar command are -v (verbose mode), -z (compress or decompress files with gzip), -c (create a new archive), and -t. (list the contents of an archive).
Q: How many commands will I learn in the blog post on extracting tar files on Linux?
A: The blog post covers 30 useful commands to extract tar files on Linux. You will learn different techniques and options to make the process more efficient and effective.
Q: Is it difficult to extract a tar file on Linux?
A: No, extracting a tar file on Linux is relatively easy. With the right commands and options, you can extract files and directories quickly and easily. The blog post includes step-by-step instructions and examples to help you master the process.
Q: Can I extract multiple tar files at once on Linux?
A: Yes, you can extract multiple tar files at once on Linux using the tar command with the -f option. For example, you can extract two tar files named file1.tar and file2.tar by running the command tar -xf file1.tar file2.tar.
Q: Can I extract specific files from a tarball on Linux?
A: Yes, you can extract specific files from a tarball on Linux using the tar command with the -x and -f options. For example, you can extract a file named file.txt from a tar archive named archive.tar by running the command tar -xf archive.tar file.txt.
Q: Can I extract a tar file with a different name on Linux?
A: Yes, you can extract a tar file with a different name on Linux by using the "mv" command to rename the file after extraction. For example, you can extract a tar file named archive.tar and rename it to new-archive.tar by running the command tar -xf archive.tar && mv archive.tar new-archive.tar .