Linux入门教程(第19天)
最编程
2024-02-23 18:14:42
...
6.1 压缩打包介绍
家用带宽和机房带宽的区别:
- 家用的IP是动态的、不固定的,而机房的IP却是固定不变的
- 家用的上传和下载带宽也是不同的;而在机房中的带宽,下载和上传带宽都是相同的
- 举例:家用—>联通的百兆宽带,指的是下载的速度是一百兆,而上传一般只有10M到20M
常见压缩文件
- windows系统中,常见的压缩文件有 .rar, .zip, .7z
- linux系统中,常见的压缩文件有 .zip, .gz, .bz2, .xz, .tar.gz, .tar.bz2, .tar.xz
压缩后优势:
- 压缩的文件会变小很多
- 压缩后的文件带宽资源耗费也变小了
- 压缩后的文件会减少传输时间
6.2 gzip压缩工具
压缩和解压
- 压缩:在多个文件一起压缩的时候,只能压缩第一个文件
- 解压:多个文件可以在一起直接,比如bzip2 -d 1.txt.bz2 2.txt.bz2 这样会将两个文件都解压
gzip目录概要
- gzip 1.txt 压缩1.txt文件
- gzip -d 1.txt 或者 gunzip 1.txt.gz 解压1.txt文件的两种方法
- gzip -# 1.txt //#表示范围1-9,默认为6
- 不能压缩目录
- zcat 1.txt.gz
- gzip -c 1.txt > /root/1.txt.gz 压缩文件,并指定目录
- gunzip -c /root/1.txt.gz > /tmp/1.txt.new 解压文件,并指定
gzip压缩文件
- gzip 1.txt 压缩1.txt文件
- 首先切换到/tmp目录下,并新建一个目录d6z [root@localhost ~]# cd /tmp/ [root@localhost tmp]# ls 11.sh han.111 23.sh fstab [root@localhost tmp]# mkdir d6z
- 切换到/d6z目录下,并查找/etc目录下所有以conf结尾的文件 [root@localhost tmp]# cd d6z [root@localhost d6z]# find /etc/ -type f -name "*conf" 查找所有文件中,名字以conf结尾的文件 /etc/resolv.conf /etc/pki/ca-trust/ca-legacy.conf /etc/yum/pluginconf.d/fastestmirror.conf /etc/yum/pluginconf.d/langpacks.conf /etc/yum/protected.d/systemd.conf /etc/yum/version-groups.conf /etc/rdma/mlx4.conf /etc/rdma/rdma.conf
- 然后这文件查找出的文件内容追加到1.txt中,这个符号 {} 表示列出来的文件
- 多追加几次文件内容到1.txt中
[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt ; [root@localhost d6z]#
- 查看文件和大小 [root@localhost d6z]# ls 1.txt [root@localhost d6z]# du -sh 1.txt 3.2M 1.txt
这里多次追加会看到文件,du -sh 1.txt查看的文件数值不同,但在多次查看,文件大小会恢复正常。(跳转数值较大比,是因为这个文件本身存在很多空隙,最后在压缩并解压后,会发现大小会有不同)
- 压缩文件1.txt [root@localhost d6z]# gzip 1.txt [root@localhost d6z]# ls 1.txt.gz 会看到源文件消失了,变成了.gz的压缩文件 1.查看压缩文件大小 [root@localhost d6z]# du -sh 1.txt.gz 332K 1.txt.gz
gzip解压文件(两种方法)
- gzip -d 1.txt.gz 把1.txt文件解压出来
[root[@localhost](https://my.oschina.net/u/570656) d6z]# gzip -d 1.txt.gz
[root[@localhost](https://my.oschina.net/u/570656) d6z]# ls
1.txt
[root[@localhost](https://my.oschina.net/u/570656) d6z]# du -sh 1.txt
1.3M 1.txt
- gunzip 1.txt.gz 解压1.txt文件
[root@localhost d6z]# gunzip 1.txt.gz
[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# du -sh 1.txt
1.3M 1.txt
gzip指定压缩的级别
- gzip压缩的级别范围有1-9,默认是 6 级别,也可以指定压缩级别
- 9级别是压缩的最严谨,所耗费的CPU资源也最大(压缩的文件也是最小的)
- 压缩到一定级别后,到达极限后,会压缩不了
file命令,查看压缩后的文件
- 压缩后的文件变成了二进制文件,不能直接使用cat查看
- file命令,查看压缩的文件
- 格式 file 1.txt.gz
[root@localhost d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov 9 14:23:33 2017, max compression
这里会看到这是一个gzip的压缩数据,名称是1.txt,基于Unix平台,最后一次的更改时间,压缩的级别
zcat命令,查看压缩文件的内容
- zcat命令,查看压缩文件的内容
- 格式 zcat 1.txt.gz
- 这是先解压,后cat查看的
- 格式 zcat 1.txt.gz
gzip压缩文件,并指定目录
- gzip -c 1.txt > /tmp/1.txt.gz 压缩文件,并重定向目录和名称
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz
[root@localhost d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
gzip解压文件,并指定目录
- gunzip -c /tmp/1.txt.gz > /tmp/6dz/2.txt
- gzip -d -c /tmp/1.txt.gz > /tmp/6dz/2.txt
[root@localhost d6z]# gunzip -c /tmp/1.txt.gz >/tmp/d6z/2.txt
[root@localhost d6z]# ls
1.txt 2.txt
[root@localhost d6z]# gzip -c -d /tmp/1.txt.gz >/tmp/d6z/2.txt
[root@localhost d6z]# ls
1.txt 2.txt
1.txt和2.txt这两个文件大小相同(du -sh 1.txt 2.txt),行数形同(wc -l 1.txt 2.txt)
gzip不能压缩目录
6.3 bzip2压缩工具
bzip2目录概要
- bzip2 1.txt 或 bzip2 -z 1.txt 压缩文件
- bzip2 -d 1.txt.bz2 或 bunzip2 1.txt.bz2 解压文件
- bzip -# 1.txt 范围1-9,默认9
- 不能压缩目录
- bzcat 1.txt.bz2 查看压缩文件的内容(含义为:先解压,后查看)
- bzip2 -c 1.txt > /root/1.txt.gz 压缩文件,并重定向文件的目录和名称
- bzip2 -c -d /root/1.txt.bz2 > /tmp/1.txt.nws 解压文件,并重定向文件的目录和名称
bzip2压缩文件
- 安装bzip2包——>yum install -y bzip2
- bzip2 1.txt 压缩文件
[root@hf-01 d6z]# du -sh 1.txt
2.0M 1.txt
[root@hf-01 d6z]# bzip2 1.txt
[root@hf-01 d6z]# ls
1.txt.bz2
[root@hf-01 d6z]# du -sh 1.txt.bz2
168K 1.txt.bz2
bzip2解压文件(两种办法)
- bzip2 -d 1.txt 解压文件
[root@hf-01 d6z]# bzip2 -d 1.txt.bz2
[root@hf-01 d6z]# ls
1.txt
[root@hf-01 d6z]# du -sh 1.txt
1.5M 1.txt
- bunzip2 1.txt.bz2 解压文件
[root@hf-01 d6z]# bunzip2 1.txt.bz2
[root@hf-01 d6z]# ls
1.txt
bzip2压缩文件,并指定目录
- bzip2 -c 1.txt > /tmp/2.txt.bz2 压缩1.txt文件,并压缩到/tmp下2.txt.bz2
[root@hf-01 d6z]# bzip2 -c 1.txt > /tmp/2.txt.bz2
[root@hf-01 d6z]# du -sh /tmp/2.txt.bz2
168K /tmp/2.txt.bz2
bzip解压文件,并指定目录(两种方法)
- bzip2 -d -c /tmp/2.txt.bz2 > 4.txt 解压tmp目录下的文件,并解压到当前目录下,改名为4.txt
[root@hf-01 d6z]# bzip2 -d -c /tmp/2.txt.bz2 > 4.txt
[root@hf-01 d6z]# ls
1.txt 4.txt
[root@hf-01 d6z]# du -sh 4.txt
1.5M 4.txt
- bunzip2 1.txt.bz2 > 5.txt 解压文件,并解压到当前目录下,名称为5.txt
[root@hf-01 d6z]# bunzip2 1.txt.bz2 > 5.txt
[root@hf-01 d6z]# ls
1.txt 4.txt 5.txt
bzip2压缩级别
- bzip2默认压缩级别为 9
file查看压缩文件大小
- file命令,会知道压缩文件的类型,大小
[root@hf-01 d6z]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
- 若是文件名的格式被改错,或者不知道
- 在使用less、cat、head查看该文件的时候,会提示“该文件是二进制文件,是否继续查看呢”
- 这时候可以使用 file命令 去查看,会知道该文件的格式
bzcat查看压缩文件内容
- bzcat命令,查看压缩文件中的内容
- 格式 bzcat 1.txt.bz2
[root@hf-01 d6z]# bzcat 1.txt.bz2
查看的时候,需注意压缩文件是否为空,若为空,则看不到什么文件
6.4 xz压缩工具
xz目录概要
- xz 1.txt 或 xz -z 1.txt 压缩文件
- xz -d 1.txt.xz 或 unxz 1.txt.xz 解压文件
- xz -# 1.txt 1.txt //#表示范围1-9,默认为 6
- 不能压缩目录
- xzcat 1.txt.xz 查看压缩文件的内容
- xz -c 1.txt > /root/1.txt.xz 压缩文件,压缩到指定目录下,并修改名称
- xz -d -c /root/1.txt.xz > 1.txt.news 解压文件,解压到指定目录下,并修改名称
xz压缩文件
[root@hf-01 d6z]# xz 1.txt
[root@hf-01 d6z]# ls
1.txt.xz 4.txt 5.txt
[root@hf-01 d6z]# du -sh 1.txt.xz
60K 1.txt.xz
xz解压文件(两种方法)
- xz -d 1.txt.xz 解压文件
[root@hf-01 d6z]# xz -d 1.txt.xz
[root@hf-01 d6z]# ls
1.txt 4.txt 5.txt
[root@hf-01 d6z]# du -sh 1.txt
1.5M 1.txt
- unxz 1.txt.xz 解压文件
[root@hf-01 d6z]# unxz 1.txt.xz
[root@hf-01 d6z]# ls
1.txt 4.txt 5.txt
xz压缩文件,并压缩到指定目录
- xz -c 1.txt > /tmp/ha.txt.xz
[root@hf-01 d6z]# xz -c 1.txt > /tmp/ha.txt.xz
[root@hf-01 d6z]# du -sh /tmp/ha.txt.xz
60K /tmp/ha.txt.xz
xz解压文件,并解压到指定目录
- xz -d -c /tmp/ha.txt.xz > ./8.txt
- unxz -c /tmp/ha.txt.xz > ./8.txt
[root@hf-01 d6z]# unxz -c /tmp/ha.txt.xz > ./8.txt
[root@hf-01 d6z]# ls
1.txt 4.txt 5.txt 8.txt
[root@hf-01 d6z]# du -sh 8.txt
1.5M 8.txt
xzcat查看压缩文件
- xzcat /tmp/ha.txt.xz
xz压缩级别
- xz压缩级别是1-9,默认是 6 级别
压缩工具排序
- xz >bzip2 >gzip(这不是绝对的,关键还得看文件)
- xz 压缩最为严谨,bzip2稍次之,gzip排在最后
上一篇: 如何轻松使用tar工具解开POSIX格式的归档文件?
下一篇: 简单实用的tar解压缩命令汇总指南
推荐阅读
-
Linux 第 17 章
-
❤️ 新 Linux 零基础快速入门到精通 - 第 3 部分 ❤️
-
.NET Emit 入门教程:第 7 部分:实践项目 1:将 DbDataReader 变为实体
-
Linux 功能入门教程:基础操作
-
第19天:Linux 下的压缩和打包软件介绍
-
Linux Notes - 第 8 章 打包和压缩文件
-
第 8 章,[Linux] 文件和文件系统压缩、打包和备份
-
epoll简介及触发模式(accept、read、send)-epoll的简单介绍 epoll在LT和ET模式下的读写方式 一、epoll的接口非常简单,一共就三个函数:1. int epoll_create(int size);创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大。这个参数不同于select中的第一个参数,给出最大监听的fd+1的值。需要注意的是,当创建好epoll句柄后,它就是会占用一个fd值,在linux下如果查看/proc/进程id/fd/,是能够看到这个fd的,所以在使用完epoll后,必须调用close关闭,否则可能导致fd被耗尽。2. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);epoll的事件注册函数,它不同与select是在监听事件时告诉内核要监听什么类型的事件,而是在这里先注册要监听的事件类型。第一个参数是epoll_create的返回值,第二个参数表示动作,用三个宏来表示:EPOLL_CTL_ADD:注册新的fd到epfd中;EPOLL_CTL_MOD:修改已经注册的fd的监听事件;EPOLL_CTL_DEL:从epfd中删除一个fd;第三个参数是需要监听的fd,第四个参数是告诉内核需要监听什么事,struct epoll_event结构如下:struct epoll_event { __uint32_t events; /* Epoll events */ epoll_data_t data; /* User data variable */};events可以是以下几个宏的集合:EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭); EPOLLIN事件:EPOLLIN事件则只有当对端有数据写入时才会触发,所以触发一次后需要不断读取所有数据直到读完EAGAIN为止。否则剩下的数据只有在下次对端有写入时才能一起取出来了。现在明白为什么说epoll必须要求异步socket了吧?如果同步socket,而且要求读完所有数据,那么最终就会在堵死在阻塞里。 EPOLLOUT:表示对应的文件描述符可以写; EPOLLOUT事件:EPOLLOUT事件只有在连接时触发一次,表示可写,其他时候想要触发,那要先准备好下面条件:1.某次write,写满了发送缓冲区,返回错误码为EAGAIN。2.对端读取了一些数据,又重新可写了,此时会触发EPOLLOUT。简单地说:EPOLLOUT事件只有在不可写到可写的转变时刻,才会触发一次,所以叫边缘触发,这叫法没错的!其实,如果真的想强制触发一次,也是有办法的,直接调用epoll_ctl重新设置一下event就可以了,event跟原来的设置一模一样都行(但必须包含EPOLLOUT),关键是重新设置,就会马上触发一次EPOLLOUT事件。1. 缓冲区由满变空.2.同时注册EPOLLIN | EPOLLOUT事件,也会触发一次EPOLLOUT事件这个两个也会触发EPOLLOUT事件 EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);EPOLLERR:表示对应的文件描述符发生错误;EPOLLHUP:表示对应的文件描述符被挂断;EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里3. int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);等待事件的产生,类似于select调用。参数events用来从内核得到事件的集合,maxevents告之内核这个events有多大,这个maxevents的值不能大于创建epoll_create时的size,参数timeout是超时时间(毫秒,0会立即返回,-1将不确定,也有说法说是永久阻塞)。该函数返回需要处理的事件数目,如返回0表示已超时。-------------------------------------------------------------------------------------------- 从man手册中,得到ET和LT的具体描述如下EPOLL事件有两种模型:Edge Triggered (ET)Level Triggered (LT)假如有这样一个例子:1. 我们已经把一个用来从管道中读取数据的文件句柄(RFD)添加到epoll描述符2. 这个时候从管道的另一端被写入了2KB的数据3. 调用epoll_wait(2),并且它会返回RFD,说明它已经准备好读取操作4. 然后我们读取了1KB的数据5. 调用epoll_wait(2)......Edge Triggered 工作模式:如果我们在第1步将RFD添加到epoll描述符的时候使用了EPOLLET标志,那么在第5步调用epoll_wait(2)之后将有可能会挂起,因为剩余的数据还存在于文件的输入缓冲区内,而且数据发出端还在等待一个针对已经发出数据的反馈信息。只有在监视的文件句柄上发生了某个事件的时候 ET 工作模式才会汇报事件。因此在第5步的时候,调用者可能会放弃等待仍在存在于文件输入缓冲区内的剩余数据。在上面的例子中,会有一个事件产生在RFD句柄上,因为在第2步执行了一个写操作,然后,事件将会在第3步被销毁。因为第4步的读取操作没有读空文件输入缓冲区内的数据,因此我们在第5步调用 epoll_wait(2)完成后,是否挂起是不确定的。epoll工作在ET模式的时候,必须使用非阻塞套接口,以避免由于一个文件句柄的阻塞读/阻塞写操作把处理多个文件描述符的任务饿死。最好以下面的方式调用ET模式的epoll接口,在后面会介绍避免可能的缺陷。 i 基于非阻塞文件句柄 ii 只有当read(2)或者write(2)返回EAGAIN时才需要挂起,等待。但这并不是说每次read时都需要循环读,直到读到产生一个EAGAIN才认为此次事件处理完成,当read返回的读到的数据长度小于请求的数据长度时,就可以确定此时缓冲中已没有数据了,也就可以认为此事读事件已处理完成。Level Triggered 工作模式相反的,以LT方式调用epoll接口的时候,它就相当于一个速度比较快的poll(2),并且无论后面的数据是否被使用,因此他们具有同样的职能。因为即使使用ET模式的epoll,在收到多个chunk的数据的时候仍然会产生多个事件。调用者可以设定EPOLLONESHOT标志,在 epoll_wait(2)收到事件后epoll会与事件关联的文件句柄从epoll描述符中禁止掉。因此当EPOLLONESHOT设定后,使用带有 EPOLL_CTL_MOD标志的epoll_ctl(2)处理文件句柄就成为调用者必须作的事情。然后详细解释ET, LT:LT(level triggered)是缺省的工作方式,并且同时支持block和no-block socket.在这种做法中,内核告诉你一个文件描述符是否就绪了,然后你可以对这个就绪的fd进行IO操作。如果你不作任何操作,内核还是会继续通知你的,所以,这种模式编程出错误可能性要小一点。传统的select/poll都是这种模型的代表.ET(edge-triggered)是高速工作方式,只支持no-block socket。在这种模式下,当描述符从未就绪变为就绪时,内核通过epoll告诉你。然后它会假设你知道文件描述符已经就绪,并且不会再为那个文件描述符发送更多的就绪通知,直到你做了某些操作导致那个文件描述符不再为就绪状态了(比如,你在发送,接收或者接收请求,或者发送接收的数据少于一定量时导致了一个EWOULDBLOCK 错误)。但是请注意,如果一直不对这个fd作IO操作(从而导致它再次变成未就绪),内核不会发送更多的通知(only once),不过在TCP协议中,ET模式的加速效用仍需要更多的benchmark确认(这句话不理解)。在许多测试中我们会看到如果没有大量的idle -connection或者dead-connection,epoll的效率并不会比select/poll高很多,但是当我们遇到大量的idle- connection(例如WAN环境中存在大量的慢速连接),就会发现epoll的效率大大高于select/poll。(未测试)另外,当使用epoll的ET模型来工作时,当产生了一个EPOLLIN事件后,读数据的时候需要考虑的是当recv返回的大小如果等于请求的大小,那么很有可能是缓冲区还有数据未读完,也意味着该次事件还没有处理完,所以还需要再次读取: 这里只是说明思路(参考《UNIX网络编程》) while(rs) {buflen = recv(activeevents[i].data.fd, buf, sizeof(buf), 0);if(buflen < 0){// 由于是非阻塞的模式,所以当errno为EAGAIN时,表示当前缓冲区已无数据可读// 在这里就当作是该次事件已处理处.if(errno == EAGAIN)break; else return; }else if(buflen == 0) { // 这里表示对端的socket已正常关闭. } if(buflen == sizeof(buf) rs = 1; // 需要再次读取 else rs = 0; } 还有,假如发送端流量大于接收端的流量(意思是epoll所在的程序读比转发的socket要快),由于是非阻塞的socket,那么send函数虽然返回,但实际缓冲区的数据并未真正发给接收端,这样不断的读和发,当缓冲区满后会产生EAGAIN错误(参考man send),同时,不理会这次请求发送的数据.所以,需要封装socket_send的函数用来处理这种情况,该函数会尽量将数据写完再返回,返回-1表示出错。在socket_send内部,当写缓冲已满(send返回-1,且errno为EAGAIN),那么会等待后再重试.这种方式并不很完美,在理论上可能会长时间的阻塞在socket_send内部,但暂没有更好的办法. ssize_t socket_send(int sockfd, const char* buffer, size_t buflen) { ssize_t tmp; size_t total = buflen; const char *p = buffer; while(1) { tmp = send(sockfd, p, total, 0); if(tmp < 0) { // 当send收到信号时,可以继续写,但这里返回-1. if(errno == EINTR) return -1; // 当socket是非阻塞时,如返回此错误,表示写缓冲队列已满, // 在这里做延时后再重试. if(errno == EAGAIN) { usleep(1000); continue; } return -1; } if((size_t)tmp == total) return buflen; total -= tmp; p += tmp; } return tmp; } 二、epoll在LT和ET模式下的读写方式 在一个非阻塞的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就是EWOULDBLOCK) 从字面上看, 意思是: * EAGAIN: 再试一次 * EWOULDBLOCK: 如果这是一个阻塞socket, 操作将被block * perror输出: Resource temporarily unavailable 总结: 这个错误表示资源暂时不够, 可能read时, 读缓冲区没有数据, 或者, write时,写缓冲区满了 。 遇到这种情况, 如果是阻塞socket, read/write就要阻塞掉。 而如果是非阻塞socket, read/write立即返回-1, 同 时errno设置为EAGAIN. 所以, 对于阻塞socket, read/write返回-1代表网络出错了. 但对于非阻塞socket, read/write返回-1不一定网络真的出错了. 可能是Resource temporarily unavailable. 这时你应该再试, 直到Resource available. 综上, 对于non-blocking的socket, 正确的读写操作为: 读: 忽略掉errno = EAGAIN的错误, 下次继续读 写: 忽略掉errno = EAGAIN的错误, 下次继续写 对于select和epoll的LT模式, 这种读写方式是没有问题的. 但对于epoll的ET模式, 这种方式还有漏洞. epoll的两种模式 LT 和 ET
-
小型白色自制 Linux 开发板(第 2 季 V3 章) I. 更换内核,重新开始
-
使用 GAS 16 位汇编器在 Linux 下编写汇编程序 第 1 部分:环境设置