A Complete Guide to Linux Compression and Decompression Commands

Linux zip Command

zip -r myfile.zip ./*
Compress all files and folders in the current directory into myfile.zip. The -r option recursively compresses all files in subdirectories.

2. unzip
unzip -o -d /home/sunny myfile.zip
Extract myfile.zip to /home/sunny/
-o: Overwrite files without prompting;
-d: -d /home/sunny specifies the target directory /home/sunny for extraction;

3. Other
zip -d myfile.zip smart.txt
Delete smart.txt from the compressed archive
zip -m myfile.zip ./rpm_info.txt
Add rpm_info.txt to the compressed archive myfile.zip
——————————————————————————-

To use zip to compress files, type the following command at the shell prompt:

zip -r filename.zip filesdir
 

In this example, filename.zip represents the file you are creating, and filesdir represents the directory you want to place in the new zip file. The -r option specifies that you want to recursively include all files contained within the filesdir directory.

To extract the contents of a zip file, type the following command:

unzip filename.zip
 

You can use the zip command to process multiple files and directories at once by listing them one by one, separated by spaces:

zip -r filename.zip file1 file2 file3 /usr/work/school
 

The command above compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists), then places them into the filename.zip file.

 

tar Command Detailed Guide

  -c: Create a compressed archive

  -x: Extract files

  -t: View contents

  -r: Append files to the end of an archive

  -u: Update files in the original archive

  These five are independent commands; you must use one of them for compression or extraction. They can be combined with other options but only one can be used at a time. The following parameters are optional depending on your needs during compression or extraction.

  -c: Create a compressed archive

  -x: Extract files

  -t: View contents

  -r: Append files to the end of an archive

  -u: Update files in the original archive

  The following parameter -f is mandatory

  -f: Use archive file. Remember, this parameter must be the last one, followed only by the archive name.

  # tar -cf all.tar *.jpg

  This command packs all .jpg files into a package named all.tar. -c means create a new package, and -f specifies the package filename.

  # tar -rf all.tar *.gif

  This command adds all .gif files to the all.tar package. -r means append files.

  # tar -uf all.tar logo.gif

  This command updates the logo.gif file in the original tar package all.tar. -u means update files.

  # tar -tf all.tar

  This command lists all files in the all.tar package. -t means list files.

  # tar -xf all.tar

  This command extracts all files from the all.tar package. -x means extract.

  Compression

  tar –cvf jpg.tar *.jpg //Pack all jpg files in the directory into jpg.tar

  tar –czf jpg.tar.gz *.jpg //Pack all jpg files into jpg.tar, then compress with gzip to produce a gzip-compressed package named jpg.tar.gz

  tar –cjf jpg.tar.bz2 *.jpg //Pack all jpg files into jpg.tar, then compress with bzip2 to produce a bzip2-compressed package named jpg.tar.bz2

  tar –cZf jpg.tar.Z *.jpg //Pack all jpg files into jpg.tar, then compress with compress to produce a compress-compressed package named jpg.tar.Z

  rar a jpg.rar *.jpg //Compression in rar format, requires downloading rar for linux first

  zip jpg.zip *.jpg //Compression in zip format, requires downloading zip for linux first

  Extraction

  tar –xvf file.tar //Extract tar package

  tar -xzvf file.tar.gz //Extract tar.gz

  tar -xjvf file.tar.bz2 //Extract tar.bz2

  tar –xZvf file.tar.Z //Extract tar.Z

  unrar e file.rar //Extract rar

  unzip file.zip //Extract zip

  Summary

  1. *.tar Extract with tar –xvf

  2. *.gz Extract with gzip -d or gunzip

  3. *.tar.gz and *.tgz Extract with tar –xzf

  4. *.bz2 Extract with bzip2 -d or bunzip2

  5. *.tar.bz2 Extract with tar –xjf

  6. *.Z Extract with uncompress

  7. *.tar.Z Extract with tar –xZf

  8. *.rar Extract with unrar e

  9. *.zip Extract with unzip

  Detailed Guide to tar Command in Linux (Reprinted Material)

  Thursday, April 17, 2008 15:37

  tar Command

  tar can create archives for files and directories. Using tar, users can create an archive (backup file) for a specific set of files, modify files within an archive, or add new files to an archive. tar was originally used to create archives on magnetic tape, but now users can create archives on any device, such as floppy disks. With the tar command, you can pack a large number of files and directories into a single file, which is very useful for backing up files or combining several files into one file for easy network transmission. The tar on Linux is the GNU version.

  Syntax: tar [primary-options+auxiliary-options] file-or-directory

  When using this command, primary options are mandatory and tell tar what action to perform. Auxiliary options are supplementary and optional.

  Primary Options:

  c Create a new archive file. Choose this option if you want to back up a directory or some files.

  r Append files to be archived to the end of the archive. For example, if you have already created a backup file and realize you forgot to back up a directory or some files, you can use this option to append the forgotten items to the backup file.

  t List the contents of an archive file to see which files have been backed up.

  u Update files. That is, replace the original backed-up files with newer ones. If a file to be updated is not found in the backup file, it is appended to the end of the backup file.

  x Extract files from an archive file.

  Auxiliary Options:

  b This option is designed for tape drives. It is followed by a number indicating the block size, with the system default being 20 (20*512 bytes).

  f Use archive file or device. This option is typically mandatory.

  k Preserve existing files. For example, when restoring files, if an identical file is encountered, it will not be overwritten.

  m When restoring files, set the modification time of all files to the current time.

  M Create a multi-volume archive for storage across several disks.

  v Verbosely report file information processed by tar. Without this option, tar does not report file information.

  w Require confirmation for every step.

  z Use gzip to compress/decompress files. Adding this option allows the archive file to be compressed.<<>>
But you must also use this option when decompressing during restoration.

  Analyzing Compressed Files Under Linux

  For those new to Linux, the sheer variety of file names can be overwhelming. Take compressed files, for example. In Windows, the most common compressed formats are basically two: .zip and .rar. Linux, however, is different. It has a multitude of compressed file extensions like .gz, .tar.gz, .tgz, .bz2, .Z, .tar, and more. Additionally, Windows .zip and .rar files can also be used under Linux, though very few people actually use them there. This article summarizes these common compressed file types, hoping you won’t be confused the next time you encounter them.

  Before summarizing the specific types, two concepts must be clarified: archiving and compression. Archiving means combining a bunch of files or directories into a single file, while compression uses algorithms to make a large file smaller. Why distinguish between these two? It stems from the fact that many compression programs in Linux can only compress a single file. So, to compress many files, you must first use another tool to archive them into one package, and then use the original compression program to compress it.

  The most common archiving program in Linux is tar. Packages created by tar are often called tar packages, and their filenames usually end with .tar. Once a tar package is generated, other programs can be used to compress it. So, let’s first cover the basic usage of the tar command:

  The tar command has many options (you can check them with ‘man tar’), but only a few are commonly used. Let’s illustrate with examples:

  # tar -cf all.tar *.jpg

  This command packages all .jpg files into a package named all.tar. -c means to create a new archive, and -f specifies the filename of the archive.

  # tar -rf all.tar *.gif

  This command appends all .gif files to the all.tar package. -r means to append files.

  # tar -uf all.tar logo.gif

  This command updates the logo.gif file in the original tar package all.tar. -u means to update files.

  # tar -tf all.tar

  This command lists all files in the all.tar package. -t means to list files.

  # tar -xf all.tar

  This command extracts all files from the all.tar package. -x means to extract.

  The above covers the most basic usage of tar. To make it convenient for users to compress or decompress files while archiving or extracting, tar offers a special feature: it can invoke other compression programs, such as gzip or bzip2, during the archiving or extraction process.

  1) tar invoking gzip

  gzip is a compression program developed by the GNU project. Files ending in .gz are the result of gzip compression. The corresponding decompression program for gzip is gunzip. tar uses the -z parameter to invoke gzip. Let’s illustrate with examples:

  # tar -czf all.tar.gz *.jpg

  This command packages all .jpg files into a tar archive and compresses it with gzip, generating a gzip-compressed package named all.tar.gz.

  # tar -xzf all.tar.gz

  This command extracts the package created above.

  2) tar invoking bzip2

  bzip2 is a compression program with a stronger compression capability. Files ending in .bz2 are the result of bzip2 compression. The corresponding decompression program for bzip2 is bunzip2. tar uses the -j parameter to invoke bzip2. Let’s illustrate with examples:

  # tar -cjf all.tar.bz2 *.jpg

  This command packages all .jpg files into a tar archive and compresses it with bzip2, generating a bzip2-compressed package named all.tar.bz2.

  # tar -xjf all.tar.bz2

  This command extracts the package created above.

  3) tar invoking compress

  compress is also a compression program, but it seems fewer people use compress compared to gzip and bzip2. Files ending in .Z are the result of compress compression. The corresponding decompression program for compress is uncompress. tar uses the -Z parameter to invoke compress. Let’s illustrate with examples:

  # tar -cZf all.tar.Z *.jpg

  This command packages all .jpg files into a tar archive and compresses it with compress, generating a compress-compressed package named all.tar.Z.

  # tar -xZf all.tar.Z

  This command extracts the package created above.

  With the knowledge above, you should be able to decompress various compressed files. Below is a summary for the tar series of compressed files:

  1) For files ending in .tar

  tar -xf all.tar

  2) For files ending in .gz

  gzip -d all.gz

  gunzip all.gz

  3) For files ending in .tgz or .tar.gz

  tar -xzf all.tar.gz

  tar -xzf all.tgz

  4) For files ending in .bz2

  bzip2 -d all.bz2

  bunzip2 all.bz2

  5) For files ending in .tar.bz2

  tar -xjf all.tar.bz2

  6) For files ending in .Z

  uncompress all.Z

  7) For files ending in .tar.Z

  tar -xZf all.tar.z

  Additionally, for the common Windows compressed files .zip and .rar, Linux also has corresponding methods to decompress them:

  1) For .zip

  Linux provides the zip and unzip programs. zip is the compression program, and unzip is the decompression program. They have many parameter options, but we’ll just give a brief introduction here, using examples to illustrate their usage:

  # zip all.zip *.jpg

  This command compresses all .jpg files into a zip package.

  # unzip all.zip

  This command extracts all files from all.zip.

  2) For .rar

  To handle .rar files in Linux, you need to install RAR for Linux. It can be downloaded from the internet, but remember, RAR for Linux is not free. Then install it:

  # tar -xzpvf rarlinux-3.2.0.tar.gz

  # cd rar

  # make

  This completes the installation. After installation, you’ll have the rar and unrar programs. rar is the compression program, and unrar is the decompression program. Their parameter options are numerous, so we’ll just give a simple introduction here, illustrating their usage with examples:

  # rar a all *.jpg

  This command compresses all .jpg files into a rar package named all.rar. The program will automatically append the .rar extension to the package name.

  # unrar e all.rar

  This command extracts all files from all.rar.

  Up to this point, we have introduced the Linux programs tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, and unrar. You should now be able to use them to decompress these 10 compressed file types: .tar, .gz, .tar.gz, .tgz, .bz2, .tar.bz2, .Z, .tar.Z, .zip, and .rar. You should no longer be troubled by not knowing how to extract a downloaded software package in Linux. Moreover, the above methods are also largely effective for Unix.

  This article introduced Linux compression programs like tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, and unrar, and how to use them to operate on these

Leave a Comment

Your email address will not be published.