---
title: " How to Compress Files in Linux Using the tar Command"
url: https://docs.vultr.com/how-to-compress-files-in-linux-using-the-tar-command
description: Learn how to compress files in Linux using the tar command with step-by-step instructions, common options, and practical examples for efficient file archiving.
publish_date: 2024-08-16T14:01:41.733689Z
last_updated: 2023-11-21T20:23:49.337033Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1892/98e38892-43a4-47ec-be25-f3b452c40540.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

The `tar` command in Linux enables archiving and compression of files and directories. It's also known as tape archive (`tar`) and combines multiple files into a single archive file with the `.tar` extension that can be compressed to save disk space to simplify file transfers and backups.

This article explains how to use the `tar` command in Linux to perform archiving and file compression tasks.

## `tar` Command Syntax

Below is the basic `tar` command syntax:

```console
$ tar [options] [archive-file] [file/directory]
```

Within the above command, `[options]` modifies the `tar` command behavior, `[archive-file]` specifies the archive filename, and `[file/directory]` specifies the files or directories to include in the archive.

### `tar` Command Options

| Option  | Description                                             |
|---------|---------------------------------------------------------|
| `-c`    | Create a new archive.                                   |
| `-x`    | Extract files from an archive.                          |
| `-v`    | Verbose mode. Display progress in the terminal.         |
| `-f`    | Filename. Specify the archive filename.              |
| `-z`    | Compress the archive using gzip.                        |
| `-j`    | Compress the archive using bzip2.                       |
| `-J`    | Compress the archive using xz.                          |
| `-t`    | List the contents of an archive.                        |
| `-r`    | Append files to an existing archive.                    |
| `-u`    | Update files in an existing archive.                    |

## Practical Examples of the `tar` Command

1. Create a new tar archive.

    ```console
    $ tar -cvf archive.tar file1.txt file2.txt directory/
    ```

    The above command creates a new `archive.tar` that contains the file `file1.txt`, `file2.txt`, and all contents in `directory/`.

    Output:

    ![Creating a tar archive](https://docs.vultr.com/public/doc-assets/1892/090404e3-62d9-4f67-aac2-2fce58ef7eb5.gif)

1. Create a new compressed `tar` archive using Gzip.

    ```console
    $ tar -czvf archive.tar.gz file1.txt file2.txt directory/
    ```

    The above command creates a new `archive.tar.gz` and enables compression using Gzip.

    Output:

    ![Creating a gzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/7302ef86-916f-44c4-bf2f-96fce035509b.gif)

1. Extract files from a tar archive.

    ```console
    $ tar -xvf archive.tar
    ```

    The above command extracts all files from the `archive.tar` file into your working directory.

    Output:

    ![Extracting a tar archive](https://docs.vultr.com/public/doc-assets/1892/b20a20c0-3995-478a-88b2-3fb37d5adb17.gif)

1. Extract files from a compressed `tar` archive.

    ```console
    $ tar -xzvf archive.tar.gz
    ```

    The above command extracts all files from the compresses `archive.tar.gz` file to the working directory.

    Output:

    ![Extracting a gzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/0b4d7b57-c147-4603-a5c7-c9d93d7be58a.gif)

1. List all contents in a tar archive.

    ```console
    $ tar -tvf archive.tar
    ```

    The above command lists all contents in the `archive.tar` file without extraction.

    Output:

    ![Listing contents of a tar archive](https://docs.vultr.com/public/doc-assets/1892/5749bd4a-aa88-4a40-8cdc-8a25a46d70cb.gif)

1. Create a compressed `tar` archive using Bzip.

    ```console
    $ tar -cjvf archive.tar.bz2 file1.txt file2.txt directory/
    ```

    The above command creates a new compressed `archive.tar.bz2` file using the `bzip2` utility.

    Output:

    ![Creating a bzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/777cffa9-273e-475e-a8c8-8c06dbfc468d.gif)

1. Extract files from a Bzip `tar` archive.

    ```console
    $ tar -xjvf archive.tar.bz2
    ```

    The above command extracts all files from the `archive.tar.bz2` to the your working directory.

    Output:

    ![Extracting a bzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/5b484fb6-b72a-4846-b17b-47fb9cc44562.gif)

1. Create a new `tar` archive using the `xzipped` compression format.

    ```console
    $ tar -cJvf archive.tar.xz file1.txt file2.txt directory/
    ```

    The above command creates a new compressed `archive.tar.xz` file using xz.

    Output:

    ![Creating an xzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/52befb2b-4039-49fd-bcdf-853201799808.gif)

1. Extract files from an `xzipped` archive file.

    ```console
    $ tar -xJvf archive.tar.xz
    ```

    The above command extracts files from the `archive.tar.xz` file to your working directory.

    Output:

    ![Extracting an xzipped tar archive](https://docs.vultr.com/public/doc-assets/1892/d0d465c6-a169-4a82-9923-64e9c4f9cf77.gif)

1. Add new files to an existing tar archive.

    ```console
    $ tar -rvf archive.tar newfile
    ```

    The above command appends the file `newfile` to the existing `tar` archive `archive.tar`.

    Output:

    ![Appending files to an archive](https://docs.vultr.com/public/doc-assets/1892/de9b789d-452a-452e-850b-c5140dd6d6f1.gif)

1. Update files in an existing tar archive.

    ```console
    $ tar -uvf archive.tar updatedfile
    ```

    The above command updates the `updatedfile` file in the `archive.tar` archive if its newer than the existing version.

    Output:

    ![Updating files in an archive](https://docs.vultr.com/public/doc-assets/1892/5f19e741-a31d-4c0e-9005-2dcfe5dc47f1.gif)

## Advanced Usage Scenarios

1. Exclude files and directories from `tar` archive.

    ```console
    $ tar --exclude='*.log' -cvf archive.tar directory/
    ```

    The above command creates a new `archive.tar` file that includes all files except `.log` files in `directory/`.

    Output:

    ![Excluding files in an archive](https://docs.vultr.com/public/doc-assets/1892/8c131023-ed4e-464f-b3f6-7ac0d0271f2d.gif)

1. Compress and archive files to a remote location.

    ```console
    $ tar -czvf - directory/ | ssh user@remotehost "cat > /path/to/destination/archive.tar.gz"
    ```

    The above command compresses `directory/` and transfers it to `remotehost` using SSH.

    Output:

    ```
    vultr_user@vultr:~$ tar -czvf - /home/vultr_user/dir1 | ssh vultr_user@123.84.123.123 "cat > /home/vultr_user/dir2/archive.tar.gz"
    tar: Removing leading `/' from member names
    /home/vultr_user/dir1/
    /home/vultr_user/dir1/file2.txt
    /home/vultr_user/dir1/file1.txt
    /home/vultr_user/dir1/file3.txt
    The authenticity of host '123.84.123.123 (123.84.123.123)' can't be established.
    ED25519 key fingerprint is SHA256:kfJi2tl589s4r3mdDLq32Ziksl1j5f2sXM6aUw.
    This key is not known by any other names.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    vultr_user@139.84.176.145's password: 
    vultr_user@vultr:~$ 
    ```

1. Extract a specific file from a tar archive.

    ```console
    $ tar -xvf archive.tar path/to/file
    ```

    The above command extracts `path/to/file` from `archive.tar`.

    Output:

    ![Extracting a specific file](https://docs.vultr.com/public/doc-assets/1892/b6a8315c-344f-4808-a8e6-3094c2bec9b2.gif)

1. Verify the integrity of a tar archive.

    ```console
    $ tar -tvf archive.tar > /dev/null
    ```

    The above command lists all contents of `archive.tar` and discards the output to verify the file integrity.

1. Use `tar` with `find` to archive specific files.

    ```console
    $ find . -name "*.txt" -print0 | tar -cvf archive.tar --null -T -
    ```

    The above command finds all `.txt` files and archives them to the `archive.tar` file.

    Output:

    ![Using tar with find](https://docs.vultr.com/public/doc-assets/1892/55131ba4-2a68-4975-a6f3-dd0a791dbf1c.gif)

1. Extract files to a specific directory.

    ```console
    $ tar -xvf archive.tar -C /path/to/directory/
    ```

    The above command extracts files from `archive.tar` to `/path/to/directory/`.

    Output:

    ![Extracting to a specific directory](https://docs.vultr.com/public/doc-assets/1892/75e7f050-f8c1-4101-a81f-855ac5e98995.gif)

1. Create a new tar archive split into multiple volumes

    ```console
    $ tar -cvf - directory/ | split -b 5M - archive_part_
    ```

    The above command creates a new tar archive using files from `directory/` and splits it into multiple volumes of `5 MB` for each file.

    Output:

    ![Creating split archives](https://docs.vultr.com/public/doc-assets/1892/1c386624-bfe0-4d16-91c5-55dcd88106d6.gif)

## Conclusion

You have used the `tar` command to archive and compress files, manage backups, and optimize storage on your system. For more command options, run the `man tar` command to view the `tar` manual page.
