# A Simple Guide to Compressing Files with tar and gzip


Do you have a large file or directory that you want to archive and compress to save space or send to someone?

Using the tar and [gzip commands in Linux](https://developnsolve.com/compressing-files-with-gzip-on-linux) provides an easy way to bundle and shrink down files. With just a few keystrokes, you can quickly prepare a compressed archive ready for transfer or storage.

In this beginner's guide, I'll walk you through the basic steps to tar and gzip a file using the Linux terminal. We'll cover:

- What tar and gzip do
- How to tar a file or folder
- Compressing the tar archive with gzip
- Decompressing the files later

Follow along below and you'll have mastered the Linux file compression duo in no time.

## Understanding tar and gzip

**Tar** is an archiving utility that bundles together files and folders into one aggregated file but does not compress the data. **Gzip** is a compression program that significantly reduces file size using advanced algorithms.

By combining both in sequence, you get the archiving capabilities of tar with the size benefits of compression from gzip. The final .tar, .gz file takes up way less room but still holds all your original data.

This makes tar+gzip ideal for backing up important material, storing old projects, or emailing hefty digital assets. The compressed file transfers faster and takes up less drive space.

Now let's see them in action...

## Step 1) Tar the File or Directory

The `tar` command syntax generally follows:

```plaintext
tar [options] <source_files> [destination_archive.tar]
```

Some useful options:

- `-c` creates a new tar archive
- `-v` prints verbosely the files being stored
- `-f` specifies the tar filename to write to

For example, to recursively tar an entire directory _myproject/_ into an archive called _myproject.tar_, do:

```plaintext
tar -cvf myproject.tar myproject/
```

This bundles up all folders, subfolders, and files into _myproject.tar_. Check your file explorer and you should see the new tar archive present.

**_Pro Tip:_** _You can tarball multiple sources by listing more directories/files._

## Step 2) Compress with Gzip

Next, pipe the new tar file into gzip to compress it.

```plaintext
gzip [options] <input_file>
```

Common options:

- `-k` keeps the original tarball intact after zipping
- `-#` sets compression level 1-9 (9 highest)

Let's run:

```plaintext
gzip -k9 myproject.tar
```

Now observe _myproject.tar_ shrink down into a compressed _myproject.tar.gz_ version. The original stays put but all the content got squeezed into a smaller package.

**_Pro Tip:_** _Use gzip first then tar if you want to archive and compress a single file instead of a directory._

## Step 3) Decompress Files Later

Opening compressed archives is just as easy. Extract anywhere with:

```plaintext
tar -xvf myproject.tar.gz
```

This unpacks _myproject.tar.gz_ into a _myproject/_ folder with all original content reconstructed. To also decompress, add-in `gunzip`:

```plaintext
gunzip myproject.tar.gz
tar -xvf myproject.tar
```

Now you have your data and space back!

## Key Takeaways

That's all there is to basic tar+gzip compression and extraction on Linux. To quickly recap:

- **tar** bundles files and directories into .tar archives
- **gzip** squeezes and optimizes tar archives into smaller .tar.gz or .tgz files
- Combining allows easy archiving and compression in one go
- Options like **\-c**, **\-v,** and **\-f** customize the tar process
- Flags like **\-k** and **\-#** modify gzip performance
- Use **gunzip** and **tar -xvf** to later reconstitute your stuff

Compression helps organize and backup important documents, media, code repositories, and other digital assets while saving drive space. It also speeds up transfers like email attachments or cloud uploads considering the reduced size.

%[https://developnsolve.com/best-linux-firewall]

The tar/gzip duo is a Linux staple you'll be reaching for often either locally or on remote servers. So get comfortable with the commands through examples like my project folder above.

Before you know it, archiving and compressing files will feel like second nature.

