file compression on Unix and Linux

1. gzip Compression

​ Typically, gzip files are sotred with a .gz extension.

1.1. Compress files with gzip :

gzip sourcefile

​ This wil compress the file and change the name to sourcefile.gz.

1.2. Recursively compress an entire directory

gzip -r directory1

1.3. list info of the compressed file

gzip -l test.gz

1.4. Adjust the compression optimization

​ From -1(–fast) to -9(–best):

gzip -9 sourcefile

1.5. Decompress a .gz file

gzip -d test.gz

​ or

gunzip test.gz

2. bzip2 Compression

​ Files compressed with bzip2 are generally given a .bz2 file extension.

2.1. Compress files with bz2

bzip2 testfile

​ This will compress the file and give it the name testfile.bz2.

2.2. Numbered flags (different from that of gzip)

​ The number represents the block size that utility manages to implement its compression.

bzip2 -9 testfile

​ The default behavior is the -9 flag.

2.3. Decompress a .bz2 file

bzip2 -d testfile.bz2

3. xz Compression

3.1. Compress files with xz

xz testfile

​ This will process the file and produce a file called file.xz.

3.2. List statistics about the compression of the file

xz -l testfile.xz

3.3. Decompress a .xz file

xz -d testfile.xz

4. Using tar archiving with compression

4.1. Using tar with gzip

4.1.1. compress

tar czvf test.tar.gz directory1

4.1.2. peek inside

tar tzvf test.tar.gz

4.1.3. decompress

tar xzvf test.tar.gz

4.2. Using tar with bzip2

​ To use archiving with bzip2, you can replace the -z flag, which is gzip-specific, with the -j flag.

4.2.1. compress

tar cjvf test.tar.bz2 directory1

4.2.2. peek inside

tar tjvf test.tar.bz2

4.2.3. decompress

tar xjvf teat.tar.bz2

4.3. Using tar with xz

​ Any remotely recent versions of tar have added similar functionality for xz compression. These follow the exact same format using the -J flag.

4.3.1. compress

tar cJvf test.tar.xz directory1

4.3.2. peek inside

tar tJvf test.tar.xz

4.3.3. decompress

tar xJvf test.tar.xz
Written on October 24, 2016