The gzip command

Yaser Rahmati | یاسر رحمتی

The gzip command in Linux/Unix is used to compress/decompress data.

1. Compress a file

Action:

Compressing a file

Details:

Reduce the size of the file by applying compression

Command:

[root@academy mydir]# truncate -s 20M myfile.txt
[root@academy mydir]# ls -ltrh
total 0
-rw-r--r--. 1 root root 20M Dec 22 23:00 myfile.txt
[root@academy mydir]# gzip myfile.txt
[root@academy mydir]# ls -ltrh
total 20K
-rw-r--r--. 1 root root 20K Dec 22 23:00 myfile.txt.gz
[root@academy mydir]#

2. Decompress a file

Action:

Decompressing a file

Details:

Restore the file's original form in terms of data and size

Command:

[root@academy mydir]# ls -ltrh
total 20K
-rw-r--r--. 1 root root 20K Dec 22 23:00 myfile.txt.gz
[root@academy mydir]# gzip -d myfile.txt.gz
[root@academy mydir]# ls -ltrh
total 20M
-rw-r--r--. 1 root root 20M Dec 22 23:00 myfile.txt
[root@academy mydir]#

3. Compress multiple files:

Action:

Compress multiple files

Details:

Compress multiple files into multiple archives

Command:

[root@academy mydir]# touch file1.txt file2.txt file3.txt
[root@academy mydir]# ls
file1.txt  file2.txt  file3.txt
[root@academy mydir]# gzip file1.txt file2.txt file3.txt
[root@academy mydir]# ls
file1.txt.gz  file2.txt.gz  file3.txt.gz

4. Decompress multiple files:

Action:

Decompress multiple files

Details:

Decompress multiple files from multiple archives

Command:

[root@academy mydir]# ls
file1.txt.gz  file2.txt.gz  file3.txt.gz
[root@academy mydir]# gzip -d file1.txt.gz file2.txt.gz file3.txt.gz
[root@academy mydir]# ls
file1.txt  file2.txt  file3.txt
[root@academy mydir]#

5. Compress a directory:

Action:

Compress all the files in a directory

Details:

Compress multiple files under a directory in one single archive

Command:

[root@academy tmp]# ls
ks-script-ggLlAt  mydir  yum.log
[root@academy tmp]# ls ./mydir
file1.txt  file2.txt  file3.txt
[root@academy tmp]# gzip -r mydir
[root@academy tmp]# ls
ks-script-ggLlAt  mydir  yum.log
[root@academy tmp]# ls ./mydir
file1.txt.gz  file2.txt.gz  file3.txt.gz
[root@academy tmp]#

6. Decompress a directory:

Action:

Decompress all the files in a directory

Details:

Decompress multiple files under a directory from one single archive

Command:

[root@academy tmp]# ls
ks-script-ggLlAt  mydir  yum.log
[root@academy tmp]# ls ./mydir
file1.txt.gz  file2.txt.gz  file3.txt.gz
[root@academy tmp]# gzip -dr mydir/
[root@academy tmp]# ls ./mydir
file1.txt  file2.txt  file3.txt
[root@academy tmp]#

Last updated