Comprimindo dados no Linux (tar, bzip, gzip, zip, xz)

Categoria: Linux Ubuntu
Publicado em 08 de Setembro de 2014

gzip

gzip is the most oftenly used Linux compression utility. It compresses very well and is very fast. The following table provides some usage examples:

gzip * Compresses all files in the current directory; each file is compressed and renamed with a .gz extension.
gzip -r projectX Compresses all files in the projectX directory along with all files in all of the directories under projectX.
gunzip foo De-compresses foo found in the file foo.gz. Under the hood, gunzip command is actually the same as gzip –d.

bzip2

bzip2 has syntax that is similar to gzip but it uses a different compression algorithm and produces significantly smaller files, at the price of taking a longer time to do its work. Thus, It is more likely to be used to compress larger files.

bzip2 * Compress all of the files in the current directory and replaces each file with a file renamed with a .bz2 extension.
bunzip2 *.bz2 Decompress all of the files with an extension of .bz2 in the current directory. Under the hood, bunzip2 is the same as calling bzip2 -d.

xz

xz is the most space efficient compression utility used in Linux and is now used by www.kernel.org to store archives of the Linux kernel. Once again it trades a slower compression speed for an even higher compression ratio.

$ xz * Compress all of the files in the current directory and replace each file with one with a .xz extension.
xz foo Compress the file foo into foo.xz using the default compression level (-6), and remove foo if compression succeeds.
xz -dk bar.xz Decompress bar.xz into  bar and don't remove bar.xz even if decompression is successful.
xz -dcf a.txt b.txt.xz > abcd.txt Decompress a mix of compressed and uncompressed files to standard output, using a single command.
$ xz -d *.xz Decompress the files compressed using xz.

zip

The zip program is not often used to compress files in Linux, but is often required to examine and decompress archives from other operating systems. It is only used in Linux when you get a zipped file from a Windows user. It is a legacy program.

zip backup * Compresses all files in the current directory and places them in the file backup.zip.
zip -r backup.zip ~ Archives your login directory (~) and all files and directories under it in the file backup.zip.
unzip backup.zip Extracts all files in the file backup.zip and places them in the current directory.

tar

Historically, tar stood for "tape archive" and was used to archive files to a magnetic tape. It allows you to create or extract files from an archive file, often called a tarball. At the same time you can optionally compress while creating the archive, and decompress while extracting its contents.

$ tar xvf mydir.tar Extract all the files in mydir.tar into the mydir directory
$ tar zcvf mydir.tar.gz mydir Create the archive and compress with gzip
$ tar jcvf mydir.tar.bz2 mydir Create the archive and compress with bz2
$ tar Jcvf mydir.tar.xz mydir Create the archive and compress with xz
$ tar xvf mydir.tar.gz Extract all the files in mydir.tar.gz into the mydir directory. Note you do not have to tell tar it is in gzip format.

You can separate out the archiving and compression stages, as in:

$ tar mydir.tar mydir ; gzip mydir.tar
$ gunzip mydir.tar.gz ; tar xvf mydir.tar

but this is slower and wastes space by creating an unneeded intermediary .tar file.

 

Copyright © Fernando Hidemi Uchiyama 2010 - Todos os direitos reservados