首页IT科技linux系统压缩文件命令tar(Linux中文件的压缩与解压缩命令操作示例集锦)

linux系统压缩文件命令tar(Linux中文件的压缩与解压缩命令操作示例集锦)

时间2025-05-05 14:57:39分类IT科技浏览3911
导读:所谓压缩就是将原有的文件通过不同的编码技术进行运算,以减少数据存储所需要的空间,使用前再利用解压缩还原源文件的内容即可。和windows一样,在linux下也存在多种压缩与解压缩方法。...

所谓压缩就是将原有的文件通过不同的编码技术进行运算           ,以减少数据存储所需要的空间                 ,使用前再利用解压缩还原源文件的内容即可           。

和windows一样    ,在linux下也存在多种压缩与解压缩方法                 。

1           、zip压缩与解压缩

zip是最为广泛使用的压缩程序        ,经它压缩的文件会产生扩展名为zip的压缩文件                  ,而且这种格式在多种系统上可以使用       ,像windows中的winzip

下面看一下在linux中如何建立zip文件    。

我们在终端中输入zip会出现这个命令的一些介绍和参数的意义        。

xiaopeng@ubuntu:~/test$ zip

Copyright (c) 1990-2006 Info-ZIP-Type zip "-L" for software license.

Zip 2.32 (June 19th 2006). Usage:

zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

The default action is to add or replace zipfile entries from list, which

can include the special name-to compress standard input.

If zipfile and list are omitted, zip compresses stdin to stdout.

-f freshen: only changed files -u update: only changed or new files

-d delete entries in zipfile -m move into zipfile (delete files)

-r recurse into directories -j junk (dont record) directory names

-0 store only -l convert LF to CR LF (-ll CR LF to LF)

-1 compress faster -9 compress better

-q quiet operation -v verbose operation/print version info

-c add one-line comments -z add zipfile comment

-@ read names from stdin -o make zipfile as old as latest entry

-x exclude the following names -i include only the following names

-F fix zipfile (-FF try harder) -D do not add directory entries

-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)

-T test zipfile integrity -X eXclude eXtra file attributes

-y store symbolic links as the link instead of the referenced file

-R PKZIP recursion (see manual)

-e encrypt -n dont compress these suffixes

下面我们就最简单的实验一下                  。我们就是把当前目录下文件名以test开头的所有文件压缩文一个文件     ,并可以查看一下压缩比       。(红色是我的注释)

代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 24K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4
代码如下:
xiaopeng@ubuntu:~/test$ zip test.zip test*

zip命令后面先跟压缩后的文件名                  ,这里是test.zip,当然后缀名不是必须的     。然后跟要压缩的文件名                  。这里用的test*指的是全部以test开头的文件          ,包括test1 test2 test3 test4

adding: test1 (deflated 30%) 这里显示的是压缩比

adding: test2 (deflated 65%)

adding: test3 (deflated 64%)

adding: test4 (deflated 73%) 大体可以看出源文件越大  ,压缩比就越大
代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 32K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

-rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:17 test.zip

xiaopeng@ubuntu:~/test$

上面是压缩了相同类型的文件                 ,其实也可以把不同类型的文件压缩到一起          。有时候为了节省硬盘空间             ,可以在建立压缩文件后,自动删除原始文件              ,此时只要带一个 -m 的参数就可以  。

代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 24K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

xiaopeng@ubuntu:~/test$ zip -m test.zip test* 带参数-m

updating: test1 (deflated 30%)

updating: test2 (deflated 65%)

updating: test3 (deflated 64%)

updating: test4 (deflated 73%)

xiaopeng@ubuntu:~/test$ ls -lh

总用量 8.0K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 5.0K 2009-06-25 14:26 test.zip

xiaopeng@ubuntu:~/test$

可以看出 原始文件已经被删除                ,只有压缩文件留下了                 。

在压缩一些目录的时候  ,经出在目录中会有子目录           ,此时根据子目录中的文件是否压缩分为两种情况                 ,一种是压缩    ,一种是忽略自录中的内容        ,如果选择压缩子目录                  ,则使用-r参数       ,如果不压缩     ,则使用-j 参数

下面举例                  ,一个是-r 一个是-j
代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 28K

代码如下:

drwxr-xr-x 2 xiaopeng xiaopeng 4.0K 2009-06-25 14:31 pdf

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

xiaopeng@ubuntu:~/test$ zip -r test.zip * 压缩当前目录所有内容          ,r 参数说明pdf这个子目录中的内容也压缩

adding: pdf/ (stored 0%)

adding: pdf/case_Contact.pdf (deflated 10%)

adding: pdf/case_KRUU.pdf (deflated 9%)

adding: pdf/case_howard_county_library.pdf (deflated 24%)

adding: test1 (deflated 30%)

adding: test2 (deflated 65%)

adding: test3 (deflated 64%)

adding: test4 (deflated 73%)

xiaopeng@ubuntu:~/test$

下面的情况是子目录不压缩

代码如下:
xiaopeng@ubuntu:~/test$ ls -l

总用量 28

代码如下:

drwxr-xr-x 2 xiaopeng xiaopeng 4096 2009-06-25 14:31 pdf

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1233 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3412 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 10091 2009-06-25 14:14 test4

xiaopeng@ubuntu:~/test$ zip -j test.zip *

adding: test1 (deflated 30%)

adding: test2 (deflated 65%)

adding: test3 (deflated 64%)

adding: test4 (deflated 73%)

子目录pdf被忽略

代码如下:
xiaopeng@ubuntu:~/test$

令外一个技巧: 某些文件因为编码的原因  ,已经大幅的减少了文件的大小                 ,如GIF,JPG 等格式             ,在用zip压缩几乎没什么作用而浪费了时间,此时可一用-n参数直接保存这些文件而不压缩             。例如:

代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 68K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 18K 2009-06-04 21:18 duality.jpg

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 14:13 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

-rw-r--r-- 1 xiaopeng xiaopeng 23K 2009-06-10 15:07 test.jpg

xiaopeng@ubuntu:~/test$ zip -n .jpg test.zip *

adding: duality.jpg (stored 0%)

adding: test1 (deflated 30%)

adding: test2 (deflated 65%)

adding: test3 (deflated 64%)

adding: test4 (deflated 73%)

adding: test.jpg (stored 0%)

jpg格式的没有压缩而是直接保存了

代码如下:
xiaopeng@ubuntu:~/test$

如果需要直接保存的格式多于一个              ,可以用冒号隔开 如: -n .jpg: .mpg

小技巧                ,有时候一个目录下要压缩的文件很多  ,但是有那么很少的几个不压缩           ,那么我们可以用-x参数来排除这几个不压缩的。例如

代码如下:

xiaopeng@ubuntu:~/test$ ls

duality.jpg test1 test2 test3 test4 test.jpg test.zip

xiaopeng@ubuntu:~/test$ zip -n .jpg test.zip * -x test2 不压缩test2

updating: duality.jpg (stored 0%)

updating: test1 (deflated 30%)

updating: test3 (deflated 64%)

updating: test4 (deflated 73%)

updating: test.jpg (stored 0%)

xiaopeng@ubuntu:~/test$

可以看到test2没有被压缩                 ,而是直接跳过了它              。

压缩链接    ,zip会先读取该链接的指向的原文件的内容        ,然后再压缩                  ,而且压缩完了       ,该链接也就不存在了                。

另外     ,压缩率也是可以调整的  。等级是1到9                  ,1最低          ,9最高  ,默认是6            。我们可以用1和9来比较下                 ,压缩率                 。

代码如下:

xiaopeng@ubuntu:~/test$ zip -1 low.zip *

adding: test1 (deflated 30%)

adding: test2 (deflated 63%)

adding: test3 (deflated 62%)

adding: test4 (deflated 70%)

xiaopeng@ubuntu:~/test$ zip -9 high.zip *

adding: low.zip (deflated 4%)

adding: test1 (deflated 30%)

adding: test2 (deflated 65%)

adding: test3 (deflated 64%)

adding: test4 (deflated 73%)

因为文件都比较小             ,效果不是很明显,但是9的压缩率确实高了一点点    。

压缩率高              ,节省空间                ,但是压缩时间要长  ,压缩率低           ,节省空间少                 ,但是用时间少    ,所以我们要合理选择压缩率        ,一般都用默认        。

zip文件解压缩                  。这个比较简单                  ,就是unzip命令       。

代码如下:

xiaopeng@ubuntu:~/test$ ls

test.zip

xiaopeng@ubuntu:~/test$ unzip test.zip

Archive: test.zip

inflating: test1

inflating: test2

inflating: test3

inflating: test4

xiaopeng@ubuntu:~/test$

当然也可以用-x参数来指定哪个文件不需要压缩     。

代码如下:

xiaopeng@ubuntu:~/test$ unzip test.zip -x test3 test3不需要压缩出来

Archive: test.zip

inflating: test1

inflating: test2

inflating: test4

xiaopeng@ubuntu:~/test$

还有一个很有用的参数       ,-Z ,注意是大写的Z                   。作用是查看压缩文件的内容          。就像windows中的winzip,我们不用解压缩     ,也可以打开看看里面有什么文件                  ,文件的类型什么  。比如我想看看test.zip里面的内容          ,而又不想把这个解压缩了再看  ,可以如下操作                 。

代码如下:

xiaopeng@ubuntu:~/test$ unzip -Z test.zip

Archive: test.zip 5069 bytes 4 files

-rw-r--r-- 2.3 unx 212 tx defN 25-Jun-09 14:13 test1

-rw-r--r-- 2.3 unx 1233 tx defN 25-Jun-09 14:13 test2

-rw-r--r-- 2.3 unx 3412 tx defN 25-Jun-09 14:14 test3

-rw-r--r-- 2.3 unx 10091 tx defN 25-Jun-09 14:14 test4

4 files, 14948 bytes uncompressed, 4567 bytes compressed: 69.4%

xiaopeng@ubuntu:~/test$

当然除了这些参数外                 ,还有很多参数可以使用             ,这里就不一一实验了,我们可以在使用的过程中加以掌握             。

2               、zip与 tar

如果你在Linux里面安装过软件压缩包              ,对这个以.tar.gz为后缀的压缩文件不会陌生                ,比如我们在Linux QQ 的下载页面http://im.qq.com/qq/linux/download.shtml ,就会看到其中一个安装包就是.tar.gz包。

这种包带两个后缀是有原因的  ,gz和tar 是分别由两种程序产生的              。gz时由gzip压缩而成的压缩文件           ,压缩效果和zip差不多                 ,但是和zip最大的不同在于    ,gzip无法把很多个单一文件压缩成一个单一文件        ,所以tar就有了用武之地                  ,tar不是什么压缩程序       ,它是用来打包文件的                。tar和gzip一见如故     ,两个人合作起来实现压缩                  ,也就是当多个文件压缩时          ,先用tar把这些文件打包  ,成为.tar的包                 ,然后再由gzip压缩这个包             ,于是就有了.tar.gz的文件格式  。

首先先看一下gzip和 gunzip的应用           。gzip的用法很简单,后面加上要压缩的文件名就行                 。

代码如下:
xiaopeng@ubuntu:~/test$ ls -lh

总用量 24K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 212 2009-06-25 15:49 test1

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

xiaopeng@ubuntu:~/test$ gzip test1

xiaopeng@ubuntu:~/test$ ls -lh

总用量 24K

代码如下:

-rw-r--r-- 1 xiaopeng xiaopeng 173 2009-06-25 15:49 test1.gz

-rw-r--r-- 1 xiaopeng xiaopeng 1.3K 2009-06-25 14:13 test2

-rw-r--r-- 1 xiaopeng xiaopeng 3.4K 2009-06-25 14:14 test3

-rw-r--r-- 1 xiaopeng xiaopeng 9.9K 2009-06-25 14:14 test4

注意和zip的不同              ,只要在命令后加上要压缩的文件名即可                ,系统会自动为生成的压缩文件起名为原文件名加后缀.gz   ,而且原文件在压缩完成后会被删除    。

解压缩用gunzip
代码如下:

xiaopeng@ubuntu:~/test$ gunzip *.gz

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4

xiaopeng@ubuntu:~/test$

完成后以前的压缩文件test1.gz也会被删除        。

同样gzip在解压前也可以查看文件内容           ,用参数-l                 ,

gzip也支持压缩率修改    ,为1到9        ,和zip相同                  。

下面看tar的用应       。tar是用来打包文件的                  ,打包后的包的大小和以前所有原文件大小的和是相等的       ,(其实大小是不相等的     ,打完包后的大小大于源文件的大小和                  ,这个可以验证一下     。《Ubuntu 入门到精通》说一样大          ,显然是不对的)也就是说tar没有压缩的效果                  。tar有非常多的参数  ,可以通过在线帮助文档查看                 ,或者用--help命令查看          。这里我们只用简单用到几个  。

首先是多个文件打包                 。看例子             。
代码如下:

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4

xiaopeng@ubuntu:~/test$ tar -cvf test.tar *

是把当前目录下的所有文件打包成test.tar 几个参数的意义为: c(Creat)建立新文件 v(Verbose)显示命令执行时的信息 f(File)指定打包为文件形式。

代码如下:

test1

test2

test3

test4
代码如下:

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4 test.tar

xiaopeng@ubuntu:~/test$

要解开tar文件             ,只需把参数中的c改为x(eXtract)即可

代码如下:

xiaopeng@ubuntu:~/test$ ls

test.tar

xiaopeng@ubuntu:~/test$ tar -xvf test.tar

test1

test2

test3

test4

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4 test.tar

xiaopeng@ubuntu:~/test$

下面我们看一下tar和gzip合作完成对4个文件的压缩              。步骤是先用tar打包,然后对这个.tar包用gzip压缩              ,最后得到.tar.gz文件                。例子:

代码如下:

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4

xiaopeng@ubuntu:~/test$ tar cvf test.tar * 首先打包成test.tar

test1

test2

test3

test4

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4 test.tar

xiaopeng@ubuntu:~/test$ gzip test.tar 把test.tar用gzip压缩成test.tar.gz 压缩包  。

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4 test.tar.gz

xiaopeng@ubuntu:~/test$

解压.tar.gz包时                ,和压缩过程相反  ,现解压           ,再tar把包打开           。

代码如下:

xiaopeng@ubuntu:~/test$ ls

test.tar.gz

xiaopeng@ubuntu:~/test$ gunzip test.tar.gz 先用gunzip把.tar.gz包解压缩

xiaopeng@ubuntu:~/test$ ls

test.tar

xiaopeng@ubuntu:~/test$ tar xvf test.tar 然后用tar把.tar包打开

test1

test2

test3

test4

xiaopeng@ubuntu:~/test$ ls

test1 test2 test3 test4 test.tar

xiaopeng@ubuntu:~/test$

还可以tar 和gzip同时实现的                 ,只要在tar参数加一个z即可 tar -xvfz test.tar.gz 即可实现上面两个命令的功能                 。

还是比较好理解的    。

声明:本站所有文章    ,如无特殊说明或标注        ,均为本站原创发布        。任何个人或组织                  ,在未征得本站同意时       ,禁止复制      、盗用         、采集               、发布本站内容到任何网站         、书籍等各类媒体平台                  。如若本站内容侵犯了原著者的合法权益     ,可联系我们进行处理       。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
wps的广告怎么关掉(WPS广告怎么关闭 ?教你轻松去掉WPS广告方法)