zip, gzip, bzip2 are Linux commands commonly used when compressing, decompressing, or archiving files.
- zip: compress files or directories and archive them in
.zip
format. And extract.zip
file. - gzip: compress a file as
.gz
format. And decompress.gz
file. - bzip2: compress a file as
.bz2
format. And decompress.bz2
file.
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.
1 | echo "Hello" >> hello.txt |
Zip the two files.
1 | zip helloworld hello.txt world.txt |
helloworld
is the name of the archive to be created. As such the helloworld.zip
will be created.
Now, delete the original two text files for the next section(unzip).
1 | rm *.txt |
Unzip the .zip file using unzip command.
1 | unzip helloworld |
Extracting the helloworld.zip
, the two original files will appear.
gzip Exercise
First of all, create a text file which has some text.
1 | echo "Hello" >> hello.txt |
Compress the file using gzip
.
1 | gzip hello.txt |
The hello.txt.gz
will be created. To check the file type, run the following command.
1 | file hello.txt.gz |
Next, decompress the hello.txt.gz
using gzip
or gunzip
.
1 | # For gzip |
After that, the original text file named hello.txt
will appear.
1 | file hello.txt |
bzip2 Exercise
As with previous exercises, create a text file.
1 | echo "Hello" >> hello.txt |
Compress the file using bzip2
.
1 | bzip2 hello.txt |
The hello.txt.bz2
will be created.
1 | file hello.txt.bz2 |
Decompress the hello.txt.bz2
using bzip2
or bunzip2
.
1 | # For bzip2 |
The original hello.txt
file will return in front of us.
1 | file hello.txt |