[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedOctober 9, 2023

How to archive a file with tar gz. and gzip command.

How To Archive A File With Tar Gz. And Gzip Command.

 Archiving and compressing files is a common task in Linux and Unix-based systems. This article will guide you through the process of creating and extracting tar.gz files using the tar and gzip commands. We will cover the basic syntax, options, and practical examples to help you efficiently manage your files.

 

Creating Tar.gz Files:

Command Syntax:

The general form of the command for creating tar.gz files is as follows:

tar -czf archive-name.tar.gz file-name...
  • -c: Instructs tar to create a new archive.

  • -z: Sets the compression method to gzip.

  • -f archive-name.tar.gz: Specifies the archive name.

  • file-name…: A space-separated list of files and directories to be added to the archive.

 

Examples:

  1. Create an archive named “archive.tar.gz” from “file1” and “file2”:

    tar -czf archive.tar.gz file1 file2
  2. Create a tar.gz file from all “.jpg” files in the current directory:

    tar -czf images.tar.gz *.jpg
  3. Create an archive named “web_backup.tar.gz” of the “/var/www/website” directory:

    tar -czf web_backup.tar.gz /var/www/website

 

Extracting Tar.gz Files:

Command Syntax:

To extract a tar.gz file, use the following command:

tar -xzf archive-name.tar.gz
  • -x: Instructs tar to extract files.

  • -z: Specifies that the archive is compressed with gzip.

  • -f archive-name.tar.gz: Specifies the archive file name.

 

Examples:

  1. Extract the contents of “archive.tar.gz” in the current directory:

    tar -xzf archive.tar.gz
  2. Extract specific files from “archive.tar.gz”:

    tar -xzf archive.tar.gz file1 file2
  3. Extract all files from “archive.tar.gz” to a specific directory:

    tar -xzf archive.tar.gz -C /path/to/directory

 

Additional Tips:

  • Use the -v option to make the tar command verbose, displaying the names of the files being processed.

  • To create a tar.bz2 file, you can use the -j option instead of -z with the tar command.

 

Using Tar for Creating Archives:

Command for Creating an Archive:

To create a simple tar archive (without gzip compression), use the following command:

tar -cf archive-name.tar file-name...
  • -c: Instructs tar to create a new archive.

  • -f archive-name.tar: Specifies the archive name.

  • file-name…: A space-separated list of files and directories to be added to the archive.

This command will create an uncompressed tar archive, which can be useful if you want to keep the files in an archived but uncompressed format.

 

Example of Creating an Uncompressed Tar Archive:

To create a simple tar archive named “archive.tar” from “file1” and “file2,” use the command:

tar -cf archive.tar file1 file2

 

Adding and Deleting Files from an Existing Archive:

Adding Files to an Existing Archive:

To add new files to an existing archive, use the -r (append) option with the tar command:

tar -rf archive-name.tar new-file...
  • -r: Instructs tar to append files to the archive.

For example, to add a file named “newfile” to an existing archive named “archive.tar,” you would run:

bashCopy code

tar -rf archive.tar newfile

 

Removing Files from an Archive:

You can use the –delete operation to remove specific files from an existing tar archive:

tar --delete -f archive-name.tar file-to-delete...

For instance, to remove the file “file1” from “archive.tar,” you would execute:

tar --delete -f archive.tar file1

Including these additional details will provide readers with a more comprehensive understanding of how to work with tar and tar.gz files.

 

Conclusion:

In this comprehensive guide, we’ve explored how to create and work with tar and tar.gz files using the command-line interface in Linux and Unix-based systems. These archive formats are widely used for bundling and compressing files and directories.

To create a tar archive, you can use the tar -cf command, specifying the archive name and the files or directories you want to include. For gzip-compressed tar.gz archives, you can use the tar -czf command, adding the -z option for compression.

In addition to creating archives, we’ve covered how to extract files from tar and tar.gz archives using the tar -xf and tar -xzf commands. You can extract specific files or entire directories, making it a versatile tool for managing archived data.

We’ve also discussed how to list the contents of an archive with tar -tf and how to add files to existing archives using tar -rf. Moreover, you can delete specific files from an archive using tar –delete.

By mastering these commands, you can efficiently manage your data, create backups, and transfer files between systems while preserving their directory structure and permissions.

In conclusion, understanding how to work with tar and tar.gz files is a valuable skill for any Linux or Unix user, and it provides a powerful way to organize and compress data effectively.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM