web123456

Detailed explanation of the linux compression (decompression) command

Detailed explanation of the linux compression (decompression) command
Note: This article is referenced from the Internet, but has been modified by the author
1.tar command
tar can create archives for files and directories. With tar, users can create archives (backup files) for a specific file, or change files in the archive, or add new files to the archive. tar was originally used to create archives on tapes, and 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 easy network transmission. tar on Linux is GNU version.
grammar:tar [main option + secondary option] file or directory
When using this command, the main option is a must. It tells tar what to do. The auxiliary option is used as an 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, you need to 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 found that there is another directory or some files that have forgotten to backup. At this time, you can use this option to append the forgotten directory or file to the backup file.
t List the contents of the archive file and see which files have been backed up.
u Update the file. That is to say, replace the original backup file with the newly added 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 file.
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 preset value is 20 (20*512 bytes).
f Use archive files or devices,This option is usually a must
k Save the existing file. For example, when we restore a file, we encounter the same file during the restoration process, and it will not be overwritten.
m When restoring files, set the modification time of all files to now.
M Creates multi-volume archive files for storage on several disks.
v Report the file information processed by tar in detail. Without this option, tar will not report file information.
wCheck is required for each step.
z Use gzip to compress/decompress files. After adding this option, you can compress the archive files, but you must also use this option to decompress them when restoring.

2. Analysis of compressed files under Linux

For those who are just getting involved in Linux, they will definitely give Linux a lot of file names to make them dizzy. Let’s not talk about it, just like 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.rar. But Linux is different. It has many compressed file names such as .gz, ., tgz, bz2, .Z, .tar, etc. In addition, .zip and .rar under Windows can also be used under Linux, but there are too few people using .zip and .rar in Linux. This article will summarize these common compressed files. I hope you won’t be dizzy next time you encounter these files :)

Before specifically summarizing various 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 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 lot of files, you have to use another tool to first type the pile of files into a package, and then compress the original compression program.
The most commonly used package program in Linux is tar. We often call the packages made using the tar program. The commands of the tar package file 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 see it with man tar), but there are only a few commonly used options. Let’s give an example below:
# tar -cf *.jpg
This command is to type all .jpg files into a package called. -c means to generate a new package, and -f specifies the file name of the package.
# tar -rf *.gif
This command adds all .gif files to the package. -r means adding files.
        # tar -uf
This command updates the original tar package file, and -u means to update the file.

# tar -tf
This command lists all files in the package, and -t means listing files
# tar -xf
This command is to unpack all files in the package, and -x 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 that tar can call other compressed programs while packaging or unpacking, such as calling gzip, bzip2, etc.
1) tar calls gzip
gzip is a compression program developed by GNU organization. The files ending with .gz are the result 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 *.jpg
This command is to type all .jpg files into a tar package, and compress them with gzip to generate a gzip compressed package with the package name
# tar -xzf
This command untie the package generated above.
2) tar calls bzip2
bzip2 is a compression program with stronger compression capabilities. The files ending with .bz2 are the result of bzip2 compression. The decompression program opposite to bzip2 is bunzip2. Use the -j parameter in tar to call bzip2. Let’s give an example below:
# tar -cjf .bz2 *.jpg
This command will type all .jpg files into a tar package, and call bzip2 to compress, generating a bzip2 compressed package, with the package name .bz2
# tar -xjf .bz2
This command untie the package generated above.
3) tar calls compress

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

# tar -cZf *.jpg
This command will type all .jpg files into a tar package, and call compress to generate an uncompress compressed package, with the package name
# tar -xZf
This command unwraps the package generated above
With the above knowledge, you should be able to unpack a variety of compressed files. Here is a summary of the compressed files of the tar series:
1) For files ending with .tar
tar -xf
2) For files ending with .gz
gzip -d
gunzip
3) For .tgz or ending files
tar -xzf
tar -xzf
4) For files ending with .bz2
bzip2 -d all.bz2
bunzip2 all.bz2
5) For files ending with tar.bz2
tar -xjf .bz2
6) For files ending with .Z
uncompress
7) For the ending file
tar -xZf
In addition, for common compressed files .zip and .rar under Window, Linux also has corresponding methods to decompress them:
1) For .zip
Zip and unzip programs are provided under Linux. Zip is a compressed program and unzip is an unzip program. There are many parameter options, so here is a brief introduction, and I will give an example to illustrate their usage:
# zip *.jpg
This command compresses all .jpg files into a zip package

      Note: If the folder you want to compress is a folder, add the -r parameter to indicate that recursive compression is called,like:

      zip -r temp

        # unzip
This command decompresses all files in it

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
Not free; then install:
# tar -xzpvf rarlinux-3.2.
# cd rar
# make
This is done. After installation, there are two programs: rar and unrar. rar is the compression program and unrar is the decompression program. There are many parameter options, so here is a brief introduction, and I will give an example to illustrate their usage:
# rar a all *.jpg
This command compresses all .jpg files into a rar package named. The program will automatically append the .rar extension to the package name.
# unrar e
This command decompresses all files in it
At this point, we have introduced programs such as tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar, etc. under Linux. You should be able to use them to decompress 10 compressed files such as .tar, .gz, ., .tgz, .bz2, .tar.bz2, .Z, ., .zip, and .rar. In the future, you should not worry about downloading a software and not knowing how to unzip it in Linux. Moreover, the above method is basically effective for Unix.
This article introduces the compressed programs tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar and other programs under Linux, as well as how to use them to operate 10 compressed files such as .tar, .gz, ., .tgz, .bz2, .tar.bz2, .Z, .zip, and .rar.