Tuesday, April 9, 2019

UNIX gzip command with examples

Create a gz archive

To create a gzip archive simply specify the name of the file to compress. gzip will create an archive with the name of the file. One note to remember is that gzip will delete the input file being compressed. So you need to specify the ‘-k’ option which tells gzip to keep the input file.

gzip filename.ext
sam@asus:~/unix% gzip -k 5MB.zip
sam@asus:~/unix% ls -l 5MB.zip.gz 
-rw-rw-r-- 1 sam sam 5243706 Jun  2  2008 5MB.zip.gz
sam@asus:~/unix% 

You can set the compression level with gzip by specifying a range of 1 through 9. The ‘1’ represents the fastest compression but with a loss in compression size. The faster the compression the bigger the file. The ‘9’ represents the smallest compression ratio but also takes the longest. The default compression setting to gzip is ‘6’.

gzip -[1-9] filename.ext
sam@asus:~/unix% gzip -9k 5MB.zip
sam@asus:~/unix% ls -l 5MB.zip.gz 
-rw-rw-r-- 1 sam sam 5243706 Jun  2  2008 5MB.zip.gz
sam@asus:~/unix% 

Compress all files within a directory

With gzip you have the ability to compress all files within a directory use specify. Doing so will delete the original input files unless you also give the ‘-k’ option.

sam@asus:~/unix% ls docs/
file1.txt  file.txt  script.sh
sam@asus:~/unix% gzip -rk docs/
sam@asus:~/unix% ls docs/
file1.txt  file1.txt.gz  file.txt  file.txt.gz  script.sh  script.sh.gz
sam@asus:~/unix% 

Decompress a gz archive

To decompress or extract a gz file you would use the ‘-d’ option. gzip will decompress the contents to the current directory.

gzip -d file.gz
sam@asus:~/unix% gzip -d 5MB.zip.gz 
gzip: 5MB.zip already exists; do you wish to overwrite (y or n)? y
sam@asus:~/unix% 

gzip will alert you if the file already exists in the current directory you are decompressing to. In order to force an overwrite of the file, use the ‘-d’ option to tell gzip to force overwrite.

sam@asus:~/unix% gzip -df 5MB.zip.gz 
sam@asus:~/unix% 

Decompress all gz files in a directory

Just as with compression all files in a directory, with decompression gzip will delete the input files upon decompression unless you explicitly tell it to keep the source files. The ‘-k’ option tell gzip to keep all the source files.

gzip -rd directory/
sam@asus:~/unix% ls docs/
file1.txt.gz  file.txt.gz  script.sh.gz
sam@asus:~/unix% gzip -rd docs/
sam@asus:~/unix% ls docs/
file1.txt  file.txt  script.sh
sam@asus:~/unix% 

OR

gzip -rdk directory/
sam@asus:~/unix% ls docs/
file1.txt.gz  file.txt.gz  script.sh.gz
sam@asus:~/unix% gzip -rdk docs/
sam@asus:~/unix% ls docs/
file1.txt  file1.txt.gz  file.txt  file.txt.gz  script.sh  script.sh.gz
sam@asus:~/unix%

List compressed file stats

To view the stats of the compressed file, gzip gives you the ‘-l’ option which tells gzip to list the properties of the compressed file. It gives stats of how large the compressed file is, the uncompressed size, the compression ratio and the filename after decompression.

gzip -l file.gz
sam@asus:~/unix% gzip -l 5MB.zip.gz 
         compressed        uncompressed  ratio uncompressed_name
            5243706             5242880  -0.0% 5MB.zip
sam@asus:~/unix% 

Test a gz archive for integrity

To test the integrety of a gzip archive you can use the ‘-t’ option. On a successful test gzip with output nothing to the screen. To see more information apply the ‘v’ verbose flag to get more output.

gzip -vt file.gz
sam@asus:~/unix/docs% gzip -vt file1.txt.gz 
file1.txt.gz:  OK
sam@asus:~/unix/docs% 

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...