8

Linux下最常用的10个文件压缩工具

 3 years ago
source link: http://www.cnblogs.com/lemon-le/p/14268584.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

jY3Q7zi.gif!mobile

作者简介

李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。公众号:运维汪(ID:Leeeee_Li)。

一、前言

作为运维人员,经常会遇到“小李啊,帮我取一下今天的日志”,因此在Linux服务器上压缩包、解压包是经常的事情,但是就我个人而言,经常会遇到很多不一样的压缩解压工具,最常用的是tar。虽然经常用,但是还是很多参数记不住,当然不用去记住每一个参数,--help就行。在十万火急的情况下,一个解压还要去google一下,好像也不合理,因此记住常用的几个还是有必要的,再同样的技术水平情况下,那就只能比速度了。

二、Linux下最常用的压缩工具

1、tar

1)压缩

tar -zcvf too.tar.gz too

2)解压

#.tar.gz     
tar -zxvf  too.tar.gz
 
#.tar.gz2    
tar -jxvf   too.tar.gz2

3)排除某个目录打包

把too目录打包成too.tar.gz,除logs目录;注这里的too/logs后面不能加/,如果加的话还是会打包进去。

tar -czvf too.tar.gz --exclude=too/logs too

4)只打包某个目录,而不是全部打包进去

加-C参数, 这样的话可以只打包api3.0 ,而不会从/usr开始一个一个目录都打包进去

tar -zcf api3.0_`date +%Y%m%d%H%M%S`.tar.gz -C /usr/local/tomcat/webapps api3.0

2、gzip

gzip工具是Linux中最流行、最快的文件压缩工具,Gzip工具保留原始文件名称压缩文件的扩展名.gz和时间戳。

1)打包

gzip filename

2)解压

gzip -d filename # 打包的文件会被删除

3、bzip2

Bzip2实用程序执行更快的gzip,它压缩文件和文件夹更紧凑。压缩文件时需要更多的内存,为了减少内存消耗,在选项中通过-s标志。

1)压缩

bzip2 examplefile or bzip2 -s examplefile

2)解压

bzip2 -d examplefile.bz2 or bunzip2 examplefile.bz2

3)详细说明

bzip2 -v examplefile

4、Lzma

Lzma是一种压缩工具,与zip或tar类似,但与bzip相比,它的执行速度更快,虽然lzma是一个强大的工具,但它在Linux用户中并不流行。

1)压缩

lzma -c --stdout examplefile> examplefile.lzma

2)解压

lzma -d --stdout examplefile.lzma >examplefile

5、xz

XZ是lzma实用程序的继承者,它只能压缩单个文件,但不能在一个命令中压缩多个文件,它将自动为压缩文件添加.xz扩展名。

1)压缩

xz examplefile 

2)解压

xz -d examplefile

6、pax

Pax它的执行速度很快,而且它不仅仅是一个压缩器,它可以真正的归档它可以远程复制文件,在Ubuntu/Mint Linux中,默认情况下Pax没有安装。

1)压缩

pax -wf examplefile.tar examplefile
pax -wf examplefile.tar.gz examplefile 

2)解压

pax -r <examplefile.tar

3)查看压缩包文件清单

pax -f examplefile.tar

7、7zip

7Zip文件压缩器是一个开源工具,它最初是为微软Windows开发的,它支持多种文件压缩格式和高文件压缩,它可以用一个命令压缩多个文件。

1)安装7zip

wget https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/p7zip-16.02-10.el7.x86_64.rpm
wget https://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/p7zip-plugins-16.02-10.el7.x86_64.rpm
 
sudo rpm -U --quiet p7zip-16.02-10.el7.x86_64.rpm
sudo rpm -U --quiet p7zip-plugins-16.02-10.el7.x86_64.rpm
 

2)压缩

7z  a examplefile.7z examplefile

3)解压

7z  a examplefile.7z examplefile

8、shar

Shar是一个命令行工具,可以用来压缩测试文件,Shar可以定义为“shell archive”。一个简单而快速的文件存档实用程序对于获取shell脚本的存档非常有用。

1)安装shar工具

yum -y install sharutils

2)压缩

shar examplefile > examplefile.shar

3)解压

unshar examplefile.shar

9、cpio

可以定义为复制输入和输出,它在输入中逐行读取文件名列表,在输出中读取归档文件。这是一个内置的经典命令。

1)压缩

ls | cpio -ov >/home/username/backup.cpio

2)解压

cpio -idv <backup.cpio

10、ar

rar的前身,仍然在Debian及其衍生物中使用,它是一个简单的归档工具,但并不是很流行。

1)压缩  

ar cvsr examplefile.a examplefile

2)解压

ar -xv examplefile.a

11、iso

ISO制作iso镜像

dd if=/media/dvd of=/home/username/filename.iso

三、学习交流

欢迎大家关注我的公众号,一起交流、学习。

3uEF3i6.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK