# Linux commands: gzip

> Learn how the Linux gzip command compresses a file into a .gz archive, how to keep the original with -k, set a level from 1 to 9, and decompress with -d.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-10-09 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-gzip/

You can compress a file using the gzip compression protocol named [LZ77](https://en.wikipedia.org/wiki/LZ77_and_LZ78) using the `gzip` command.

Here's the simplest usage:

```bash
gzip filename
```

This will compress the file, and append a `.gz` extension to it. The original file is deleted. To prevent this, you can use the `-c` option and use output redirection to write the output to the `filename.gz` file:

```bash
gzip -c filename > filename.gz
```

> The `-c` option specifies that output will go to the standard output stream, leaving the original file intact

Or you can use the `-k` option:

```bash
gzip -k filename
```

There are various levels of compression. The more the compression, the longer it will take to compress (and decompress). Levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), and the default is 6.

You can choose a specific level with the `-<NUMBER>` option:

```bash
gzip -1 filename
```

You can compress multiple files by listing them:

```bash
gzip filename1 filename2
```

You can compress all the files in a directory, recursively, using the `-r` option:

```bash
gzip -r a_folder
```

The `-v` option prints the compression percentage information. Here's an example of it being used along with the `-k` (keep) option:

![Terminal showing gzip -kv wget-log command output displaying 49.7% compression ratio and replaced with wget-log.gz](https://flaviocopes.com/images/linux-command-gzip/Screen_Shot_2020-09-09_at_15.55.42.png)

`gzip` can also be used to decompress a file, using the `-d` option:

```bash
gzip -d filename.gz
```

The `gzip` command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
