Home  >  Article  >  php教程  >  Detailed analysis of compression and decompression commands under Linux

Detailed analysis of compression and decompression commands under Linux

高洛峰
高洛峰Original
2017-01-07 13:42:161223browse

linux zip command

zip -r myfile.zip ./*
Compress all files and folders in the current directory into myfile.zip files, -r means recursive compression of subdirectories All files.

2.unzip
unzip -o -d /home/sunny myfile.zip
Extract the myfile.zip file to /home/sunny/
-o: no prompt overwrite the file in case of .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, and 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 the file1, file2, file3, and /usr/work/school directories (assuming this directory exists), and then Put it into the 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 file

-u: Update the files in the original compressed package

These five are independent commands. One of them is used for compression and decompression. They can be used with others. commands are used together 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

-x: Unzip

-t: View the contents

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

-u: Update the files in the original compressed package

The following parameter -f is required

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

# 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 adds 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 lists all files in the all.tar package, -t means to list files

# tar - xf all.tar

This command is to extract all the files in the all.tar package, -t means to 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 It uses gzip compression to generate a gzip-compressed package named jpg.tar.gz

tar –cjf jpg.tar.bz2 *.jpg //Package all jpg files in the directory into jpg.tar Finally, compress it 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 After becoming jpg.tar, compress it with compress to generate a umcompress compressed package, named jpg.tar.Z

rar a jpg.rar *.jpg //Rar format compression, required First download rar for linux

zip jpg.zip *.jpg //Compression in zip format, you need to download zip for linux

Unzip

tar –xvf file.tar //Extract tar package

tar -xzvf file.tar.gz //Extract tar.gz

tar -xjvf file.tar.bz2 //Extract tar.bz2

tar –xZvf file.tar.Z //Extract tar.Z

unrar e file.rar //Extract rar

unzip file.zip //Extract zip

Summary

1. Use tar –xvf to decompress *.tar

2. Use gzip -d or gunzip to decompress *.gz

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

4, *.bz2 Use bzip2 -d or bunzip2 to decompress

5, *.tar.bz2 Use tar –xjf to decompress

6. *.Z Use uncompress to decompress

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

8, *.rar Use unrar e to decompress

9, * .zip Use unzip to decompress

Detailed explanation of tar command in Linux (reprinted information)

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, and the auxiliary option is auxiliary Used, 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 the file from the archive.

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 an existing file. 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 the present.

M Create multi-volume archive files for storage on several disks.

v Detailed reporting 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 the large number 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 summarizing various types of compressed files in detail, 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:

The tar command has many options (you can view it with man tar), but There are only a few commonly used options. Here are some examples:

# tar -cf all.tar *.jpg

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

# tar -rf all.tar *.gif

This command adds 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 lists all files in the all.tar package, -t means to list files

# tar - xf all.tar

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

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 use gzip compression, generate a gzip compressed package, the package name is all.tar.gz

# tar -xzf all.tar.gz

This command is to unpack the package generated above open.

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 below:

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

This command is to type all .jpg files into a tar package and use bzip2 compression, generate a bzip2 compressed package, the package name is all.tar.bz2

# tar -xjf all.tar.bz2

This command is to unpack the package generated above open.

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 use compress compresses, generates an uncompress compressed package, the package name is all.tar.Z

# tar -xZf all.tar.Z

This command is to unpack the package generated above Open

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

1) For files ending in .tar

tar -xf all.tar

2) For files ending in .gz

gzip -d all.gz

gunzip all.gz

3) For files ending in .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 in .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. I will 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 unzip 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 that RAR for Linux

is not free; then install:

# tar -xzpvf rarlinux-3.2.0.tar.gz

# cd rar

# make

This will install it. After installation, you will have rar and unrar. Two programs, rar is a compression program and unrar is a decompression program. They have many parameter options. Here is just a brief introduction. I will 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 already introduced it under linux tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar and other programs, you should already be able to use them to .tar, .gz, .tar.gz, .tgz, .bz2, .tar Ten types of compressed files such as .bz2, .Z, .tar.Z, .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, . Operate 10 types of compressed files such as tar.gz, .tgz, .bz2, .tar.bz2, .Z, .tar.Z, .zip, and .rar

More compression and decompression under Linux For detailed command analysis and related articles, please pay attention to the PHP Chinese website!


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