Home  >  Article  >  php教程  >  Detailed explanation and examples of compressing zip and decompressing unzip commands under Linux

Detailed explanation and examples of compressing zip and decompressing unzip commands under Linux

高洛峰
高洛峰Original
2016-12-12 16:50:421302browse

Detailed explanation and examples of compression and decompression commands under Linux

Example: The content of the current directory on the compression server is the xxx.zip file

zip -r xxx.zip ./*

Extract the zip file to the current directory

unzip filename .zip

============================

Also: Some servers cannot execute the zip command without installing the zip package, but basically You can use the tar command. The example is as follows:

tar -zcvf /home/zdzlibs.tar.gz /home/zdz/java/zdzlibs/

================ ==============

linux zip command

zip -r myfile.zip ./*
Compress all files and folders in the current directory into the myfile.zip file, - r means recursively compress all files in the subdirectory.

2.unzip
unzip -o -d /home/sunny myfile.zip
Extract the myfile.zip file to /home/sunny/
-o: without prompting Overwrite the file;
-d:-d /home/sunny indicates to decompress the file to the /home/sunny directory;

3. Others
zip -d myfile.zip smart.txt
Delete the smart.txt file in the compressed file
zip -m myfile.zip ./rpm_info.txt
Add the rpm_info.txt file to myfile.zip in the compressed file
----------------------- -------------------------------------------------- ------

To use zip to compress files, type the following command at the shell prompt:

zip -r filename.zip filesdir

In this example, filename.zip represents the file you created, filesdir represents the directory where you want to place the new zip file. The -r option specifies that you want to recursively include all files contained in the filesdir directory.

To extract the contents of a zip file, type the following command:

unzip filename.zip

You can use the zip command to process multiple files and directories at the same time by listing them one by one, separated by spaces:

zip -r filename.zip file1 file2 file3 /usr/work/school

The above command compresses the contents of file1, file2, file3, and /usr/work/school directories (assuming this directory exists), and then puts it into filename .zip file.

tar command details

  -c: Create a compressed archive

  -x: Decompress

-t: View the content

-r: Append files to the end of the compressed archive

-u: Update the original compressed package Files

These five are independent commands. One of them is used for compression and decompression. It can be used in conjunction with other commands but only one of them can be used. The following parameters are optional when compressing or decompressing archives as needed.

  -c: Create a compressed archive

- -f is required

  -f: Use the file name. Remember, this parameter is the last parameter, and can only be followed by the file name.

 # tar -cf all.tar *.jpg

 This command is to package all .jpg files into a package named all.tar. -c means generating a new package, and -f specifies the file name of the package.

 # tar -rf all.tar *.gif

 This command is to add all .gif files to the all.tar package. -r means adding files.

 # tar -uf all.tar logo.gif

 This command is to update the logo.gif file in the original tar package all.tar. -u means to update the file.

  # tar -tf all.tar

  This command is to list all the files in the all.tar package, -t means to list the files

  # tar -xf all.tar

  This command is to extract All files in the all.tar package, -t means unpack

 Compression

 tar –cvf jpg.tar *.jpg //Package all jpg files in the directory into tar.jpg

 tar –czf jpg.tar .gz *.jpg //Package all jpg files in the directory into jpg.tar, and compress them with gzip to generate a gzip-compressed package named jpg.tar.gz

tar –cjf jpg.tar .bz2 *.jpg //Package all the jpg files in the directory into jpg.tar, and compress them with bzip2 to generate a bzip2-compressed package named jpg.tar.bz2

 tar –cZf jpg.tar .Z *.jpg //Package all jpg files in the directory into jpg.tar, and compress them with compress to generate a umcompress compressed package, named jpg.tar.Z

 rar a jpg.rar * .jpg //For compression in rar format, you need to download rar for linux first

 zip jpg.zip *.jpg //For compression in zip format, you need to download zip for linux first

 Unzip

 tar –xvf file.tar // Unzip the tar package

 tar -xzvf file.tar.gz //Unzip tar.gz

 tar -xjvf file.tar.bz2 //Unzip tar.bz2

 tar –xZvf file.tar.Z //Unzip tar. Z

 unrar e file.rar //Unzip rar

 unzip file.zip //Unzip zip

  Summary

  1. Use tar –xvf to decompress *.tar

2. *.gz Use gzip -d or gunzip Decompress

3. Use tar –xzf to decompress *.tar.gz and *.tgz

4. Decompress *.bz2 with bzip2 -d or bunzip2

5. Decompress *.tar.bz2 with tar –xjf

6. *.Z Use uncompress to decompress

7. *.tar.Z Use tar –xZf to decompress

8. *.rar Use unrar e to decompress

 9. Use unzip to decompress *.zip

 Detailed explanation of tar command in Linux (reprinted information)

 Thursday, April 17, 2008 15:37

 tar command

 tar can create archives for files and directories. Using tar, users can create archives (backup files) for a specific file, change files in the archive, or add new files to the archive. tar was originally used to create archives on tapes, but now users can create archives on any device, such as floppy disks. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission. tar on Linux is the GNU version.

  Syntax: tar [primary option + auxiliary option] file or directory

When using this command, the main option is required. It tells tar what to do. The auxiliary option is auxiliary and can be selected.

 Main options:

 c Create a new archive file. If the user wants to back up a directory or some files, he or she must select this option.

 r Append the file to be archived to the end of the archive file. For example, the user has made a backup file and finds that there is another directory or some files that he has forgotten to back up. In this case, you can use this option to append the forgotten directory or files to the backup file.

 t List the contents of archive files and see which files have been backed up.

 u Update file. That is to say, the original backup file is replaced with the new file. If the file to be updated cannot be found in the backup file, it is appended to the end of the backup file.

 x Release files from archives.

 Auxiliary options:

 b This option is set for the tape drive. It is followed by a number to indicate the size of the block. The system default value is 20 (20*512 bytes).

 f Using archive files or devices, this option is usually required.

 k Save existing files. For example, when we restore a certain file, if we encounter the same file during the restoration process, it will not be overwritten.

  m When restoring files, set the modification time of all files to now.

 M Create multi-volume archive files to store on several disks.

 v Detailed report of file information processed by tar. Without this option, tar does not report file information.

 w Every step requires confirmation.

  z Use gzip to compress/decompress files. After adding this option, you can compress the archive file, but you must also use this option to decompress when restoring.

 Analysis of Compressed Files under Linux

 For those who are new to Linux, they will definitely be confused by a lot of various file names under Linux. Not to mention anything else, just take compressed files as an example. We know that there are only two most common compressed files under Windows, one is zip and the other is .rap. But Linux is different. It has many compressed file names such as .gz, .tar.gz, tgz, bz2, .Z, .tar, etc. In addition, .zip and .rar under Windows can also be used under Linux, but in There are too few people using .zip and .rar on Linux. This article will summarize these common compressed files. I hope you will not be confused next time you encounter these files.

Before specifically summarizing various types of compressed files, we must first clarify two concepts: packaging and compression. Packaging refers to turning a large number of files or directories into a total file, while compression means turning a large file into a small file through some compression algorithms. Why should we distinguish these two concepts? In fact, this is because many compression programs in Linux can only compress one file. So when you want to compress a large number of files, you have to use other tools to compress them first. A large number of files are first packed into a package and then compressed using the original compression program.

 The most commonly used packaging program under Linux is tar. Packages produced using the tar program are often called tar packages. The commands for tar package files usually end with .tar. After generating the tar package, you can use other programs to compress it, so first let’s talk about the basic usage of the tar command:

There are many options for the tar command (you can view it with man tar), but the most commonly used ones are: Several options, here are some examples:

  # tar -cf all.tar *.jpg

 This command is to package all .jpg files into a package named all.tar. -c means generating a new package, and -f specifies the file name of the package.

 # tar -rf all.tar *.gif

 This command is to add all .gif files to the all.tar package. -r means adding files.

 # tar -uf all.tar logo.gif

 This command is to update the logo.gif file in the original tar package all.tar. -u means to update the file.

  # tar -tf all.tar

  This command is to list all the files in the all.tar package, -t means to list the files

  # tar -xf all.tar

  This command is to extract For all files in the all.tar package, -t means unpacking

The above is the most basic usage of tar. In order to facilitate users to compress or decompress files while packaging and unpacking, tar provides a special function. This is why tar can call other compression programs while packaging or unpacking, such as calling gzip, bzip2, etc.

 1) tar calls gzip

 gzip is a compression program developed by the GNU organization. Files ending in .gz are the results of gzip compression. The decompression program opposite to gzip is gunzip. Use the -z parameter in tar to call gzip. Let’s give an example below:

  # tar -czf all.tar.gz *.jpg

 This command is to type all .jpg files into a tar package and compress it with gzip to generate a gzip compressed file. The package name is all.tar.gz

 # tar -xzf all.tar.gz

 This command is to unpack the package generated above.

 2) tar calls bzip2

 bzip2 is a compression program with stronger compression capabilities. Files ending in .bz2 are the results of bzip2 compression. The decompression program opposite to bzip2 is bunzip2. Use the -j parameter in tar to call gzip. Let’s give an example:

  # tar -cjf all.tar.bz2 *.jpg

 This command is to type all .jpg files into a tar package and compress it with bzip2 to generate a bzip2 compressed file. The package name is all.tar.bz2

 # tar -xjf all.tar.bz2

 This command is to unpack the package generated above.

  3) tar calls compress

Compress is also a compression program, but it seems that not as many people use compress as gzip and bzip2. Files ending in .Z are the result of bzip2 compression. The decompressor program opposite to compress is uncompress. Use the -Z parameter in tar to call gzip. Let’s give an example below:

  # tar -cZf all.tar.Z *.jpg

 This command is to package all .jpg files into a tar package and compress it with compress to generate an uncompressed file. package, the package name is all.tar.Z

  # tar -xZf all.tar.Z

 This command is to unpack the package generated above

With the above knowledge, you should be able to unpack a variety of The files are compressed. Here is a summary of the tar series of compressed files:

 1) For files ending with .tar

 tar -xf all.tar

 2) For files ending with .gz

 gzip -d all. gz

 gunzip all.gz

 3) For files ending with .tgz or .tar.gz

 tar -xzf all.tar.gz

 tar -xzf all.tgz

 4) For files ending with .bz2

  bzip2 -d all.bz2

bunzip2 all.bz2

5) For files ending with tar.bz2

tar -xjf all.tar.bz2

6) For files ending with .Z

uncompress all. Z

 7) For files ending in .tar.Z

  tar -xZf all.tar.z

  In addition, for common compressed files .zip and .rar under Windows, Linux also has corresponding methods to decompress them:

1) For .zip

  Linux provides zip and unzip programs, zip is a compression program, and unzip is a decompression program. They have many parameter options. Here is just a brief introduction. Let’s still give an example to illustrate their usage:

  # zip all.zip *.jpg

 This command is to compress all .jpg files into a zip package

  # unzip all.zip

 This command is to extract all the files in all.zip

 2) For .rar

 To process .rar files under Linux, you need to install RAR for Linux, which can be downloaded from the Internet, but Remember, RAR for Linux

  is not free; then install:

  # tar -xzpvf rarlinux-3.2.0.tar.gz

  # cd rar

  # make

 This is installed, after installation With the two programs rar and unrar, rar is a compression program and unrar is a decompression program. They have many parameter options. Here is just a brief introduction. Let’s still give an example to illustrate their usage:

  # rar a all *.jpg

  This command is to compress all .jpg files into a rar package named all. rar, the program will automatically append the .rar extension to the package name.

  # unrar e all.rar

 This command is to extract all the files in all.rar

 That’s it, we have introduced tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar and other programs, you should already be able to use them to compress .tar, .gz, .tar.gz, .tgz, .bz2, .tar.bz2, .Z, .tar.Z, . 10 types of compressed files such as zip and .rar have been decompressed. In the future, you should not have to worry about downloading a software but not knowing how to decompress it under Linux. And the above method is basically effective for Unix.

This article introduces the compression programs tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar and other programs under Linux, and how to use them to compress .tar, .gz, .tar.gz, . Operate 10 types of compressed files: tgz, .bz2, .tar.bz2, .Z, .tar.Z, .zip, .rar


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn