In the next article we will take a look at how zip and unzip files using gzip and bzip2. Compression is very useful when backing up important files or sending large files over the Internet. Today there are many programs to compress and decompress files in GNU / Linux.
A colleague already told us about some of these programs like rare y zip in this blog. In this tutorial, we are going to take a look at two of them only, such as gzip and bzip2. As I said, let's see how to use them to compress and decompress files with some examples in Ubuntu.
Compress and decompress files using gzip and bzip2
The gzip program
Gzip is a utility to compress and decompress files using the Lempel-Ziv (LZ77) encoding algorithm.
-
Compress files
To compress a file named ubunlog.txt, replacing it with a compressed version, we will execute in the terminal (Ctrl + Alt + T):
gzip ubunlog.txt
Gzip will replace the original file called ubunlog.txt by a compressed version called ubunlog.txt.gz.
The gzip command can also be used in other ways. A good example is that we can create a compressed version of the output of a specific command. Look at the following command.
ls -l ../../Descargas / | gzip > ubunlog.txt.gz
The above command creates a compressed version of the list of files in the Downloads folder.
-
Compress and decompress keeping the original file
By default, the gzip program will compress the given file, replacing it with a compressed version. However, we can keep the original file and write the result to standard output. For example, the following command, compress ubunlog.txt and write the result to output.txt.gz.
gzip -c ubunlog.txt > salida.txt.gz
In the same way, we can unzip a compressed file specifying the name of the output file:
gzip -c -d salida.txt.gz > ubunlog1.txt
The above command unzips the output.txt.gz file and writes the result to the file ubunlog1.txt. In the two previous cases, the original file will not be deleted.
-
Unzip files
To unzip the file ubunlog.txt.gz, replacing it with the original uncompressed version, we will use the following command in the terminal (Ctrl + Alt + T):
gzip -d ubunlog.txt.gz
We can also use gunzip to unzip the files.
gunzip ubunlog.txt.gz
-
View the contents of compressed files without decompressing them
To view the contents of the compressed file without decompressing it using gzip, we will use the -c option as it's shown in the following:
gunzip -c ubunlog1.txt.gz
We can also use the zcat utility for the same purpose, like below:
zcat ubunlog.txt.gz
We will be able pipe the output using the "less" command to view the output page by page as shown below:
gunzip -c ubunlog.txt.gz | less
The less command can also be used with zcat:
zcat ubunlog.txt.gz | less
We will also have the option to use the zless program. This performs the same function as the previous pipes:
zless ubunlog.txt.gz
We can exit paging by pressing the q key.
-
Compress the file with gzip specifying the compression level
Another advantage to keep in mind of gzip is that supports compression level. Supports 3 levels of compression as below.
1 - Faster (peor)
9 - Slower (BEST)
6 - Default level
To compress the file named ubunlog.txt, replacing it with a compressed version with the best compression level, we will use:
gzip -9 ubunlog.txt
-
Concatenate multiple compressed files
Another possibility that gzip offers us is that of concatenate multiple compressed files into one. We can do this in the following way:
gzip -c ubunlog1.txt > salida.txt.gz gzip -c ubunlog2.txt >> salida.txt.gz
The above two commands will compress ubunlog1.txt and ubunlog2.txt and save them in a single file called output.txt.gz.
We can view the contents of the files (ubunlog1 .txt and ubunlog1.txt) without extracting them using any of the following commands:
gunzip -c salida.txt.gz gunzip -c salida.txt zcat salida.txt.gz zcat salida.txt
For more details about gzip, see the man pages:
man gzip
The bzip2 program
El bzip2 it is very similar to the gzip program. The main difference is that it uses a different compression algorithm called Burrows-Wheeler block classification text compression algorithm and Huffman encoding. Files compressed with bzip2 will end with the extension .bz2.
Like I said, using bzip2 is pretty much the same as gzip. We will simply have to replace gzip in the above examples with bzip2, gunzip with bunzip2, zcat with bzcat and so on.
-
Compress files
To compress a file using bzip2, replacing it with a compressed version, we will execute:
bzip2 ubunlog.txt
-
Compress the files without deleting the original file
If we don't want to replace the original file, we will use the -c option and we will write the result to a new file.
bzip2 -c ubunlog.txt > salida.txt.bz2
-
Unzip files
For unzip a file compressed we will use one of the following two possibilities:
bzip2 -d ubunlog.txt.bz2 bunzip2 ubunlog.txt.bz2
-
View the contents of compressed files without decompressing them
To see the content of a compressed file without decompressing it, we will only have to use any of the options:
bunzip2 -c ubunlog.txt.bz2 bzcat ubunlog.txt.bz2
For more details, we can consult the man pages:
man bzip2