Introduction:
The Linux Tar utility is an indispensable tool for creating, extracting, and manipulating archived files in the TAR format. These archives are widely used in the Linux community for bundling and compressing files and directories. In this blog post, we will delve into the world of Linux Tar utility commands, exploring ten more essential commands that will revolutionize your experience with archives.
Creating a Tar Archive
To create a TAR archive, use the following command:
tar -cf archive.tar files/directoriesReplace “archive.tar” with your desired archive name and “files/directories” with the list of files or directories you want to include.
Extracting a Tar Archive:
To extract the contents of a TAR archive, use the following command:
tar -xf archive.tarThis command extracts all files and directories from the archive. If you only want to extract specific files, specify their names after the archive name.
Verbose Output:
For a detailed output of the files being processed, enable verbose mode using the “-v” option:
tar -cvf archive.tar files/directoriesThe “-v” flag provides a list of files as they are added to the archive.
Compression with gzip:
Combine the Tar utility with other compression utilities to create compressed archives. One popular choice is gzip. Use the following command to create a compressed archive:
tar -czf archive.tar.gz files/directoriesThe “-z” option indicates gzip compression. You can also use the “.tgz” extension if desired.
Extracting a Compressed Archive:
To extract a compressed archive, use this command:
tar -xzf archive.tar.gzThe “-x” option extracts files, while the “-z” option signifies gzip compression.
Exclude Files/Directories:
Exclude specific files or directories from the archive using the “–exclude” option:
tar -cf archive.tar --exclude="directory_to_exclude" files/directoriesReplace “directory_to_exclude” with the name of the directory you want to exclude.
Listing Contents:
To list the contents of an archive without extracting them, employ the “-t” option:
tar -tf archive.tarThis command displays the list of files and directories within the archive.
Append Files to an Existing Archive:
Add files or directories to an existing TAR archive with the “–append” option:
tar -rf archive.tar files/directoriesThis command appends new files to the end of the archive.
Extract to a Specific Directory:
Specify the target directory for extracting files using the “-C” option:
tar -xf archive.tar -C /path/to/directoryThis command extracts the archive contents to the specified directory.
Preserve Permissions and Ownership:
Maintain file permissions and ownership during extraction using the “–preserve-permissions” option:
tar -xf archive.tar --preserve-permissionsThis ensures that the extracted files retain their original permissions and ownership.
Extract a Single File:
To extract a single file from an archive, use the following command:
tar -xf archive.tar path/to/fileReplace “path/to/file” with the location of the file you wish to extract.
Extract a Specific Directory:
Extract only a specific directory from the archive with the “–strip-components” option:
tar -xf archive.tar --strip-components=1 path/to/directoryThis command extracts the specified directory while removing any leading components of the path.
Conclusion:
By mastering these Linux Tar commands, you can make your day to day linux administration much more efficient. It is always a wise idea to refer to the man page of tar utility

