Sunday, October 4, 2009

Linux - Compressing Files With gzip and bzip2

Linux - Compressing Files With gzip and bzip2

Compressing files is a mean of reducing the size of a file. The most common file compression programs in *nix are gzip and bzip2.

'bzip2' usually compresses files a bit better than 'gzip' due to a different algorithm, but they are used in exactly the same manner.

Compress a File

Using gzip,

$ gzip file1.c
$ ls
a.out file1.c.gz file3 src

This compresses the file and leaves file1.c.gz in the current directory; the original file file1.c is replaced by the .gz file.

Using bzip2,

$ bzip2 file2.c

$ ls

a.out file1.c.gz file3 src

Decompress a File

Using gzip, the -d option decompresses a .gz file,

$ gzip -d file1.c.gz
$ ls
a.out file1.c file3 src

This decompresses the file and leaves file1.c in the current directory; the .gz file is removed.

Using bzip2, which also uses the -d option,

$ bzip2 -d file1.c.gz
$ ls
a.out file1.c file3 src

 

Examples

To compress every .c file in a directory,

$ cd
$ ls src
file01.c file02.c file03.c file01.o file02.o file03.o
$ cd src
$ gzip *.c
$ ls

file01.c.gz file02.c.gz file03.c.gz file01.o file02.o file03.o

 

To decompress every gzipped .c file in a directory,

$ gzip -d *.c.gz
$ ls
file01.c file02.c file03.c file01.o file02.o file03.o

 

To compress a tar file, just gzip or bzip2 it like a normal file,

$ ls src

file01.c file02.c file03.c file01.o file02.o file03.o
$ tar cf src.tar src
$ ls
a.out file1.c file3 src src.tar
$ bzip2 src.tar
$ ls
a.out file1.c file3 src src.tar.bz2

  

 

No comments:

Post a Comment