about gzip y bzip2

In the adjacent article we will take a wait 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 the states most some of these programs like rare y zip in this blog. In this tutorial, we are going to accept a look at two of them only, such as gzip and bzip2. Every bit I said, allow's see how to apply them to compress and decompress files with some examples in Ubuntu.

Table of Contents

  • 1 Compress and decompress files using gzip and bzip2
    • 1.1 The gzip program
      • 1.1.1 Shrink files
      • 1.i.2 Shrink and decompress keeping the original file
      • 1.1.three Unzip files
      • 1.1.4 View the contents of compressed files without decompressing them
      • 1.1.5 Compress the file with gzip specifying the compression level
      • 1.1.6 Concatenate multiple compressed files
    • ane.2 The bzip2 programme
      • 1.2.1 Shrink files
      • 1.2.2 Compress the files without deleting the original file
      • 1.two.3 Unzip files
      • 1.2.iv View the contents of compressed files without decompressing them

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 called ubunlog.txt, replacing it with a compressed version, nosotros volition execute in the terminal (Ctrl + Alt + T):

comprimir y descomprimir archivos con gzip

gzip ubunlog.txt

Gzip will replace the original file called ubunlog.txt for a compressed version called ubunlog.txt.gz.

The gzip control 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.

gzip comprimir salida ls

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 plan will compress the given file, replacing it with a compressed version. However, we tin can keep the original file and write the outcome to standard output. For case, the following command compresses ubunlog.txt and writes the event to output.txt.gz.

gzip comprimir convervando archivo gzip

gzip -c ubunlog.txt > salida.txt.gz

In the aforementioned mode, we tin can unzip a compressed file specifying the name of the output file:

gzip comprimir conservando archivo

gzip -c -d salida.txt.gz > ubunlog1.txt

The in a higher place command unzips the output.txt.gz file and writes the output to the ubunlog1.txt file. In the ii previous cases, the original file volition not be deleted.

  • Unzip files

To unzip the file ubunlog.txt.gz, replacing it with the original uncompressed version, we volition utilize the following control in the terminal (Ctrl + Alt + T):

gzip descomprimir archivo

gzip -d ubunlog.txt.gz

We tin also apply gunzip to unzip the files.

gunzip descomprimir archivo

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, nosotros will use the -c option equally information technology's shown in the following:

gunzip -c ver contenido archivos comprimidos

gunzip -c ubunlog1.txt.gz

Nosotros can also use the zcat utility for the same purpose, similar below:

zcat ver contenido archivo comprimido

zcat ubunlog.txt.gz

We will be able pipe the output using the "less" command to view the output page by folio as shown beneath:

gunzip -c ubunlog.txt.gz | less

The less command tin can also be used with zcat:

zcat ubunlog.txt.gz | less

Nosotros will also take the option to use the zless program. This performs the same function as the previous pipes:

zless ubunlog.txt.gz

We tin leave 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 equally below.

1 - Faster (peor)
ix - Slower (BEST)
vi - Default level

To compress the file called ubunlog.txt, replacing it with a compressed version with the best compression level, we will employ:

gzip -ix ubunlog.txt
  • Concatenate multiple compressed files

Another possibility that gzip offers us is that of concatenate multiple compressed files into one. We tin do this in the following way:

gzip -c ubunlog1.txt > salida.txt.gz  gzip -c ubunlog2.txt >> salida.txt.gz

The higher up two commands will compress ubunlog1.txt and ubunlog2.txt and save them to 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 post-obit commands:

gunzip -c salida.txt.gz  gunzip -c salida.txt  zcat salida.txt.gz  zcat salida.txt

For more details about gzip, encounter the man pages:

man gzip

man gzip

The bzip2 program

El bzip2 it is very similar to the gzip program. The primary deviation is that it uses a different compression algorithm called Burrows-Wheeler block nomenclature 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 every bit gzip. We will simply have to replace gzip in the to a higher place examples with bzip2, gunzip with bunzip2, zcat with bzcat and so on.

  • Shrink files

To shrink a file using bzip2, replacing information technology with a compressed version, nosotros will execute:

bzip2 comprimir archivo

bzip2 ubunlog.txt
  • Shrink the files without deleting the original file

If we don't want to supplant the original file, we will use the -c selection and we will write the result to a new file.

bzip2 comprimir conservando archivo

bzip2 -c ubunlog.txt > salida.txt.bz2
  • Unzip files

For, unzip a file compressed nosotros will use one of the post-obit two possibilities:

bzip2 -d ubunlog.txt.bz2  bunzip2 ubunlog.txt.bz2
  • View the contents of compressed files without decompressing them

To encounter the content of a compressed file without decompressing it, we will only take to use any of the options:

bunzip2 -c ubunlog.txt.bz2  bzcat ubunlog.txt.bz2

For more details, we can consult the human being pages:

man bzip2

homo bzip2