Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
20 Important tar Commands in Linux (with Examples)

The tar commands, short for Tape Archive, is a powerful utility in Linux used for creating and managing archives of files and directories. It allows users to combine multiple files into a single file, making it easier to store, share, and back up data. The tar command can also compress these files to save space, making it a valuable tool for system administrators and users alike.

In this article, we’ll explore 20 important tar commands, along with examples and explanations. This guide is designed to be beginner-friendly, so that anyone can understand and use the tar command effectively.

Basic Syntax of the tar Commands

The general structure of the tar command is as follows:

tar [options] [archive_name.tar] [file or directory to be archived]

Key Components:

  • tar: The command itself.
  • [options]: Specifies the action to perform (e.g., create, extract, list).
  • [archive_name.tar]: The name of the archive file.
  • [file or directory]: The files or directories you want to include in the archive.

Understanding these components will help you effectively use the tar command for various tasks.


20 Common tar Commands with Examples

1. Create a Tar Archive

To create an archive from files or directories, use the -cvf options. This command bundles specified files and directories into a single archive file, making it easier to manage. Use this command when organizing multiple files into a single location.

tar -cvf archive_name.tar file1 file2 directory/
create an archive from files or directories, use the -cvf options

2. Extract Files from a Tar Archive

To extract files from an archive, use the -xvf options. This command will retrieve the files contained in the specified archive and restore them to their original locations. It’s particularly useful for accessing archived data.

tar -xvf archive_name.tar
This command will retrieve the files contained in the specified archive and restore them to their original locations.

3. Create a Compressed Tar Archive with Gzip

You can compress the archive using gzip with the -z option. This command not only creates an archive but also compresses it, resulting in a smaller file size. It is ideal for saving space when storing or transferring files.

tar -czvf archive_name.tar.gz file1 file2 directory/
This tar commands not only creates an archive but also compresses it, resulting in a smaller file size. It is ideal for saving space

4. Extract a Compressed Tar Archive (Gzip)

To extract a .tar.gz archive, use the same -x options but include the -z for decompression. This command will decompress and extract the files from the compressed archive, making them readily accessible for use.

tar -xzvf archive_name.tar.gz
This command will decompress and extract the files from the compressed archive

5. Create a Compressed Tar Archive with Bzip2

For higher compression, use the -j option for bzip2. This command creates an archive while applying bzip2 compression, which usually results in smaller file sizes compared to gzip. It’s great for maximizing space savings.

tar -cjvf archive_name.tar.bz2 file1 file2 directory/
This command creates an archive while applying bzip2 compression, which usually results in smaller file sizes compared to gzip

6. Extract a Bzip2 Compressed Tar Archive

To extract a .tar.bz2 archive, use the -xjvf options. This command will decompress and extract files from a bzip2-compressed archive, making them available for immediate use while maintaining their original structure.

tar -xjvf archive_name.tar.bz2
This command will decompress and extract files from a bzip2-compressed archive

7. List the Contents of a Tar Archive

To view the files in an archive without extracting them, use the -tvf options. This command provides a detailed list of all files contained within the specified archive, helping you verify its contents before extraction.

tar -tvf archive_name.tar
This command provides a detailed list of all files contained within the specified archive

8. Extract a Specific File from an Archive

If you want to extract a specific file, specify its name after the -xvf options. This command allows you to retrieve only the files you need from an archive, which can save time and storage space.

tar -xvf archive_name.tar file1
This command allows you to retrieve only the files you need from an archive

9. Add a File to an Existing Archive

To append a file to an existing tar archive, use the -rvf option. This command allows you to include additional files in an existing archive without having to recreate it, making updates convenient and efficient.

tar -rvf archive_name.tar newfile.txt
This command allows you to include additional files in an existing archive without having to recreate it

10. Delete a File from a Tar Archive

To remove a file from an archive, use the –delete option (only works with uncompressed archives). This command enables you to eliminate specific files from an existing archive, allowing for better file management and organization.

tar --delete -f archive_name.tar file_to_remove.txt
This command enables you to eliminate specific files from an existing archive

11. Extract Files to a Specific Directory

To extract files to a different directory, use the -C option. This command directs the extraction process to a specified location, providing flexibility in managing file organization after extraction.

tar -xvf archive_name.tar -C /path/to/directory
This command directs the extraction process to a specified location, providing flexibility in managing file organization after extraction

12. Compress an Archive with Xz

For maximum compression, you can use the -J option. This command creates an archive and compresses it with xz, which typically achieves better compression rates than gzip and bzip2, making it ideal for reducing file sizes.

tar -cJvf archive_name.tar.xz file1 file2 directory/
This command creates an archive and compresses it with xz, which typically achieves better compression rates than gzip and bzip2

13. Extract an Xz Compressed Archive

To extract a .tar.xz file, use the -xJvf options. This command will decompress and extract the files from the xz-compressed archive, allowing you to access the archived data while retaining the original file structure.

tar -xJvf archive_name.tar.xz
This command will decompress and extract the files from the xz-compressed archive

14. Create an Archive from Multiple Directories

To create an archive containing multiple directories, use the command with multiple paths. This command bundles several directories into a single archive, simplifying file management and making sure that related files are stored together.

tar -cvf archive_name.tar /dir1 /dir2 /dir3
This command bundles several directories into a single archive

15. Exclude Specific Files or Directories

To exclude files or directories when creating an archive, use the –exclude option. This command allows you to specify patterns for files or directories that should not be included in the archive, enhancing control over the contents.

tar -cvf archive_name.tar --exclude='*.log' directory/
This command allows you to specify patterns for files or directories that should not be included in the archive

16. View the Progress of a Tar Archive

To monitor the progress while creating or extracting an archive, use –checkpoint. This command provides visual feedback during the archiving process, indicating progress at specified intervals, which is helpful for large operations.

tar -cvf archive_name.tar directory/ --checkpoint=.100
This command provides visual feedback during the archiving process

17. Extract Multiple Archives in a Directory

To extract multiple .tar.gz files in a directory at once, can use a loop. This command processes each matching archive file in the current directory, extracting all contents quickly without needing to run separate commands for each.

for file in *.tar.gz; do tar -xzvf "$file"; done
This command processes each matching archive file in the current directory

18. Extract Files Without Overwriting Existing Files

If you want to avoid overwriting existing files during extraction, use the –skip-old-files option. This command safeguards existing files by preventing any file from being replaced, making sure of data integrity during the extraction process.

tar -xvf archive_name.tar --skip-old-files
This command safeguards existing files by preventing any file from being replaced

19. Check the Integrity of a Tar Archive

To verify the integrity of an archive without extracting it, use the -W option. This command checks the file structure of the specified archive, making sure that it is complete and free of errors, which is crucial for data reliability.

tar -tvf archive_name.tar -W

20. Compress Files Using the GNU Tar Compression

You can specify the compression program directly with –use-compress-program. This command allows you to select which compression method to use while creating an archive, providing flexibility depending on your specific needs for file size and compatibility.

tar --use-compress-program=gzip -cvf archive_name.tar.gz file1 file2
This command allows you to select which compression method to use while creating an archive

Additional Information

Benefits of Using tar

  • File Management: Easily combine multiple files into a single archive for better organization.
  • Compression: Save disk space by compressing files.
  • Backup: Create backups of important files and directories quickly.
  • Portability: Easily share a single archive file instead of multiple files.

Common File Extensions

  • .tar: Uncompressed tar archive.
  • .tar.gz: Gzip compressed tar archive.
  • .tar.bz2: Bzip2 compressed tar archive.
  • .tar.xz: Xz compressed tar archive.

Conclusion

The tar command is a fundamental tool for file management in Linux, allowing users to create, extract, and manipulate archives easily. Mastering these 20 commands will enhance your productivity and simplify tasks related to data organization, backups, and file transfers. By using tar, you can combine multiple files and directories into a single archive for easier management, save disk space with compression tools like gzip and bzip2, and extract specific files as needed, making sure you retrieve only what you require. Additionally, the command allows you to verify the integrity of your archives, maintaining data reliability for backups. Incorporating the tar command into your workflow provides a versatile solution for managing data effectively. Whether you’re a beginner or an experienced user, these commands will prove invaluable in various computing scenarios. Feel free to revisit this guide for reference and share it with others looking to enhance their Linux skills. Happy archiving!

About the writer

Vinayak Baranwal Article Author

This article was written by Vinayak Baranwal, a skilled system administrator, content writer, and AI development enthusiast with expertise in delivering detailed, research-driven content. Vinayak specializes in simplifying complex technical topics, making them accessible to a broad audience while ensuring precision and clarity. His work spans IT services, web hosting, and server solutions, providing valuable insights for businesses and developers alike.

For more insightful content or collaboration opportunities, feel free to connect with Vinayak on LinkedIn through the provided link.

Leave a Reply

Your email address will not be published. Required fields are marked *

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers