linux-compress-archive-1
Linux

Linux File compression and archiving using bzip2, gzip and zip part1

0 534

Compress and Archive Part 1

Hello and welcome to our another new tutorial. In this tutorial we will discuss about compressing and archiving Linux file. Linux users will never have enough disk space. After some time disk space will be exhausted. Its not good idea to keep adding new disk space. Good system administer will do house keeping jobs which includes managing disk space. One of task in house keeping is to archive and compress old files to reduce disk space usage.




It is useful to store a group of files in one file for easy backup, for transfer to another directory, or for transfer to another computer. It is also useful to compress large files; compressed files take up less disk space and download faster via the Internet.[1]

File Compression

A compressed file is a collection of files and directories that are stored in one file and stored in a way that uses less disk space than all the individual files and directories combined. If disk space is a concern, compress rarely-used files, or place all such files in a single archive file and compress it.[1]

File Archiving

An archive file is a collection of files and directories stored in one file. The archive file is not compressed — it uses the same amount of disk space as all the individual files and directories combined [1]

Note: An archive file is not compressed but compressed file can be an archive file.

Compressing Files at the Shell

Linux provides the bzip2, gzip and zip for compression from a shell prompt.

bzip2

bzip2 is technique to compress a file and replaced into bzip2 file by using command bzip2 filename.bzip2 is also recommend to use for compression because its widely available in other UNIX-like operating systems

You can also  compress multiple file by using command bzip2 filename1 filename2 (or you can also define the path of file which you are going to compress). The file format/extension is .bz2.

You can use bzip2 to compress multiple files and directories at the same time by listing them with a space between each one

bzip2 filename.bz2 filename1 filename2 filename3 /path-to-directory
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file  Pictures  Public  Templates  upload.txt  Videos
[fahmed@faraz ~]$ bzip2 newstring.file
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file.bz2  Pictures  Public  Templates  upload.txt  Videos
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file.bz2  Pictures  Public  Templates  test1  test2file  upload.txt  Videos
[fahmed@faraz ~]$ bzip2 test1 test2file
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file.bz2  Pictures  Public  Templates  test1.bz2  test2file.bz2  upload.txt  Videos
[fahmed@faraz ~]$

To  view the contents of compressed file by using command bzcat filename.bz2.

[fahmed@faraz ~]$ bzcat newstring.file.bz2
THIS IS NEW FILE FOR CREATE AND SAVE STRING IN A FILE
APPEND IN STRING FILE
[fahmed@faraz ~]$

To uncompress compressed files we can use command bunzip2 filename.bz2. For multiple you need defines the names of file or path if file is in different directory/location.

 




 

[fahmed@faraz ~]$ bunzip2 newstring.file.bz2 test1.bz2 test2file.bz2
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file  Pictures  Public  Templates  test1  test2file  upload.txt  Videos
[fahmed@faraz ~]$

gzip

gzip is also used to compress a file or group of files.This compressed file will always have extension .gz.   Syntax of using this utility is gzip filename.

To compress multiples files into one compressed file you can use below syntax

gzip filename1 filename2.

you can compress multiple files and directory into one compressed file by using below syntax

gzip -r filename.gz file1 file2 file3 /path-to-diretcory/

[fahmed@faraz ~]$ gzip newstring.file
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file.gz  Pictures  Public  Templates  test1  test2file  upload.txt  Videos
[fahmed@faraz ~]$ gzip test1 test2file
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file.gz  Pictures  Public  Templates  test1.gz  test2file.gz  upload.txt  Videos
[fahmed@faraz ~]$

For view the contents of compress file by using command zcat filename.gz.

[fahmed@faraz ~]$ zcat newstring.file.gz
THIS IS NEW FILE FOR CREATE AND SAVE STRING IN A FILE
APPEND IN STRING FILE
[fahmed@faraz ~]$

To uncompress that files we can use command gunzip filename.gz. For multiple you need defines the names of file or path if file is in different directory/location.

[fahmed@faraz ~]$ gunzip newstring.file.gz test1.gz test2file.gz
[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file  Pictures  Public  Templates  test1  test2file  upload.txt  Videos
[fahmed@faraz ~]$

Zip

By default this utility is not installed. install zip and unzip utility do

yum install zip
yum install unzip

to zip file or directory you can use below syntax

zip filename it will create filename.zip file

zip Downloads(directory) this will create Downloads.zip in current directory

To zip all contents in Downloads directory including subdirectories you can use below command

zip -r Downloads

And to uncompress simple use

unzip Downloads.zip

To name compressed zip file to different name instead of default name  you can use below command

zip compressed.zip file1 file2

You can also add more files to existing zip file by using below command

zip compressd.zip file3 file4

[fahmed@faraz ~]$ ls
Desktop  Documents  Downloads  Faraz  Music  newstring.file  Pictures  Public  Templates  test1.gz  test2file.gz  upload.txt  Videos
[fahmed@faraz ~]$ zip allfiles.zip newstring.file
adding: newstring.file (deflated 20%)
[fahmed@faraz ~]$ ls
allfiles.zip  Desktop  Documents  Downloads  Faraz  Music  newstring.file  Pictures  Public  Templates  test1.gz  test2file.gz  upload.txt  Videos

[fahmed@faraz ~]$ zip allfiles.zip test1.gz test2file.gz
adding: test1.gz (stored 0%)
adding: test2file.gz (stored 0%)
[fahmed@faraz ~]$

In above condition you can see i have added two more files in allfiles.zip.

To unzip these files by using command unzip filename.zip. this will unzip to current location

To unzip to different location use below command

unzip compressed.zip -d /path-to-directory

[fahmed@faraz ~]$ unzip allfiles.zip
Archive:  allfiles.zip
replace newstring.file? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: newstring.file
replace test1.gz? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: 123
extracting: 123
replace test2file.gz? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: test2file.gz
[fahmed@faraz ~]

Files are already present in existing folder that’s why its prompting the message, to replace file select the option as you can see I have chosen two options in above example.

This is end of part 1 tutorial . We have learnt how to use file compression utilities bzip2, gzip and zip commands.

I hope you have enjoyed this tutorial and now you feel comfortable in using these commands, Subscribe to our news letter for more upcoming new and exciting tutorials

Thanks

Reference list

  1. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/s1-managing-compressing-archiving.html
SUBSCRIBE OUR NEWSLETTER
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a reply

You must be logged in to post a comment.