How to Do File Compression in Linux - CloudMinister

Wondering What is File compression in Linux? this is the article for you…

Here you will learn about file compression and the full overview of types of file compression

File compression used to contain two or more files & directories in smaller size rather than the original. Compression means reduce the size of the file.

In linux have many types of file compression
1. gzip

2. bzip2
3. xz
4. zip

 

# du -sh filename/or/path (This command used to check the size of the file)

# yes “string” > file_name (This command used to copy the string into the file)

Alert:- immediately press (ctrl + c) after using the yes command. If you not press (ctrl +c) immediately file size increase every second.

example:- 

  1. gzip:-

gzip is used to compress files but gzip not reduce more size as compared to others. gzip extension is .gz.   

# gzip file_name (This command used to compress the file in .gz extension)


Uncompress gzip command

# gunzip  file_name.gz (This command used to unzip .gz file)

Example:- 


Create compress and tar file simultaneously 

#tar cvzf destination\path/file_name.tar.gz /source/path/ (This command used to compress in gz format and convert in tar file)

Example:- 


Uncompress and untar  file simultaneously

# tar xvzf file_name.tar.gz (This command used for uncompressing and untar file those extensions have .tar.gz or .tgz)

Example:- 

  1. bzip2

bzip2 is used to compress file bzip2 to reduce the size as compared to gzip. Bzip2 takes time to compress. Bzip2 extension is .bz2

# bzip2 file_name (This command used to compress the file with extension .bz2)

Uncompress bzip2 command

# bunzip2  file_name.bz2 (This command used to unzip .bz2      file)

Example:- 


Create compress and tar file simultaneously using bzip2 

#tar -cvjf destination\path/file_name.tar.bz2 /source/path/ (This command used to compress in bzip2 format and convert in tar file)

Example:- 

Uncompress and untar  file simultaneously using bzip2

# tar -xvf file_name.tar.gz (This command use for uncompress and untar file those extensions have .tar.bz2 or .tbz or .tb2)

Example:- 

  1. Xz

xz is used to compress file xz reduce more size as compared to other compression methods.xz extension is .xz.

# xz file_name (This command used to compress file with extension .xz)

Example:- 

Uncompress xz command

# unxz  file_name.bz2 (This command used to unzip .xz  file)

Example:- 


  1. Zip

Zip is used to compress the file or folder so that reduces the size of that package.
zip is available in Linux, Windows, and Unix. Much other software is available that works the same as zip. example:- WinRAR, 7zip etc.

 

Note: before using the zip command first install the zip package.

In centos installation :- # yum install zip -y

In ubuntu installation :- # apt-get install zip -y

Zip current all directory and file

# zip file_name.zip * (This command used to zip all current working directory and file) 

Recursively zip all directory file

# zip -r file_name.zip directory\path (This command used to recursively zip all file in a directory)

Exclude some file in zip

# zip -r file_name.zip directory\path –exclude “*.extension”

(This command use to zip all file in the directory but exclude some files using exclude option)

Extract zip file 

# unzip file_name.zip (This command used to extract the zip file)

Extract zip file into the directory 

# unzip file_name.zip -d directory\path (This command use to extract the zip file into a directory)

Learn how to scale, manage, and optimize your applications with a SLB. Read our solution brief "Get More from Your Enterprise Network".

DOWNLOAD SOLUTION BRIEF

Get started with CloudMinister Today