In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "the method of compressing files on Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
There are a number of commands for compressing files on Linux. The latest and most effective method is xz, but all methods have the advantages of saving disk space and maintaining backup files for later use. In this article, we will compare these compression commands and point out significant differences.
Tar
The tar command is not a dedicated compression command. It is often used to pull multiple files into a single file for easy transfer to another system, or to back up files as a related group. It also provides compression, which makes sense, adding a z compression option to compress files.
When you use the z option to attach a compression process to the tar command, tar uses gzip for compression.
Just like compressing a set of files, you can use tar to compress individual files, although this operation has no special advantage over using gzip directly. To do this with tar, simply use the tar cfz newtarfile filename command to identify the files to be compressed, just as you would a set of files, like this:
$tar cfz bigfile.tgz bigfile ^ ^ | +-New file +-File to be compressed $ls-l bigfile*-rw-rw-r-- 1 shs shs 103270400 Apr 16 16:09 bigfile-rw-rw-r-- 1 shs shs 21608325 Apr 16 16:08 bigfile.tgz
Notice that the size of the file has been significantly reduced.
If you prefer, you can use the tar.gz extension, which may make the file more distinctive, but most Linux users will probably realize that it means the same as tgz-a combination of tar and gz to show that the file is a compressed tar file. After the compression is complete, you will get both the original file and the compressed file.
To collect many files together and compress "tar ball" in a single command, use the same syntax, but specify that the files to be included are in a group, not a single file. Here is an example:
$tar cfz bin.tgz bin/* ^ ^ | +-the file to be included + the new file
Zip
The zip command creates a compressed file while preserving the integrity of the original file. The syntax is as simple as using tar, except that you must remember that your original file name should be the last argument on the command line.
$zip. / bigfile.zip bigfile updating: bigfile (deflated 79%) $ls-l bigfile bigfile.zip-rw-rw-r-- 1 shs shs 103270400 Apr 16 11:18 bigfile-rw-rw-r-- 1 shs shs 21606889 Apr 16 11:19 bigfile.zip
Gzip
The gzip command is very easy to use. You just type gzip, followed by the name of the file you want to compress. Unlike the commands described above, gzip encrypts the file "in place". In other words, the original file will be replaced by the encrypted file.
$gzip bigfile $ls-l bigfile*-rw-rw-r-- 1 shs shs 21606751 Apr 15 17:57 bigfile.gz
Bzip2
Just like using the gzip command, bzip2 will compress the file of your choice "in place" without leaving the original file.
$bzip bigfile $ls-l bigfile*-rw-rw-r-- 1 shs shs 18115234 Apr 15 17:57 bigfile.bz2
Xz
Xz is a relatively new member of the compression command team and is a leader in the ability to compress files. Like the previous two commands, you only need to provide the file name to the command. Again, the original file is compressed in place.
$xz bigfile $ls-l bigfile*-rw-rw-r-- 1 shs shs 13427236 Apr 15 17:30 bigfile.xz
For large files, you may notice that xz takes more time to run than other compression commands, but the compression results are amazing.
Contrast
Most people have heard that size is not everything. So, let's compare the file size and some of the problems when you plan to compress the file.
The statistics shown below are all related to compressing a single file, and bigfile is used in the example shown above. This file is a large and fairly random text file. The compression ratio depends to some extent on the contents of the file.
(1) size reduction rate
When compared, the various compression lives shown above produce the following results. The percentage represents the comparison between the compressed file and the original file.
-rw-rw-r-- 1 shs shs 103270400 Apr 16 14:01 bigfile-rw-rw-r-- 1 shs shs 18115234 Apr 16 13:59 bigfile.bz2 ~ 17%-rw-rw-r-- 1 shs shs 21606751 Apr 16 14:00 bigfile.gz ~ 21 %-rw-rw-r-- 1 shs shs 21608322 Apr 16 13:59 bigfile.tgz ~ 21%-rw-rw-r-- 1 shs shs 13427236 Apr 16 14:00 bigfile.xz ~ 13%-rw-rw-r-- 1 shs shs 21606889 Apr 16 13:59 bigfile.zip ~ 21%
The xz command won and ended up with only 13% of the compressed file size, but all of these compression commands significantly reduced the size of the original file.
(2) whether to replace the original file
The bzip2, gzip, and xz commands all replace the original file with a compressed file. The tar and zip commands are not replaced.
(3) running time
The xz command seems to take more time than other commands to encrypt files. For bigfile, the approximate time is:
Command run time tar 4.9 seconds zip 5.2 seconds bzip2 22.8 seconds gzip 4.8 seconds xz 50.4 seconds
Unzipping the file is likely to take much less time than the compression.
(4) File permissions
No matter what permissions you set for compressed files, the permissions for compressed files will be based on your umask settings, with the exception of bzip2, which retains the permissions of the original files.
(5) compatibility with Windows
Files created by the zip command can be used (that is, unzipped) on Windows systems as well as Linux and other Unix systems without the need to install additional tools, whether they may be available or unavailable.
Extract the file
The command to extract a file is similar to the command to compress a file. After we run the compression commands above, these commands are used to extract the bigfile:
Tar: tar xf bigfile.tgz
Zip: unzip bigfile.zip
Gzip: gunzip bigfile.gz
Bzip2: bunzip2 bigfile.gz2
Xz: xz-d bigfile.xz or unxz bigfile.xz
Run your own compression comparison
If you want to run some tests yourself, grab a large and replaceable file and use each of the commands shown above to compress it-preferably a new subdirectory. You may need to install xz first if you want to include it in the test. This script may be easier to compress, but it may take a few minutes to complete.
#! / bin/bash # ask the user for the file name echo-n "filename >" read filename # you need this Because some commands will replace the original file cp $filename $filename-2 # first (in case the previous results are still available) rm $filename.* tar cvfz. / $filename.tgz $filename > / dev/null zip $filename.zip $filename > / dev/null bzip2 $filename # restore the original file cp $filename-2$ filename gzip $filename # restore the original file cp $filename-2$ filename xz $filename # display the result ls-l $filename.* # replace the original file mv $filename-2 This is the end of the introduction of $filename "the method of compressing files on Linux". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.