In Linux, there are many compression commands. Using these compression commands, you can easily download large files from the network. At the same time, we know that the Linux File Extension has no special significance. However, because there are many compression commands in Linux, in order to facilitate memory, some special naming methods will be performed on the files compressed by these commands. This article will mainly record common Linux compression commands.
I. Principles of File compression
Compression Technology, in short, can be imagined as: in fact, there is a lot of "space" in the file, not completely filled, the "compression" technology fills up these "spaces" to reduce the usage of the entire file.
Currently, all the operating system data is measured in bytes. However, in fact, the smallest measurement unit in the computer should be bit ), we know that 1 byte = 8 bit. So how do we record some data?
For example, we only need to remember a number, that is, the number 1. Considering the so-called binary of a computer, 1 occupies 1 bit on the far right, the other 7 bits will be automatically filled with 0! In fact, in this example, the seven bits should be blank! However, to meet the needs of the current operating system data access, the data will be converted to a byte type for record. The compression technology is to "drop" unused space.
After File compression, the disk usage and file size can be reduced. However, these "compressed files" cannot be directly used by the operating system. Therefore, to use the compressed file data, it must be "restored" to an uncompressed image, which is "decompressed ".
Ii. Common Linux compression commands
In Linux, most of the compressed file extensions are :【*. tar, * .tar.gz ,*. tgz ,*. gz ,*. Z ,*. bz2], why are there so many extensions?
This is because Linux supports many compression commands, and different commands use different compression technologies. Of course, files cannot be compressed or decompressed to each other. Therefore, when downloading a compressed file, you need to know which compression command is used to decompress the file. Although the attributes of a Linux file are basically irrelevant to the file name, the proper extension is necessary to help us identify the file. The following extensions are commonly used compressed files in Linux:
*. Z files compressed by the compress program; // This compression command is not supported by default in mainstream Linux versions, because the gzip command has replaced the compress command.
*. Files compressed by the gz gzip program ;*. bz2 bzip2 compressed file ;*. the data packaged by the tar program has not been compressed; * .tar.gz tar package files, which are compressed by the gzip * .tar.bz2 tar package files, and compressed by bzip2
Next, let's take a look at some common compression commands in Linux.
1. gzip and zcat
Gzip is the most widely used compression command! Currently, gzip can be used to decompress files compressed by compress, zip, gzip, and other software. The compressed file created using the gzip command is the *. gz file name.
[Root @ xiaoluo ~] # Gzip [-cdtv #] file name [root @ xiaoluo ~] # Zcat file name .gz options and parameters:-c: Output compressed data to the screen, which can be processed through data stream redirection;-d: extracted parameter;-t: it can be used to check the consistency of a compressed file to see if there are any errors in the file;-v: It can display the compression ratio of the original file/compressed file and other information;-#: Compression level (1 ~ 9),-1 is the fastest, but the compression ratio is the worst,-9 is the slowest, but the compression ratio is the best! The default value is-6 instance 1: copy/etc/passwd to the/home directory, and use gzip to compress [root @ xiaoluo home] # cp/etc/passwd [root @ xiaoluo home] # ls-l-rw-r --. 1 root 2679 April 26 19:37 passwd [root @ xiaoluo home] # gzip passwd [root @ xiaoluo home] # ls-l
-Rw-r --. 1 root 1066 April 26 19:37 passwd.gz
# At this time, we will find that the file passwd we just copied has become the compressed file passwd.gz.
Example 2: Read the file content of instance 1
[Root @ xiaoluo home] # zcat passwd.gz
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
Adm: x: 3: 4: adm:/var/adm:/sbin/nologin
.................................... N words are omitted here ......................................
# Now we can see the file content after passwd.gz is decompressed
Example 3: Decompress instance 1 files
[Root @ xiaoluo home] # gzip-d passwd.gz
[Root @ xiaoluo home] # ls-l
-Rw-r --. 1 root 2679 19:37 passwd
Finally, we can see that the new passwd.gz file was decompressed into the passwd file.
Example 4: compress the passwd file extracted from instance 3 with the best package and keep the original file
[Root @ xiaoluo home] # gzip-c9 passwd> passwd.gz
[Root @ xiaoluo home] # ls-l passwd *
-Rw-r --. 1 root 2679 19:37 passwd
-Rw-r --. 1 root 1066 April 26 19:52 passwd.gz
Note:When you use gzip for compression, the original file will be compressed into a. gz file name by default, and the original file will no longer exist. Therefore, if you want to retain the original file, you can use the method of instance 4 for compression.
We know that the cat command can be used to read the content of a text file. What command should we use to read the data content of a compressed file? The zcat command of the above instance.
2.bzip2, bzcat
If gzip is used to replace the compress command, the bzip2 command is another upgraded version of gzip.
[Root @ xiaoluo ~] # Bzip2 [-cdkzv #] file name [root @ xiaoluo ~] # Bzcat file name. bz2 option and parameter:-c: output the data generated during the compression process to the screen-d: extracted parameter-k: Retain the original file, instead of deleting the original file-z: the compressed parameter-v: displays the compression ratio of the original file/compressed file and other information;-#: Same as gzip, all are parameters for calculating the compression ratio.-9 is the best, and-1 is the fastest!
Example 1: compress the passwd.
[Root @ xiaoluo home] # bzip2-z passwd
[Root @ xiaoluo home] # ls-l passwd *
-Rw-r --. 1 root 1102 April 26 19:37 passwd.bz2
# Export passwdhas been transformed into the passwd.bz2 File
Example 2: Decompress instance 1 files
[Root @ xiaoluo home] # bzip2-d passwd.bz2
[Root @ xiaoluo home] # ls-l passwd *
-Rw-r --. 1 root 2679 19:37 passwd
Example 3: compress instance 2 and keep the original file
[Root @ xiaoluo home] # bzip2-zk passwd
[Root @ xiaoluo home] # ls-l passwd *
-Rw-r --. 1 root 2679 19:37 passwd
-Rw-r --. 1 root 1102 April 26 19:37 passwd.bz2
#-K can retain the original file or use the bzip2-c passwd> passwd.bz2 command.
Use gzipand the extension name is automatically .gz. When bzip2is used, the extension name is automatically changed to .bz2. Similarly, if we need to read the data content of the bzip2 compressed file, use the bzcat command.
3. tar
We should have seen a lot about this command. This command was usually used for decompression.
Most of the commands mentioned above can only be compressed for a single file. Although gzip and bzip2 can also be compressed for directories, these two commands compress directories by compressing all files in the directory! Unlike Windows systems, you can use a compression software similar to WinRAR to [Package A lot of data into a file.
This command is called a packaging command to package multiple files or directories into a large file! Is there such a command in Linux? Of course, the tar command is the packaging command we need. Tar can package multiple directories or files into a large file, and compress the file at the same time through gzip/bzip2 support.
[Root @ xiaoluo ~] # Tar [-j |-z] [cv] [file name created by-f] filename... <= package and compress [root @ xiaoluo ~] # Tar [-j |-z] [TV] [file name created by-f] <= view the file name [root @ xiaoluo ~] # Tar [-j |-z] [xv] [-f file name] [-C Directory] <= decompression option and parameter:-c: Create a packaging file, you can use-v to check the packaged file name (filename)-t: to view the files in the tarfile.
-X: unpacks or decompresses data. It can be used with-C (uppercase) to unpacks data in a specific directory. Note that-c,-t, -x cannot appear in a string of command columns at the same time. -J: Use bzip2 to compress/decompress the file name. * .tar.bz2-z: Use gzip to compress/decompress the file name. * .tar.gz-v: during compression/decompression, the file name being processed is displayed
-F filename:-f is followed by the file name to be processed immediately! -F is recommended to write an option-C directory separately: This option is used for decompression. to decompress the option in a specific directory, you can use this option. -P: retains the original permissions and attributes of backup data, which is often used in backup (-c) important configuration files.
-P: retain the absolute path, that is, allow the backup data to contain the root directory; -- exclude = FILE: Do not package the FILE during compression.
Example 1: package all the files in the/etc directory into/home/etc.tar.
[Root @ xiaoluo home] # tar-cvf etc.tar/etc --> this command is only used for packaging without compression.
[Root @ xiaoluo home] # tar-zcvf etc.tar/etc --> compress the package with gzip
[Root @ xiaoluo home] # tar-jcvf etc.tar/etc --> compress the package with bzip2.
# Note that the file name after parameter f is obtained by ourselves, and we use. tar as the identifier.
# If the z parameter is added, .tar.gz or. tgz is used to represent the compressed tar file in gzip.
# If the j parameter is added, .tar.bz2 is used as the extension.
Example 2: Describe the content in the etc.tar file on the role
[Root @ xiaoluo ~] # Tar-ztvf etc.tar
# When we use gzip to compress files in the tarfile,
# Add the z parameter.
Example 3: extract the etc.tar file to the current directory
[Root @ xiaoluo home] # tar-zxvf etc.tar
# Now we can find that there is an etc folder in the current directory, which is the file we decompressed.
Example 4: If I just want to extract the etc/passwd from etc.tar
[Root @ xiaoluo home] # tar-zxvf etc.tar etc/passwd
# I can use tar-ztvf to check the file name in the tarfile. If you only need one file,
# This method can be used. Notice! The root directory/In etc.tar.gz is missing
Instance 5: Back up all files in/etc and save their Permissions
[Root @ xiaoluo home] # tar-zxvpf etc.tar.gz/etc/
# This-p attribute is very important, especially when you want to retain the attributes of the original file
Example 6: In/home, a new file is backed up when compared
[Root @ xiaoluo home] # tar-N '2017/01'-zcvf home.tar/home
Example 7: I want to back up all files in/home and/etc, but do not back up files in the/home/xiaoluo directory.
[Root @ xiaoluo home] # tar -- exclude/home/xiaoluo-zcvf myfile.tar.gz/home/*/etc
Through some examples above, we can see that the tar command is a multi-purpose compression command. We can use the tar command to complete the backup function. Since we usually name files after tar integration as *. tar, if you still use the gzip compression attribute during the packaging process, the file will be named * .tar.gz.
This article mainly records commonly used File compression commands in Linux, and will continue to record and learn about Linux in the future !!!
You can clickBottom right cornerTo evaluate the content of the article.Follow buttonTo follow the latest developments in my blog. If the article content is helpful to you, do not forget to clickRecommendation buttonIf you have any questions about the content of the article, you can contact me by comment or by email: 501395377@qq.com/lzp501395377@gmail.com