码迷,mamicode.com
首页 > 系统相关 > 详细

linux文件管理之解压缩

时间:2019-03-26 01:25:27      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:目录结构   please   执行   默认   ase   size   mkdir   不显示   href   

文件的压缩与解压缩



Linux文件压缩工具有:gzip、bzip2、rar、7zip、lbzip2、xz、lrzip、PeaZip、arj等。
====================================================

1.1  zip和unzip命令


zip: 既归档又压缩的工具,可以压缩目录.
zip  FILE
unzip  FILE
unzip  -d  FILE  +路径

素材准备:
[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test
[root@localhost test]# for i in {1..5};do echo "test$i" > test$i.txt;done
[root@localhost test]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
[root@localhost test]# mkdir dir1
[root@localhost test]# cp /etc/fstab dir1/

实例1: 使用zip压缩文件test1.txt
[root@localhost test]# zip test1.zip test1.txt
adding: test1.txt (stored 0%)
[root@localhost test]# ls test1*
test1.txt test1.zip

压缩率为最高压缩test2.txt
[root@localhost test]# zip -9 test2.zip test2.txt
adding: test2.txt (stored 0%)
[root@localhost test]# ls test2*
test2.txt test2.zip

实例2: 讲当前目录dir1连同目录下文件一起压缩
[root@localhost test]# zip -r dir1.zip dir1/
adding: dir1/ (stored 0%)
adding: dir1/fstab (deflated 44%)
[root@localhost test]# ls dir1*
dir1.zip

dir1:
fstab

实例3: 向压缩文件中test1.zip中添加test2. txt文件
[root@localhost test]# zip -m test1.zip test2.txt
adding: test2.txt (stored 0%)

实例4: 删除压缩文件中的文件
[root@localhost test]# zip -d test1.zip test2.txt
deleting: test2.txt

实例5: 压缩文件时排除某个文件
[root@localhost test]# zip test.zip *.txt -x test1.txt
adding: test3.txt (stored 0%)
adding: test4.txt (stored 0%)
adding: test5.txt (stored 0%)


实例6: 解压文件test2.zip
[root@localhost test]# unzip test2.zip
Archive: test2.zip
extracting: test2.txt

实例7:将压缩文件text.zip在指定目录dir1下解压缩
[root@localhost test]# unzip test.zip -d dir1
Archive: test.zip
extracting: dir1/test3.txt
extracting: dir1/test4.txt
extracting: dir1/test5.txt

实例8: 查看压缩文件目录,但不解压
[root@localhost test]# unzip -v test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
6 Stored 6 0% 06-17-2017 17:53 4e6f5599 test3.txt
6 Stored 6 0% 06-17-2017 17:53 012ec35e test4.txt
6 Stored 6 0% 06-17-2017 17:53 1835f21f test5.txt
-------- ------- --- -------
18 18 0% 3 files

1.2  gzip和gunzip命令

 

1.gzip(gnu zip)命令


实例1: 使用gzip压缩文件
[root@localhost test]# gzip test1.txt
[root@localhost test]# ls test1*
test1.txt.gz test1.zip

实例2: 使用gzip压缩目录下文件
[root@localhost test]# gzip -r dir1/
[root@localhost test]# ls dir1
fstab.gz test3.txt.gz test4.txt.gz test5.txt.gz

#注意: 以上压缩之后原始文件就没有了.

实例3: 压缩但保留原始文件
[root@localhost test]# gzip -c test2.txt > test2.txt.gz
[root@localhost test]# ls test2*
test2.txt test2.txt.gz test2.zip


2.gunzip命令


实例1:解压文件至原路径
[root@localhost test]# gunzip test1.txt.gz
[root@localhost test]# ls test1*
test1.txt test1.zip

实例2:解压至指定路径
[root@localhost test]# gunzip -c test2.txt.gz > /tmp/test.txt
[root@localhost test]# cat /tmp/test.txt
test2

实例3:解压目录下的压缩文件
[root@localhost test]# gunzip -r dir1
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt

#注意: gunzip和 gzip -d等价。

1.3  bzip2、bunzip2命令


bzip2、bunzip2是更新的Linux压缩工具,比gzip有着更高的压缩率。

1.bzip2命令

 

2.bunzip2命令


bzip2、bunzip2示例如下:
# bzip2  -z  man.config  //将man.config以bzip2压缩,此时man.config变成man.config.bz2
# bzip2  -9  -c man.config > man.config.bz2 //将man.config用最佳的压缩比压缩,并保留原本的档案
# bzip2  -d  man.config.bz2 //将man.config.bz2解压缩,可用bunzip2取代bzip2  -d
# bunzip2  man.config.bz2 //将man.config.bz2解压缩

1.4  xz、unxz命令


实例1:压缩文件
[root@localhost test]# xz test1.txt
[root@localhost test]# ls test1.txt.xz
test1.txt.xz

实例2:压缩dir1目录下文件
[root@localhost test]# xz dir1/*
[root@localhost test]# ls dir1
fstab.xz test3.txt.xz test4.txt.xz test5.txt.xz

实例3:查看压缩文件内容
[root@localhost test]# xzcat test1.txt.xz
test1

实例4:解压缩
[root@localhost test]# unxz test1.txt.xz

实例5:解压缩目录dir1下文件
[root@localhost test]# xz -d dir1/*
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt

#注意:xz -d等价于unxz

1.5  显示压缩文件的内容:zcat、zless、bzcat、bzless


不解压,显示压缩文件的内容:zcat、zless、bzcat、bzless。
1.zcat、zless
[root@localhost test]# zcat test2.txt.gz
test2
2.bzcat、bzless
例如:
# bzcat  man.config.bz2 //在屏幕上显示man.config.bz2解压缩之后的内容


1.6  tar命令



例1:# tar  -cf  exam.tar  exam1*.txt(把所有exam1*.txt的文件打包成一个exam.tar文件。其中,-c是产生新备份文件;-f是输出到默认的设备,可以把它当做一定要加的选项)
例2:# tar  -rf  exam.tar  exam2*.txt(exam.tar是一个已经存在的打包文件了,再把exam2*.txt的所有文件也打包进去。-r是再增加文件的意思)
例3:# tar  -uf  exam.tar  exam11.txt(刚才exam1*.txt已经打包进去了,但是其中的exam11.txt后来又做了更改,把新改过的文件再重新打包进去,-u是更新的意思)
例4:# tar  -tf  exam.tar(列出exam.tar中有哪些文件被打包在里面。-t是列出的意思)
例5:# tar  -xf  exam.tar(把exam.tar打包文件中全部文件释放出来,-x是释放的意思)
例6:# tar  -xf  exam.tar  exam2*.txt(只把exam.tar打包文件中的所有exam2*.txt文件释放出来,-x是释放的意思)

打包的时候调用压缩,z调用gzip压缩,j调用bzip2压缩,J调用xz压缩
例7:# tar  -zcf  exam.tar.gz  exam1*.txt
例8:解压解包
1:# tar  -xzvf  exam.tar.gz //加一个选项-v,就是显示打包兼压缩或者解压的过程。因为Linux上最常见的软件包文件是.tar.gz文件,因此,最常看到的解压方式就是这样了
2:# tar  -xzvf  exam.tgz //.tgz文件名也是一样的,因性质一样,仅文件名简单一点而已
3:# tar  xzvf  exam.tar.gz  -C  exam/ // 解压到exam目录中
4:# tar  xjvf  exam.tar.bz2  -C  exam/ // j:使用bzip2


例9:打包压缩
1:# tar  cjvf  test.tar.bz2  exam1*.txt
2:# tar  czvf  test.tar.gz  exam1*.txt

# tar -czvf exam.tar.gz *.*  或  # tar -czvf exam.tgz *.*
# tar  cjvf  exam.tar.bz2  exam1*.txt
# tar  xjvf  exam.tar.bz  -C  exam/ // j:使用bzip2

#注意: 在不知道使用什么压缩工具的时候可以使用tar xf 进行解压。


1.7  cpio命令


示例如下:
# find  ./home  -print |cpio  -ov > home.cpio //将home目录备份
# cpio  -idv  < /root/home.cpio //要恢复文件的时候
# cpio  -tv  < home.cpio //查看home.cpio文件

# find  .  -depth | cpio  -ocvB > backup.cpio


将当前目录下名为inittab文件加入initrd.cpio包中
# find . -name inittab -depth | cpio -ovcB -A -F initrd.cpio
# find . -name inittab -depth | cpio -ovcB -A -O initrd.cpio
# find . -name inittab -depth | cpio -ovcB -A --quiet -O initrd.cpio(--quit:表示不显示复制块)


从cpio压缩包中的解压出文件,示例如下:
# cpio --absolute-filenames -icvu < test.cpio //解压到原始位置,解压出来的每个文件的时间属性改为当前时间
# cpio --absolute-filenames -icvum < test.cpio //解压到原始位置,同时不改变解压出来的每个文件的时间属性
# cpio -icvu < test.cpio //解压到当前目录下
# cpio -icvum < test.cpio //解压到当前目录下
# cpio -icvdu -r < grub.cpio //在解包cpio时,对解包出来的文件件进行交互的更名
# cpio -icvu --to-stdout < grub.cpio //将cpio包中的文件解压并输入标准输出,注意:既然解压到标准输出,所以就不能使用-d参数了
# cpio --absolute-filenames -vtc < boot.cpio //不忽略文件列表清单的文件名最前面的“/"
# cpio --no-absolute-filenames -vtc < boot.cpio //默认是忽略文件列表清单的文件中最前面的





1.8 Linux下rar文件的压缩与解压


.rar是在Windows中最常见的压缩文件格式,在Linux中如需使用需要安装rarlinux,官方:
 http://www.rarsoft.com/download.htm
wget http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz
tar xf rarlinux-x64-5.4.0.tar.gz
cd rar
make && make install

RHEL7 下也可以yum安装,方法如下:
yum install ftp://rpmfind.net/linux/dag/redhat/el7/en/x86_64/dag/RPMS/rar-3.8.0-1.el7.rf.x86_64.rpm
或者使用RPM安装UNRAR,下载地址: http://pkgs.repoforge.org/unrar/

安装完成后即可使用rar和unrar命令。简单用法:
实例1:压缩文件
[root@localhost test]# rar a test test*.txt

RAR 3.80 Copyright (c) 1993-2008 Alexander Roshal 16 Sep 2008
Shareware version Type RAR -? for help

Evaluation copy. Please register.

Creating archive test.rar

Adding test1.txt OK
Adding test2.txt OK
Adding test3.txt OK
Adding test4.txt OK
Adding test5.txt OK
Done
rar a test test*.txt #该命令是将所有以test开头,后缀为txt的文件压缩成一个rar包,名为test,该程序会将.rar 扩展名将自动附加到包名后。

实例2:解压文件
[root@localhost test]# rar e test.rar

RAR 3.80 Copyright (c) 1993-2008 Alexander Roshal 16 Sep 2008
Shareware version Type RAR -? for help


Extracting from test.rar

Extracting test1.txt OK
Extracting test2.txt OK
Extracting test3.txt OK
Extracting test4.txt OK
Extracting test5.txt OK
All OK

或者使用unrar
yum install ftp://rpmfind.net/linux/rpmfusion/nonfree/el/updates/7/x86_64/u/unrar-5.4.5-1.el7.x86_64.rpm

unrar e test.rar #该命令是将rar中的所有文件解压出来。

$ rar --help
 
用法: rar <命令> -<选项 1> -<选项 N> <压缩档案> <文件...>
<@文件列表...> <解压缩路径\>
 
<命令>
a 添加文件到压缩档案
c 添加压缩档案注释(注释的大小最大不能超过62000字节)
cf 添加文件备注( 当使用‘v‘参数时显示文件备注,最大不能超过32767字节)


注意:rar的<命令>部分不带”-“号,只有<选项>部分带”-“号,这一点注意一下。
例1:添加文件或目录到压缩档案中,使用a命令。例如把文件files1添加到abc.rar中,使用a或m命令,a命令把file1文件添加到abc.rar档案中保持原有的file1文件不变,m命令移动file1文件到file1.rar档案中(压缩完成后会删除原有的file1文件,注意:m命令只针对文件进行操作)
rar a abc.rarfile1
说明:如果此时abc.rar档案不存在,会自行创建abc.rar文档案,如果abc.rar档案已经存在,则将file1文件压缩进abc.rar档案中,若abc.rar档案中已存在一个file1文件,则会更新file1文件。并且原有的file1文件依然存在于当前目录下,若要将file1文件移动到file1.rar中请使用m命令,对于目录同样可以进行此操作。
注意:如果只输入”rar a file1.rar”命令,而没有接文件名或目录名的话,会将当前目录下所有的文件和文件夹压缩进file1.rar文档中。这点要注意一下。

例2:解压缩abc.rar档案中的内容,可以使用e或x命令,假设abc.rar目录中有一个名为file1的文件和一个名为test的目录,test目录中有一个名为file2的文件,
rar e abc.rar
说明:使用e命令,会将abc.rar中的file1文件连同test目录下的file2文件解压到当前目录。如果想保持abc.rar目录中的目录结构请使用x命令。
rar x abc.rar
说明:此时会将file1文件和test目录解压到当前文件夹。

例3:为整个压缩文件添加注释
rar c abc.rar
说明:输入该命令后,屏幕最下方会显示
Processingarchivetest.
rarReadingcommentfromstdin
并有一个光标在闪烁,从光标闪烁的位置输入注释信息,输入完了按下Ctrl+D结束输入

例4:为压缩文件中的单个文件添加注释,使用cf命令。假如现在要为abc.rar档案中的file1文件添加注释
rar ch abc.rar file1
此时屏幕最下方会显示
Readingcommentfor abc.rar : file1fromstdin
从光标闪烁出输入想要为file1添加的注释,Ctrl+D结束输入

例5:把整个档案的注释写入到一个文件中,使用cw命令。加入要将abc.rar的注释写入到test.txt文件中
rar cw abc.rar test.txt
说明:如果当前目录下不存在名为test.txt的文件,则会自行创建一个名为test.txt的文件,并将abc.rar的注释写入到text.txt文件中,如果当前目录中已经存在一个名为text.txt的文件,则会提示你是否覆盖已经存在的文件,如果选择Yes或者All,则会清空test.txt中原有的内容,然后将abc.rar的注释内容写入到text.txt文件中。

例6:删除压缩档案中的文件或目录,使用d命令。例如要删除abc.rar档案中的file1文件。
rar d abc.rar file1
说明:该命令删除abc.rar档案中的file1文件,对于目录同样有效。

解压缩命令unrar的使用:
$unrar --help
 
用法: unrar <command> -<switch 1> -<switch N> <archive> <files...>
<@listfiles...> <path_to_extract\>
 
<命令>
e 解压文件到当前目录
l[t,b] 列出压缩文档信息[technical, bare]
p 打印文件到标准输出
t 测试压缩我俄当
v[t,b] 列出压缩文档的详细信息[technical,bare]
x 解压文件到完整路径
以下为<选项>部分,略去。
unrar的命令和rar具有同样的效果,可以看出unrar只包含了rar的一部分命令而已,因此使用rar就可以完成所有操作。

1.9 7z文件在Linux上的解压缩


Linux要解压缩.7z文件,首先需要安装7zip软件。
yum install p7zip
使用7zip的命令是7za。

安装完成后的使用方法:
7za {a|d|l|e|u|x} 压缩包文件名 {文件列表或目录,可选}
a 向压缩包里添加文件或创建压缩包,如向7z添加001.jpg,执行:7za a 001.7z 001.jpg;将001目录打包执行:7za a 001.7z 001;
d 从压缩里删除文件,如将7z里的001.jpg删除,执行:7za d 001.7z 001.jpg
l 列出压缩包里的文件,如列出7z里的文件,执行:7za l 001.7z
e 解压到当前目录,目录结构会被破坏,如rar内有如下目录及文件123/456/789.html,执行:7za e 001.rar,目录123和456及文件html都会存放在当前目录下。
x 以完整路径解压。
实例:
→ 7za a test.7z dir1/test*.txt #打包目录dir下test开头后缀为txt的文件
→ 7za x test.7z -o/tmp #解压,-o表示输出目录,注意其与目录路径之间没有空格
→ # ls /tmp/dir1/
test3.txt test4.txt test5.txt



linux文件管理之解压缩

标签:目录结构   please   执行   默认   ase   size   mkdir   不显示   href   

原文地址:https://www.cnblogs.com/anttech/p/10597691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!