FactorPad
Build a Better Process

Linux gzip Command Summary with Examples

Linux gzip allows you to compress and decompress files.
  1. Purpose - Learn what gzip is for an how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with gzip.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux gzip, 68 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / gzip-command.html


An ad-free and cookie-free website.


Examples of the Linux gzip Command

Beginner

Learn to compress and decompress files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux gzip Command Summary with Examples (4:01)

Video Script

The Command and Why You Need It

Our sixty-eighth word, or command to memorize is gzip from our category Files.

gzip allows you to compress and decompress files.

Common Linux gzip Options
-options description
-h, --help Print help screen
-d, --decompress Decompress files
-v, --verbose Report percent reduction in file size

Recall from the last four videos on software packages and most are compressed, so now we'll focus our attention on file compression.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The gzip command has 18 options and the argument is the file or list to compress. Several data compression algorithms exist, so it may get confusing. And then also note that related commands include gunzip and zcat.

Like most commands, help is available with double-dash --help. The -d option decompresses files and -v shows percent reduction.

So why is gzip an important command? Well, bandwidth and resources are limited, so getting to know how compression works is vital. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Start with a man command with a --where to search for the location of manual pages for the program gzip.

$ man --where gzip /usr/share/man/man1/gzip.1.gz

Notice how the file is given the extension .gz?

As it is with a program such as vim.

$ man --where vim /usr/share/man/man1/vim.1.gz

Or grep.

$ man --where grep /usr/share/man/man1/grep.1.gz

So it is standard for man pages to be compressed with gzip. And that's good to know.

Next, let's use that same command and create a file to compress.

$ man gzip > video68.txt

Okay, let's copy video68a.txt to video68b.txt.

$ cp video68a.txt video68b.txt

And there's a different way to do this of course, but I just wanted to go through this illustration. Then gzip -v video68b.txt.

$ gzip -v video68b.txt video68b.txt: 64.5% -- replaced with video68b.txt.gz

And then do an ls -og on video68*.

$ ls -og video68* -rw-r--r-- 1 17742 Nov 5 14:04 video68a.txt -rw-r--r-- 1 6322 Nov 5 14:04 video68b.txt.gz

And then, notice how the size changed here, so notice the difference and it replaced the old file with the new.

And last, let's decompress with -d and that ls -og video68* again.

$ gzip -d video68b.txt.gz $ ls -og video68* -rw-r--r-- 1 17742 Nov 5 14:04 video68a.txt -rw-r--r-- 1 17742 Nov 5 14:04 video68b.txt

So the takeaway here should be that, by default, gzip compresses files in place.

A Final Tip

Okay now you know how to use gzip. And you know the syntax for commands, options and arguments.

One last tip about the gzip command. So in the man page on gzip. Read through it, and it details additional functionality provided by gunzip and zcat.

Okay, thanks for visiting today. I hope this was a helpful introduction to the gzip command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you learned something today, please consider subscribing, following, joining and contributing to our group of smart folks who just get it.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / gzip-command.html


linux gzip command
gzip command
gzip linux
tar gz
linux gzip
gzip directory
extract gz
compress files
extract gz files
gz file
gzip a directory
unzip gz file linux
linux gzip examples
compression algorithm

A newly-updated free resource. Connect and refer a friend today.