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

What are the compression and decompression commands for files in Linux

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

Share

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

This article shows you what Linux file compression and decompression commands, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

1. Zip compression and decompression

Zip is the most widely used compression program. Files compressed by it produce compressed files with the extension zip, and this format can be used on a variety of systems, such as winzip in windows.

Let's take a look at how to create a zip file in linux.

When we type zip into the terminal, some introduction to this command and the meaning of the parameters will appear.

The code is as follows:

Xiaopeng@ubuntu:~/test$ zip

Copyright (c) 1990-2006 Info-ZIP-Type 'zip "- L" for software license.

Zip 2.32 (June 19th 2006). Usage:

Zip [- options] [- b path] [- t mmddyyyy] [- n suffixes] [zipfile list] [- xi list]

The default action is to add or replace zipfile entries from list, which

Can include the special name-to compress standard input.

If zipfile and list are omitted, zip compresses stdin to stdout.

-f freshen: only changed files-u update: only changed or new files

-d delete entries in zipfile-m move into zipfile (delete files)

-r recurse into directories-j junk (don't record) directory names

-0 store only-l convert LF to CR LF (- ll CR LF to LF)

-1 compress faster-9 compress better

-q quiet operation-v verbose operation/print version info

-c add one-line comments-z add zipfile comment

-@ read names from stdin-o make zipfile as old as latest entry

-x exclude the following names-i include only the following names

-F fix zipfile (- FF try harder)-D do not add directory entries

An adjust self-extracting exe-J junk zipfile prefix (unzipsfx)

-T test zipfile integrity-X eXclude eXtra file attributes

-y store symbolic links as the link instead of the referenced file

-R PKZIP recursion (see manual)

-e encrypt-n don't compress these suffixes

Let's do the simplest experiment. We just compress all the files whose names start with test in the current directory and check the compression ratio. (red is my comment)

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 24K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

The code is as follows:

Xiaopeng@ubuntu:~/test$ zip test.zip test*

The zip command is followed by the compressed file name, here is test.zip, of course, the suffix name is not required. Then follow the file name you want to compress. The test* used here refers to all files that begin with test, including test1 test2 test3 test4

Adding: test1 (deflated 30%) the compression ratio is shown here

Adding: test2 (deflated 65%)

Adding: test3 (deflated 64%)

Adding: test4 (deflated 73%) roughly shows that the larger the source file, the greater the compression ratio

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 32K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

-rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:17 test.zip

Xiaopeng@ubuntu:~/test$

The above is compressed the same type of files, in fact, you can also compress different types of files together. Sometimes in order to save hard disk space, you can automatically delete the original file after the establishment of the compressed file, as long as you take a-m parameter.

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 24K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

Xiaopeng@ubuntu:~/test$ zip-m test.zip test* with parameter-m

Updating: test1 (deflated 30%)

Updating: test2 (deflated 65%)

Updating: test3 (deflated 64%)

Updating: test4 (deflated 73%)

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 8.0K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:26 test.zip

Xiaopeng@ubuntu:~/test$

You can see that the original file has been deleted, only the compressed file is left.

When compressing some directories, there will be subdirectories in the directory, which can be divided into two cases according to whether the files in the subdirectory are compressed or not, one is compression, the other is ignoring the contents of the self-recording, if you choose to compress the subdirectory, use the-r parameter, if not compressed, use the-j parameter

For example, one is-r and the other is-j

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 28K

The code is as follows:

Drwxr-xr-x 2 xiaopeng xiaopeng 4.0K 2009-06-25 14:31 pdf

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

Xiaopeng@ubuntu:~/test$ zip-r test.zip * compresses all the contents of the current directory. The r parameter indicates that the contents of the pdf subdirectory are also compressed.

Adding: pdf/ (stored 0%)

Adding: pdf/case_Contact.pdf (deflated 10%)

Adding: pdf/case_KRUU.pdf (deflated 9%)

Adding: pdf/case_howard_county_library.pdf (deflated 24%)

Adding: test1 (deflated 30%)

Adding: test2 (deflated 65%)

Adding: test3 (deflated 64%)

Adding: test4 (deflated 73%)

Xiaopeng@ubuntu:~/test$

The following situation is that subdirectories are not compressed

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-l

Total dosage 28

The code is as follows:

Drwxr-xr-x 2 xiaopeng xiaopeng 4096 2009-06-25 14:31 pdf

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1233 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3412 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 10091 2009-06-25 14:14 test4

Xiaopeng@ubuntu:~/test$ zip-j test.zip *

Adding: test1 (deflated 30%)

Adding: test2 (deflated 65%)

Adding: test3 (deflated 64%)

Adding: test4 (deflated 73%)

Subdirectory pdf is ignored

The code is as follows:

Xiaopeng@ubuntu:~/test$

Make a trick: some files because of coding reasons, has greatly reduced the file size, such as GIF,JPG format, in the use of zip compression almost nothing and waste of time, you can use the-n parameter to directly save these files without compression. For example:

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 68K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 18K 2009-06-04 21:18 duality.jpg

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

-rw-r--r-- 1 xiaopeng xiaopeng 23K 2009-06-10 15:07 test.jpg

Xiaopeng@ubuntu:~/test$ zip-n .jpg test.zip *

Adding: duality.jpg (stored 0%)

Adding: test1 (deflated 30%)

Adding: test2 (deflated 65%)

Adding: test3 (deflated 64%)

Adding: test4 (deflated 73%)

Adding: test.jpg (stored 0%)

The jpg format is not compressed but saved directly.

The code is as follows:

Xiaopeng@ubuntu:~/test$

If you need to save more than one format directly, you can separate it with a colon, such as:-n .jpg: .mpg

Tip: sometimes there are many files to be compressed in a directory, but there are so few uncompressed files, so we can use the-x parameter to exclude these uncompressed files. For example

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Duality.jpg test1 test2 test3 test4 test.jpg test.zip

Xiaopeng@ubuntu:~/test$ zip-n .jpg test.zip *-x test2 does not compress test2

Updating: duality.jpg (stored 0%)

Updating: test1 (deflated 30%)

Updating: test3 (deflated 64%)

Updating: test4 (deflated 73%)

Updating: test.jpg (stored 0%)

Xiaopeng@ubuntu:~/test$

You can see that test2 is not compressed, but skips it directly.

Compress the link, zip will first read the contents of the original file that the link points to, and then compress it, and after the compression, the link no longer exists.

In addition, the compression ratio can also be adjusted. Grades 1 to 9 are the lowest, 9 is the highest, and the default is 6. We can compare the compression ratio with 1 and 9.

The code is as follows:

Xiaopeng@ubuntu:~/test$ zip-1 low.zip *

Adding: test1 (deflated 30%)

Adding: test2 (deflated 63%)

Adding: test3 (deflated 62%)

Adding: test4 (deflated 70%)

Xiaopeng@ubuntu:~/test$ zip-9 high.zip *

Adding: low.zip (deflated 4%)

Adding: test1 (deflated 30%)

Adding: test2 (deflated 65%)

Adding: test3 (deflated 64%)

Adding: test4 (deflated 73%)

Because the files are relatively small, the effect is not very obvious, but the compression ratio of 9 is a little higher.

High compression ratio, save space, but long compression time, low compression ratio, save less space, but less time, so we have to choose a reasonable compression ratio, generally use the default.

Extract the zip file. This is relatively simple, which is the unzip command.

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test.zip

Xiaopeng@ubuntu:~/test$ unzip test.zip

Archive: test.zip

Inflating: test1

Inflating: test2

Inflating: test3

Inflating: test4

Xiaopeng@ubuntu:~/test$

Of course, you can also use the-x parameter to specify which files do not need to be compressed.

The code is as follows:

Xiaopeng@ubuntu:~/test$ unzip test.zip-x test3 test3 does not need to be compressed

Archive: test.zip

Inflating: test1

Inflating: test2

Inflating: test4

Xiaopeng@ubuntu:~/test$

There is also a very useful parameter,-Z, which is capitalized. The function is to view the contents of the compressed file. Just like winzip in windows, we can open it without unzipping it to see what files are inside and what types of files are inside. For example, if I want to see the contents of test.zip, but I don't want to unzip it, I can do the following.

The code is as follows:

Xiaopeng@ubuntu:~/test$ unzip-Z test.zip

Archive: test.zip 5069 bytes 4 files

-rw-r--r-- 2.3 unx 212 tx defN 25-Jun-09 14:13 test1

-rw-r--r-- 2.3 unx 1233 tx defN 25-Jun-09 14:13 test2

-rw-r--r-- 2.3 unx 3412 tx defN 25-Jun-09 14:14 test3

-rw-r--r-- 2.3 unx 10091 tx defN 25-Jun-09 14:14 test4

4 files, 14948 bytes uncompressed, 4567 bytes compressed: 69.4%

Xiaopeng@ubuntu:~/test$

Of course, in addition to these parameters, there are many parameters can be used, here is not one by one experiment, we can use in the process to master.

2. Zip and tar

If you have installed a software package in Linux, you will be familiar with this compressed file with the suffix .tar.gz. For example, on the Linux QQ download page http://im.qq.com/qq/linux/download.shtml, you will see that one of the installation packages is the .tar.gz package.

There is a reason for this packet with two suffixes. Gz and tar are generated by two programs respectively. When gz is compressed by gzip, the compression effect is similar to that of zip, but the biggest difference with zip is that gzip cannot compress many single files into a single file, so tar has the opportunity to show its talents. Tar is not a compression program, it is used to package files. Tar and gzip hit it off as soon as they hit it off, and they worked together to achieve compression, that is, when multiple files were compressed, they were first packaged in tar into a .tar package, and then compressed by gzip, resulting in a .tar.gz file format.

First of all, take a look at the application of gzip and gunzip. The use of gzip is simple, followed by the name of the file to be compressed.

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 24K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 15:49 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

Xiaopeng@ubuntu:~/test$ gzip test1

Xiaopeng@ubuntu:~/test$ ls-lh

Total dosage 24K

The code is as follows:

-rw-r--r-- 1 xiaopeng xiaopeng 173 2009-06-25 15:49 test1.gz

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

Note that unlike zip, as long as you add the name of the file to be compressed after the command, the system will automatically name the generated compressed file with the suffix. gz, and the original file will be deleted after the compression is completed.

Gunzip for decompression

The code is as follows:

Xiaopeng@ubuntu:~/test$ gunzip * .gz

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4

Xiaopeng@ubuntu:~/test$

After completion, the previous compressed file test1.gz will also be deleted.

Similarly, gzip can also view the contents of the file before decompressing it, using the parameter-l

Gzip also supports compression ratio changes from 1 to 9, the same as zip.

Let's take a look at the use of tar. Tar is used to package files, the size of the packaged package is equal to the sum of all the original file sizes before, (in fact, the size is not equal, the size after packing is greater than the size and size of the source file, this can be verified. "Ubuntu entry to proficiency" said that the same big, obviously wrong) that tar does not have the effect of compression. Tar has a lot of parameters, which can be viewed through the online help documentation or with the-- help command. We only need to use a few here.

The first is the packaging of multiple files. Look at the example.

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4

Xiaopeng@ubuntu:~/test$ tar-cvf test.tar *

Is to package all files under the current directory into test.tar several parameters are: C (Creat) to establish a new file v (Verbose) to display the information when the command is executed f (File) specifies to be packaged as a file.

The code is as follows:

Test1

Test2

Test3

Test4

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4 test.tar

Xiaopeng@ubuntu:~/test$

To unlock the tar file, simply change c in the parameter to x (eXtract)

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test.tar

Xiaopeng@ubuntu:~/test$ tar-xvf test.tar

Test1

Test2

Test3

Test4

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4 test.tar

Xiaopeng@ubuntu:~/test$

Let's take a look at tar and gzip working together to compress four files. The step is to package the .tar package with tar, then compress the .tar package with gzip, and finally get the .tar.gz file. Example:

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4

Xiaopeng@ubuntu:~/test$ tar cvf test.tar * is first packaged into test.tar

Test1

Test2

Test3

Test4

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4 test.tar

Xiaopeng@ubuntu:~/test$ gzip test.tar compresses test.tar into a test.tar.gz package using gzip.

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4 test.tar.gz

Xiaopeng@ubuntu:~/test$

When unzipping the .tar.gz package, contrary to the compression process, extract it now, and then tar open the package.

The code is as follows:

Xiaopeng@ubuntu:~/test$ ls

Test.tar.gz

Xiaopeng@ubuntu:~/test$ gunzip test.tar.gz first decompresses the .tar.gz package with gunzip

Xiaopeng@ubuntu:~/test$ ls

Test.tar

Xiaopeng@ubuntu:~/test$ tar xvf test.tar then uses tar to open the .tar package.

Test1

Test2

Test3

Test4

Xiaopeng@ubuntu:~/test$ ls

Test1 test2 test3 test4 test.tar

Xiaopeng@ubuntu:~/test$

The above is the compression and decompression commands of which files in Linux. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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