Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compress and decompress files in Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to compress and decompress files in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Compressing files is useful when backing up important files and sending large files over the network. Please note that compressing an already compressed file adds extra overhead, so you will get a larger file. Therefore, please do not compress files that have already been compressed. In GNU/Linux, there are many programs that can be used to compress and decompress files. In this tutorial, we will only learn about two of these applications.

In Unix-like systems, the most common programs used to compress files are:

Gzip

Bzip2

1. Use the gzip program to compress and decompress files

Gzip is a utility that uses the Lempel-Ziv Encoding (LZ77) algorithm to compress and decompress files.

1.1 compressed files

If you want to compress a file named ostechnix.txt into a compressed file in gzip format, simply run the following command:

$gzip ostechnix.txt

When the above command finishes running, a compressed gzip file named ostechnix.txt.gz will appear instead of the original ostechnix.txt file.

The gzip command can also be used in other ways. An interesting example is that we can pipe the output of a particular command and then create a compressed file as input to the gzip program. Look at the following command:

$ls-l Downloads/ | gzip > ostechnix.txt.gz

The above command will create a compressed file in gzip format with the contents of a directory entry in the Downloads directory.

1.2 compress the file and write the output to the new file (do not overwrite the original file)

By default, the gzip program compresses the given file and replaces the original file with the compressed file. However, you can also keep the original file and write the output to standard output. For example, the following command will compress the ostechnix.txt file and write the output to the file output.txt.gz.

$gzip-c ostechnix.txt > output.txt.gz

Similarly, to extract a compressed file in gzip format and specify the file name of the output file, simply run:

$gzip-c-d output.txt.gz > ostechnix1.txt

The above command unzips the output.txt.gz file and writes the output to the file ostechnix1.txt. In both of the above examples, the original file is not deleted.

1.3 extract the file

If you want to extract the ostechnix.txt.gz file and replace it with the original uncompressed version, simply run:

$gzip-d ostechnix.txt.gz

We can also use the gunzip program to extract the file:

$gunzip ostechnix.txt.gz1.4 views the contents of the compressed file without unzipping it

If you want to use the gzip program to view the contents of the compressed file without unzipping it, you can use the-c option like this:

$gunzip-c ostechnix1.txt.gz

Or, you can use the zcat program like this:

$zcat ostechnix.txt.gz

You can also pipe the output to the less command to view the output page by page, like this:

$gunzip-c ostechnix1.txt.gz | less$ zcat ostechnix.txt.gz | less

In addition, the zless program can also achieve the same function as the above pipeline.

$zless ostechnix1.txt.gz1.5 uses gzip to compress files and specify the compression level

Another significant advantage of gzip is that it supports compression levels. It supports the three levels of compression given below:

1-fastest (worst)

9-slowest (*)

6-default level

To compress the file named ostechnix.txt to a compressed gzip file at the "* *" compression level, run:

$gzip-9 ostechnix.txt1.6 connects multiple compressed files

We can also compress multiple files that need to be compressed into the same file. How can it be realized? Look at the following example.

$gzip-c ostechnix1.txt > output.txt.gz$ gzip-c ostechnix2.txt > > output.txt.gz

The above two commands will compress the files ostechnix1.txt and ostechnix2.txt and save the output to a file called output.txt.gz.

You can view the contents of the two files ostechnix1.txt and ostechnix2.txt without unzipping them with any of the following commands:

$gunzip-c output.txt.gz$ gunzip-c output.txt$ zcat output.txt.gz$ zcat output.txt

If you want to know more about gzip, please refer to its man manual.

$man gzip2. Use the bzip2 program to compress and decompress files

Bzip2 is very similar to gzip, but bzip2 uses the Burrows-Wheeler block sort compression algorithm and uses Huffman Huffman coding. Files compressed with bzip2 end with the ".bz2" extension.

As I said above, the use of bzip2 is almost exactly the same as gzip. Just replace gzip with bzip2, gunzip with bunzip2, and zcat with bzcat in the above example.

To compress a file using bzip2 and replace it with the compressed file, simply run:

$bzip2 ostechnix.txt

If you don't want to replace the original file, you can use the-c option and write the output to the new file.

$bzip2-c ostechnix.txt > output.txt.bz2

If you want to extract the file, run:

$bzip2-d ostechnix.txt.bz2

Or

$bunzip2 ostechnix.txt.bz2

If you want to view the contents of a compressed file without unzipping it, run:

$bunzip2-c ostechnix.txt.bz2

Or

$bzcat ostechnix.txt.bz2

If you want to know more about bzip2, please refer to its man manual.

$man bzip2

In this tutorial, we learned what gzip and bzip2 programs are, and how to use them to compress and decompress files through some examples under GNU/Linux.

The above is the editor for you to share how to compress and decompress files in Linux, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report