As we know Compression is the reduction in size of data to save space. For data compression ZIP format is used and it’s in archive format. ZIP file contains one or more files that have been compressed to reduce file size, or stored as it is. This time we are going to reduce size of file or directory using command-line and save space on HDD.
For compression of files or directories we are using COMPACT command in Windows.
So let’s play with ‘COMPACT’ in command-line.
“COMPACT” command is used for un-compressed NTFS files, it doesn't extract files from a compressed (zipped) file.
Let’s see how to use this command.
For getting help or all options use following command.
For compressing the specified directory or file use following command
C:>compact /c /s:c:CHMag
s- is used to all subdirectories of the specified directory. CHMag is my directory on C drive and it contains some files and subdirectories. After execution of this command CHMag folder gets compressed. For verification go to explorer and check that if the folder name is blue colored.
Fig.1 shows you detail of compresses the CHMag directory
For decompressing the specified directory or file use /u option i.e. for uncompressing the specified directory or file.
C:>compact /u /s:c:CHMag
To set the compression of the current directory, its subdirectories, and existing files use following command.
C:>compact /c /s
Fig.3 shows you compression of the C directory.
To Unset the compression of the current directory, its subdirectories, and existing files use the following command. (refer Fig.4)
C:>compact /u /s
We can also compress the image files like jpg, png, bmp files using compact command
For compression of a .bmp file use following command
C:>compact /c /f varun.bmp
“varun.bmp” is my file on C drive use your file name with the address. /f –used to force complete compression of the file. And to uncompress use /u option instead of /c (refer Fig.5)
Now it’s time to save disk space in Linux platform. We all know that Linux is open source and is enhanced by free advanced tools to operate on data. So in Linux platform we are going to use “gzip”.
gzip is also a standard internal command library in Linux used to reduce file size using Lempel-Zev compression algorithm. One important thing to remember about gzip is that it replaces your original file with a compressed version with .gz extension. The amount of compression varies with the type of data, but a typical text file will be reduced by 70 to 80 percent and other types accordingly vary For Example, if you compress plain/text file clubhack which is 4.6 kb (4741 bytes). After compression it is reduced to After compressing using gzip, extension is changed like this
.gz is added after file name which means file is compressed using gzip.
Now you have compressed file using gzip it’s time to extract or decompress file. To do this enter command
[email protected]:~$ gunzip clubhack.gz or [email protected]:~$ gzip –d clubhack.gz
This command replaces this file back with original one ‘clubhack’ and extension .gz is removed.
-r : This is the most useful option which tells gzip and gunzip to recursively compress or decompress all files in the current directory and any subdirectories.
[email protected]:~$ gzip -r somedirectory
Zip all files in a directory called somedirectory.
Unzip all files in the somedirectory.
[email protected]:~$ gunzip -r somedirectory
-n : with this option you can tell gzip to use different levels of compression with the-n flag, where n is a number from 1 to 9.
The -1 flag means "fast but less efficient" compression, and -9 means "slow but most efficient" compression. Values of n between 1 and 9 will vary speed and efficiency, and the default is -6. If you want to get the best possible compression and use the -9 flag, like this:
[email protected]:~$ gzip -9 clubhack
-c : It's common to apply gzip to a tar file, which is why you see files with names like clubhack.tar.gz on Linux systems. When you want to extract the contents of a gzipped tar file, you have several choices. The first is to use gunzip followed by tar, like this:
[email protected]:~$ gunzip clubhack.tar.gz [email protected]:~$ tar xvf clubhack.tar
Or you can do it all in one command, like this:
[email protected]:~$ gunzip -c clubhack.tar.gz | tar xvf -
The -c flag tells gunzip to decompress the file, but instead of creating a clubhack.tar file, it extracts the decompressed data directly to the tar command. The tar command on the right side of the pipeline looks a little strange, instead of a file name after the xvf, there's just a dash. The dash tells tar that the input is not an actual file on disk, but rather a stream of data from the pipeline.
Concatenating multiple files:
It is possible to concatenate multiple files in to one file. In that case gunzip will extract all files at once.
[email protected]:~$ gzip -c file1 > clubhack.gz [email protected]:~$ gzip -c file2 >> clubhack.gz
then to extract files
[email protected]:~$ gunzip -c clubhack