Linux compression packaging

1. What is compression

Before talking about compression tools, let's first explain the principle of compression. The computer system we currently use is measured in bytes, but in fact, the smallest unit of measurement in a computer is bits. We know that 1byte=8bits. How do we record when we record a number 1?
We said before that a byte is 8bits and we can understand it as follows:

Mouth Mouth Mouth Mouth Mouth Mouth Mouth

A space represents a bit, 1 byte is8 spaces, When we are storing the number 1, we count according to the secondary system,What is stored here is 00000001, 7 0s and 1 1, in fact, the first 7 0s are empty, but because of the access method of our operating system, it must be stored in 8-bit units, which will cause some space It is not filled up. Compression is to throw out the unused space, so that the space occupied by the file becomes smaller. This is the compression technology.
Decompression technology is to restore the compressed data to an uncompressed state.
The compression ratio refers to the ratio of the disk space occupied by the file after compression to that before compression.
Our common website data transmission generally uses compression technology. The data is compressed data used in the network transmission process. When the compressed data reaches the user host, it is decompressed and then displayed.

2. Compression and decompression commands

  • 1).zip and uzip commands

(The zip and unzip commands are mainly used to process zip packages)

-d Delete the specified file from the compressed file
-f The effect of the parameter is similar to that of specifying the "-u" parameter, but it not only updates existing files. If some files do not exist in the compressed file, using this parameter will add them to the compressed file.
-u Replace newer files into compressed files
-j Only save the file name and its content, not any directory name
-r Recursive processing, processing all files and subdirectories in the specified directory together
-v Display instruction execution process or display version information
-and Save the symbolic link directly instead of the file pointed to by the link. This parameter is only valid under systems such as UNIX
-<Compression efficiency> The compression efficiency is a value between 1-9

Note: Compression ratio:The effect name describing the compressed file is the ratio of the size of the file after compression to the size before compression.
For example: after compressing a 100m file to 90m, the compression rate is 90/100*100%=90%,The compression rate is generally as small as possible, But the smaller the pressure, the longer the decompression time.
The greater the compression efficiency (compressionefficiency), the smaller the compressed file, and the longer the decompression time.

Example 1: compressed file install.log
Insert picture description here
Example 2: compression efficiency, and 9, respectively 3
Insert picture description here
Example 3: The test of all files and folders of all directory files compressed into test.zip, -r recursively expressed compress all the files in the directory
Insert picture description here
Example 4: Unzip lin.zipInsert picture description here

  • 2).gzip and gunzip commands

(The gzip command is used to compress files. gzip is a widely used compression program. After the file is compressed, there will be multiple ".gz" extensions behind its name.
gzip is a commonly used one in Linux systems to compress and decompress files The compressed command is both convenient and easy to use. Not only can gzip be used to compress large and less used files to save disk space, it can also be used with the tar command to form a more popular compressed file format in the Linux operating system. According to statistics, The gzip command has a compression ratio of 60% to 70% for text files.
)

-a Use ASCII text mode
-d Unzip the compressed file
-f Compress files forcibly. Ignore whether the file name or hard link exists and whether the file is a symbolic link
-l List information about compressed files
-L Display version and copyright information
-n When compressing files, do not save the original file name and time stamp
-N When compressing files, save the original file name and time stamp
-r Recursive processing, processing all files and subdirectories in the specified directory together
-S or <compressed suffix string> or ----suffix<compressed suffix string> Change the compressed suffix string
-v Show instruction execution process
-t Test whether the compressed file is correct
-<Compression efficiency> The compression efficiency is a value between 1-9, the default value is "6", the higher the value specified, the higher the compression efficiency

Example 1: Compress each file in the test directory into a .gz file
Here is the quote
Example 2: Decompress the above file and list the detailed information
In fact, gunzip is a hard link to gzip, so whether it is compression or decompression, it can be done separately through the gzip command
Insert picture description here

  • 3).bzip2 and bunzip2 commands

(The bzip2 command is similar to the gzip command. It can only compress (or decompress) files. For a directory, you can only compress (or decompress) all files in the directory and subdirectories. When the compression task is completed, a Compressed package with ".bz2" suffix )

-c Send the results of compression and decompression to standard output
-d Perform decompression
-f or -force When bzip2 compresses or decompresses, if the output file has the same name as the existing file, the existing file will not be overwritten by default. To overwrite. Please use this parameter
-k After bzip2 is compressed or decompressed, the original file will be deleted. To keep the original file, use this parameter
-s Reduce memory usage during program execution
-t Test the integrity of .bz2 compressed files
-v Display detailed information when compressing or decompressing files
-with Enforce compression
-V Display version information

Example 1: Compress the file file
Insert picture description here
Example 2: Decompress the file file
This decompression is similar to gzip
Insert picture description here

4).xz and unxz commands

xz [option] … FILE…

-d unzip
-# Specify the compression ratio, the default is 6, the larger the number, the greater the compression ratio (1-9)
-k Keep original file

Three. Pack and archive commands

First, we must clarify two concepts: packaging and compression.
Packaging refers to turning a large number of files or directories into a total file;
== Compression is to turn a large file into a small file through some compression algorithms. ==
Why distinguish these two concepts? This is because many compression programs in Linux can only compress a file, so when you want to compress a large number of files, you have to pack the large number of files into a package (tar command), and then use The compression program performs compression (gzipbzip2 command).

1).tar command

tar命令可以为linux的文件和目录创建档案。利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件。tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案。利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。

-c 建立压缩档案
-x 解压
-t 查看内容
-r 向压缩归档文件末尾追加文件
-u 更新原压缩包中的文件
-A 追加tar文件至归档

这六个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。

-z 有gzip属性的
-j 有bz2属性的
-J 有xz属性的
-Z 有compress属性的
-v 显示所有过程
-O 将文件解压到标准输出
-N [date file] somedir

-f:必须的选项,这个参数是最后一个参数,后面要接的是压缩文件名

例1:打包文件file1和file2,仅打包不压缩
Here is the quote
例2:先将file文件打包成lin1.tar,并且执行gzip压缩
Insert picture description here
tar -jcvf lin2.tar.bz2 file
tar -Jcvf lin3.tar.xz file

这两指令与上述图片方法相同,只是压缩文件格式不同(参照上述图片)

在 文件夹当中,比某个日期新的文件才备份
Insert picture description here

四.转换或拷贝文件dd命令

用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。

  • dd if=/dev/zero of=/tmp/file bs=1K count=10
  • if:标准输入:输入的文件名,指定源文件
  • of:标准输出,输出的文件名,指定目的文件
  • count:拷贝多个块
  • bs:指定输入输出块大小
ibs:读入块
obs:输出块
  • skip=* :从输入的文件跳过多少个块后复制
  • seek=* :从输出的文件跳过多少个块后复制
  • conv= *:用指定的参数转换文件

conv=* 参数:

ascii ascii:转换ebcdic为ascii
ebcdic 转换ascii为ebcdic
ibm ibm:转换ascii为alternateebcdic
block 把每一行转换为长度为cbs,不足部分用空格填充
unblock 使每一行的长度都为cbs,不足部分用空格填充
lcase 把大写字符转换为小写字符
ucase 把小写字符转换为大写字符
swab 交换输入的每对字节
noerror 出错时不停止
notrunc 不截短输出文件
sync 将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

例1:将本地的/dev/sda整盘备份到/dev/sdb(备份磁盘)
[root@localhost ~]#dd if=/dev/sda of=/dev/sdb

例2:将/dev/sda全盘数据备份到指定路径的file文件中(备份磁盘到文件)
[root@localhost ~]#dd if=/dev/sda of=/tmp/file

[root@localhost ~]#dd if=/dev/sda | gzip > /tmp/file.gz

例3:将备份文件恢复到指定磁盘
[root@localhost ~]#dd if=/tmp/file of=/dev/sdd

例4:备份与恢复MBR
备份磁盘开始的512字节大小的MBR信息到指定文件
[root@localhost ~]#dd if=/dev/sdd of=/tmp/file count=1 bs=512
恢复:(将备份的MBR信息写道磁盘开始部分)
[root@localhost ~]#dd if=/tmp/file of=/dev/had

例5:拷贝内存文件
[root@localhost ~]#dd if=/dev/mem of=/tmp/file bs=1m count=1024

例6:测试硬盘的读写速率
[root@localhost ~]#dd if=/dev/zero of=/tmp/file bs=100K count=1024

例7:测试硬盘的最佳块大小,1K 4K 8 K 16K
[root@localhost ~]#dd if=/dev/zero of=/tmp/file bs=1024 count=1000000 [root@localhost ~]#dd if=/dev/zero of=/tmp/file bs=4096 count=250000 [root@localhost ~]#dd if=/dev/zero of=/tmp/file bs=8192 count=125000

例8:大写转换此文件
[root@localhost ~]#dd if=/tmp/file of=/tmp/file1 conv=ucase

五.cpio备份命令

cpio是用来建立,还原备份档案的工具程序,它可以加入,解开cpio,img或tra备份档内的文件

Common operating modes:
1). Backup: cpio -ocvB> file
2). Restore: cpio -ivcdu> file
3). View: cpio -ivct> file

-O Copy files to a file or device
-c Storage method
-v Show stored procedure
-B Change the input/output block size to 5210 Bytes
-i Copy from a file or device
-d Create directory automatically
-u Automatically overwrite old files with new files
-t View the contents of a file or device

Example 1: Write all data on the system to the tape drive
[root@localhost ~]# find / -print | coip -covb>dev/st0

Guess you like

Origin blog.csdn.net/qq_44944641/article/details/104939438