What is tar in Linux?
The tar command is used by Linux to archive files and directories into one compressed file, so that you can easily store, share and transfer all your data. Its name, “tape archive,” harks back to its origins in the storage of data on tape drives. More than just its legacy, tar is still relevant for its ability to create, extract, and manage archives fast. That is a fundamental command used by both the novice and advanced Linux user. And if you master it, it opens up powerful file management features.
1. Create a .tar Archive
What is an Archive in.tar?
A tar archive is a file format used to collect multiple files and directories into one single file. Archive or an archive, in this context, it means a bunch (a compressed file) of these files, along with their structure and their Metadata (permission, timestamps, directory hierarchy, etc.). There’s nothing inherently compressed about it: it is merely grouping the files in one location for simplified storage, distribution, or backup.
The. A tar format is a common format for packaging files on Linux and UNIX systems and is commonly used in conjunction with compressing utilities such as gzip, bzip2 or xz to reduce the size of the file.
To create an archive file containing multiple files or directories:
tar -cvf archive_name.tar file1 file2 directory/
Where
2. Extract a .tar Archive
What is Extracting a .tar Archive?
Extracting a .tar archive refers to the process of unpacking or decompressing the contents of a .tar file, making its files and directories accessible again. A .tar archive is a collection of files and directories bundled together into a single file, often for storage, transfer, or backup purposes. When you extract the archive, you retrieve and restore the original files and directory structure.
To extract files from a .tar archive:
tar -xvf archive_name.tar.
3. Compress a .tar File with gzip (.tar.gz)
What is Compressing a .tar File with gzip (.tar.gz)?
Compressing a .tar file with gzip (.tar.gz) means taking a .tar archive (a file that bundles multiple files and directories into one) and applying gzip compression to reduce its size. The resulting .tar.gz file is both an archive and a compressed file, making it easier to store or transfer while saving disk space.
To compress the archive using gzip:
tar -czvf archive_name.tar.gz file1 file2 directory/
4. Extract a .tar.gz File
What is Extracting a .tar.gz File?
Extracting a. tar. gz files, here is a guide on how to extract a. These files combine. tar (a archive of files and directories) with. gz (to save some space with gzip compression). The tar command is utilized to extract: -xzf (here -x is for extraction, -z is for gzip decompression, and -f is for file). Example: tar -xzf archive. tar. gz dumps everything out here into the current directory, where it is accessible and ready to be used.
The extraction process involves two steps:
To extract a gzip-compressed archive:
tar -xzvf archive_name.tar.gz
5. Compress a .tar File with bzip2 (.tar.bz2)
What is Compressing a .tar File with bzip2 (.tar.bz2)?
Compressing a. tar –bzip2 compresses to a. tar. bz2 file, which is actually an archive (. tgz – A tar file that has been compressed using the bzip2 compression algorithm. Preserves their structure or organization while performing redundancy on redundant file content.
To create a bzip2-compressed archive:
tar -cjvf archive_name.tar.bz2 file1 file2 directory/
6. Extract a .tar.bz2 File
What is Extracting a .tar.bz2 File?
Extracting a .tar.bz2 file refers to decompressing and unpacking its contents. A .tar.bz2 file is an archive (.tar) that has been compressed using the bzip2 compression algorithm. When you extract a .tar.bz2 file, you retrieve the original files and directories bundled in the archive.
To extract a bzip2-compressed archive:
tar -xjvf archive_name.tar.bz2
7. Compress a .tar File with xz (.tar.xz)
What is Compressing a .tar File with xz (.tar.xz)?
Compressing a .tar file with xz creates a .tar.xz file, which is a highly compressed archive format. The .tar portion bundles multiple files and directories into a single archive, while the .xz portion applies xz compression to reduce its size. This format is widely used in Linux and UNIX systems for distributing software, creating backups, and saving storage space.
To compress using xz:
tar -cJvf archive_name.tar.xz file1 file2 directory/
8. Extract a .tar.xz File
What is Extracting a .tar.xz File?
Extracting a .tar.xz file involves decompressing and unpacking its contents. A .tar.xz file is a compressed archive where multiple files and directories are bundled into a single .tar file, then compressed using the xz compression algorithm. When you extract the .tar.xz file, you restore its original files and directory structure.
To extract an xz-compressed archive:
tar -xJvf archive_name.tar.xz
9. List Contents of an Archive
What is Listing the Contents of an Archive?
Seeing the contents of an archive audience means simply looking at the information files and directories contained in it without extracting them. This can come in handy when you want to see what an archive contains before extracting it, validate its structure, or simply find specific files in the archive.
Listing Contents of tar File in Linux In Linux, the tar command is typically used to list contents of. tar,. tar. gz,. tar. bz2,. tar. In the zipbox → tar, tar.bz2 (bzip), tar.gz (gz), tar.xz, and other archive formats.
To view the contents of an archive without extracting:
tar -tvf archive_name.tar
10. Extract Specific Files from an Archive
What is Extracting Specific Files from an Archive?
Extracting files from an archive refers to the process of selecting specific files or directories inside a compressed or uncompressed archive and extracting them without the entire contents of the archive. It comes in handy when all you need is a handful of files from a massive archive, potentially saving time as well as disk space.
[So You Want to Extract Some Files Using tar In Linux?] tar,. tar. gz,. tar. bz2,. tar. xz, and similar formats.
To extract only specific files from an archive:
tar -xvf archive_name.tar file1 file2
Specify the exact filenames you want to extract.
Bonus Tips
Preserve Permissions While Archiving
To retain file permissions when creating an archive:
tar -cvpf archive_name.tar file1 file2 directory/
Exclude Specific Files or Directories
To exclude files or directories from the archive:
tar --exclude='*.log' -cvf archive_name.tar directory/
Verify an Archive
To check if an archive is intact:
tar -tvf archive_name.tar
Conclusion
The tar command is a powerful tool for managing archives in Linux. By mastering these 10 essential commands, you can handle various archiving and compression tasks efficiently. Whether you’re backing up files, compressing data, or extracting archives, tar is your go-to command!