---
title: How to Compress and Decompress Files Using tar and gzip
description: "Compressing and decompressing files is essential for saving disk space and transferring files efficiently. In Linux, the `tar` and `gzip` commands are commonly used for these purposes. Below is a guide on how to use these tools for compressing and…"
url: "https://truehost.com/support/knowledge-base/how-to-compress-and-decompress-files-using-tar-and-gzip/"
updated: "2024-11-02"
category: Cloud servers in Kenya
---

# How to Compress and Decompress Files Using tar and gzip

Compressing and decompressing files is essential for saving disk space and transferring files efficiently. In Linux, the `tar` and `gzip` commands are commonly used for these purposes. Below is a guide on how to use these tools for compressing and decompressing files.

### What are tar and gzip?

- **tar**: The `tar` command (short for "tape archive") is used to create, maintain, modify, or extract files from an archive file. It groups multiple files into a single file, often referred to as a tarball (typically with a `.tar` extension).
- **gzip**: The `gzip` command is used to compress files, reducing their size for storage or transfer. It typically adds a `.gz` extension to the compressed files.

### Installing tar and gzip

Most Linux distributions come with `tar` and `gzip` pre-installed. You can verify their availability by running:

```
tar --versiongzip --version
```

If they are not installed, you can install them using your package manager.

On Debian/Ubuntu:

```
sudo apt updatesudo apt install tar gzip
```

On RHEL/CentOS:

```
sudo yum install tar gzip
```

### How to Compress Files with tar and gzip

1. **Creating a tar.gz Archive**

- To compress files and directories into a single `.tar.gz` file, you can use the `tar` command with the `-z` option for gzip compression. The general syntax is:

```
tar -czvf archive_name.tar.gz file1 file2 directory1
```

**Options Explained**:

- `c`: Create a new archive.
- `z`: Compress the archive with `gzip`.
- `v`: Verbose mode (optional, shows progress in the terminal).
- `f`: Specify the filename of the archive.

**Example**:

```
tar -czvf my_files.tar.gz file1.txt file2.txt my_directory/
```

- This command creates a compressed file named `my_files.tar.gz` containing `file1.txt`, `file2.txt`, and the entire `my_directory`.

1. **Creating a tar Archive (without gzip compression)**

- If you want to create a `.tar` file without compression, omit the `-z` option:

```
tar -cvf archive_name.tar file1 file2 directory1
```

### How to Decompress Files with tar and gzip

1. **Extracting a tar.gz Archive**

- To decompress a `.tar.gz` file, use the following command:

```
tar -xzvf archive_name.tar.gz
```

**Options Explained**:

- `x`: Extract files from the archive.
- `z`: Uncompress the archive with `gzip`.
- `v`: Verbose mode (optional).
- `f`: Specify the filename of the archive.

**Example**:

```
tar -xzvf my_files.tar.gz
```

- This command extracts the contents of `my_files.tar.gz` into the current directory.

1. **Extracting a tar Archive (without gzip compression)**

- To extract a `.tar` file without gzip compression, use:

```
tar -xvf archive_name.tar
```

### Summary of Commands

- **Compressing files and directories**:

```
tar -czvf archive_name.tar.gz file1 file2 directory1
```

- **Extracting .tar.gz files**:

```
tar -xzvf archive_name.tar.gz
```

- **Compressing files without gzip**:

```
tar -cvf archive_name.tar file1 file2 directory1
```

- **Extracting .tar files**:

```
tar -xvf archive_name.tar
```

### Author's Final Word

Using `tar` and `gzip`, you can efficiently compress and decompress files on Linux. This guide provides a basic overview of how to use these commands for file archiving and compression, allowing you to save disk space and streamline file transfers.
