Thursday, 24 August 2017

How to compress and decompress files in bash

There are available more commands to compress and decompress file in bash. Some of them are: 
  • tar 
  • zip / unzip
  • rar / unrar
  • gzip
  • bzip2

Tar and untar 

To create a tar.gz from bash shell you can use the following command:
$ tar -zcvf myarchive.tar.gz mydirectory
Instead to extract the files from an archive:
$ tar -xvf myarchive.tar.gz

Zip and unzip 

Create an archive form a text file:
$ zip myarchive.zip myfile.txt
Create an archive form a directory:
$ zip -r myarchive.zip mydirectory
Create an archive with the subdirectories and files into a directory (without it):
$ zip -r -D myarchive.zip mydirectory
Extraction of files and directories from an archive:
$ unzip myarchive.zip
Extraction and overwrite the files and directories from an archive:
$ unzip -o myarchive.zip
Extraction the files and directories from an archive to a target directory
$ unzip myarchive.zip -d mydirectory

Rar and unrar

Create a new rar archive myarchive.rar containing myfile.txt
$ rar a myarchive.rar myfile.txt
Create a new rar archive myarchive.rar containing a directory
$ rar a myarchive.rar mydirectory/
Create a new rar archive that splits the file/files into multiple parts of equal size (10 MB)
$ rar a -v10M -R myarchive.rar mydirectory/
Extraction the files and directories from an archive:
$ unrar e myarchive.rar
List the content of a rar file without uncompressing it
$ unrar l myarchive.rar

Gzip 

The command `gzip' is designed as a complement to `tar', not as a replacement. Instead the command`gunzip' can currently decompress files created by `gzip', `zip', `compress' or `pack'. The detection of the input format is automatic. 
Create an archive form a text file:
$ gzip myfile.txt
Decompress the archive created in previous step:
$ gzip myfile.gz

Bzip2

Bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. The command-line options are deliberately very similar to those of GNU gzip, but they are not identical. 
Create an archive form a text file:
$ bzip2 myfile.txt
Decompress the archive created in previous step:
$ bunzip2 myfile.txt.bz2

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...