Zip Gzip Bzip2 Linux Commands


zip, gzip, bzip2 are Linux commands commonly used when compressing, decompressing, or archiving files.

Each command has a corresponding format so is not compatible with other formats.
For example, zip command cannot extract .gz files. gzip command cannot decompress .bz2 files. bzip2 command cannot decompress .gz files, and so on.

In this article, for beginners (most likely contains me), I listed easy exercises for each command. It may be too easy, but I think it will be useful for true beginners.

For the tar command, please check the post.

zip Exercise

In advance, create two files which contain some text.

echo "Hello" >> hello.txt
echo "World" >> world.txt

Zip the two files.

zip helloworld hello.txt world.txt
# adding: hello.txt (stored 0%)
# adding: world.txt (stored 0%)

# hello.txt world.txt

helloworld is the name of the archive to be created. As such the will be created.

Now, delete the original two text files for the next section(unzip).

rm *.txt


Unzip the .zip file using unzip command.

unzip helloworld
# Archive:
# extracting: hello.txt
# extracting: world.txt

# helloworld.txt hello.txt world.txt

Extracting the, the two original files will appear.

gzip Exercise

First of all, create a text file which has some text.

echo "Hello" >> hello.txt

Compress the file using gzip.

gzip hello.txt

The hello.txt.gz will be created. To check the file type, run the following command.

file hello.txt.gz
# hello.txt.gz: gzip compressed data, was "hello.txt", last modified: Sat Mar 12 05:39:29 2022, from Unix, truncated

Next, decompress the hello.txt.gz using gzip or gunzip.

# For gzip
gzip -d hello.txt.gz

# For gunzip
gunzip hello.txt.gz

After that, the original text file named hello.txt will appear.

file hello.txt
# hello.txt: ASCII text

bzip2 Exercise

As with previous exercises, create a text file.

echo "Hello" >> hello.txt

Compress the file using bzip2.

bzip2 hello.txt

The hello.txt.bz2 will be created.

file hello.txt.bz2
# hello.txt.bz2: bzip2 compressed data, block size = 900k

Decompress the hello.txt.bz2 using bzip2 or bunzip2.

# For bzip2
bzip2 -d hello.txt.bz2

# For bunzip2
bunzip2 hello.txt.bz2

The original hello.txt file will return in front of us.

file hello.txt
# hello.txt: ASCII text