How to compress a file/directory in Linux?
In the present era computer has become necessity of human being because almost all daily life problems are being solved by computers such as health problems, education, space, automobiles etc. But your computer has limited available space in form of hard disk. In order to save your hard disk space, you should save a compress files of your data.
How to make a compress file of data?
You can make a compress file using several commands, which are as follows
- .tar
- .tar.gz
- .zip
- .rar
- .gz
- .bz2
.tar file
tar file contains more then one file or directory in a singles file.
Some options which are used to make a tar file
- -c — create a new archive
- -f — when used with the -c option, use the filename specified for the creation of the tar file; when used with the -x option, unarchive the specified file
- -t — show the list of files in the tar file
- -v — show the progress of the files being archived
- -x — extract files from an archive
- -z — compress the tar file with gzip
- -j — compress the tar file with bzip2
To create tar file
$tar –cvf filename.tar path/to/directory/file1 file2 file3 …
To make a tar directory
$tar -cvf directory_name.tar path/to/directory/
.tar using files from different directories
$tar -cvf path/to/first/directory/file1 path/to/second/directory/file2
To extract a tar file
$tar -xvf file_name.tar
To check contents in a .tar file
$tar -tvf file_name.tar
or
$less file_name.tar
Using less, your all file from archive will be shown in a new window. Press ‘q’ to come out form this window.
.tar.gz file
.tar.gz is another way to compress a file in Linux which is a gzipped tar archive.
To create tar file
$tar -zcvf filename.tar path/to/directory/file1 file2 file3 …
To make a tar directory
$tar -zcvf directory_name.tar path/to/directory/
.tar using files from different directories
$tar -zcvf path/to/first/directory/file1 path/to/second/directory/file2
To extract a tar file
$tar -zxvf file_name.tar
To check contents in a .tar file
$tar -ztvf file_name.tar
or
$less file_name.tar.gz
Using less, your all file from archive will be shown in a new window. Press ‘q’ to come out form this window.
.zip file
This a third way to compress a file using command line
$zip file_name.zip file1 file2 file3
or
$zip -r file_name.tar file1 file2 file3
To extract a .zip file in your working directory use commend as follows:
$unzip file_name.zip
To check contents in a zip file
$less file_name.zip
How to add a new file in existing zip file
$ zip -r file_name.zip file_new
.rar file
You need a rar tool to extract .rar file. But unfortunately pre-installed package for .rar file is not available in Linux, where rar is freely available under windows operating system. rar is very familiar tool to compress a file in windows.
Let’s try to make a .rar file before installing rar tool
$rar a file_name.rar file1 file2 file3
It will show a error as follows
rar tool can be installed as follows in Linux operating system
$sudo apt-get install rar
or
You can install this tool by using yum as well
$sudo yum install rar
But in some distros first you will have to install yum. Which can be installed as follows
$sudo apt-get install yum
Next, you will be asked to install yum repository %% This portion will be covered in next article %%
After installing a rar tool using either sudo apt-get or sudo yum, you can create a rar file as follows
$rar a file_name.rar file1 file2 file3
To extract a rar file, use command as follows
$rar e file_name.rar
To check contents in a rar file, use command as follows
$rar l file_name.rar
Option to test a .rar file is as follows
$rar t file_name.rar
To recover a rar file, use command as follows
$rar r file_name.rar
.gz file
Using this a single file can be compressed at a time. Although you can compress more then one file but you will different .gz files for each file.
$gzip file1
Let’s try to compress more then one file at a time
$gzip file1 file2
To extract a .gz file, command can be used as follows
$gzip -d file1.gz
To extract more then one file
$gzip -d file1.gz file2.gz file3.gz
.bz2 file
Syntax of this file format is pretty similar to .tar and .tar.gz file. Which is as follows
$tar -jcvf file_name.bz2 file1 file2 file3
To extract a .bz2 file, command can be used as follows
$tar -jxvf file_name.bz2
To check contents in a .bz2 file, command is as follows
$tar -jtvf file_name.bz2
I am pleased to inform that you can also write articles for PaperGuide. Visit our page Join PaperGuide for more details or send your detailed resume along with a sample article ( atleast 1000 words )at admin@paperguide.org.
Awesome, it’s helpful for me and i think it’s helpful for all new linux user.
Thank you so much.
Hi,
I thank to you for your valuable time and appreciation. Let me inform you that you can also write for paperguide. Click here to know more.
Thanks
Ramesh