Linux中tar命令和zip命令总结

一、背景说明

我们在linux系统下经常碰到需要解压文件和压缩文件的情况,而我们最常用的就是tar包和zip包,在这里总结以下tar命令和zip命令的使用。

二、tar命令

1、语法:tar [主选项+辅选项] 文件或目录

使用该命令时,主选项必须有,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用。

主选项:【一条命令以下5个参数只能有一个】

-c: --create 新建一个压缩文档,即打包

-x: --extract,--get解压文件

-t: --list,查看压缩文档里的所有内容

-r:--append 向压缩文档里追加文件

-u:--update 更新原压缩包中的文件

辅助选项:

-z:是否同时具有gzip的属性?即是否需要用gzip压缩或解压?一般格式为xxx.tar.gz或xx.tgz

-j:是否同时具有bzip2的属性?即是否需要用bzip2压缩或解压?一般格式为xx.tar.bz2

-v:显示操作过程!这个参数很常用

-f:使用文档名,注意,在f之后要立即接文档名,不要再加其他参数!

-C:切换到指定目录

--exclude FILE:在压缩过程中,不要将FILE打包

2、压缩

习惯上以.tar后缀代表tar包,用xxx.tar.gz或.tgz代表gzip压缩过的tar文件,用.tar.bz2代表bzip2压缩过的tar文件。

  • 只打成tar包,不压缩
[root@LeoLinux tempfile]# ls
img1.png  img2.png
[root@LeoLinux tempfile]# tar cvf img.tar img1.png img2.png
img1.png
img2.png
[root@LeoLinux tempfile]# ls -lht
总用量 12K
-rw-r--r--. 1 root root 10K 2月  21 13:59 img.tar
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
  • 使用gzip压缩
[root@LeoLinux tempfile]# tar czvf img.tar.gz img1.png img2.png
img1.png
img2.png
[root@LeoLinux tempfile]# ls -lht
总用量 16K
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root 10K 2月  21 13:59 img.tar
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
  • 使用bzip2压缩
[root@LeoLinux tempfile]# tar cjvf img.tar.bz2 img1.png img2.png
img1.png
img2.png
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root 10K 2月  21 13:59 img.tar
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
  • 通过以上方式压缩后,tar包变小
[root@LeoLinux tempfile]# du -sh *
0       img1.png
0       img2.png
12K     img.tar
4.0K    img.tar.bz2
4.0K    img.tar.gz

3、查看压缩内容

  • tf参数查看压缩包中的文件,加v查看详细信息
[root@LeoLinux tempfile]# tar tf img.tar.gz
img1.png
img2.png
[root@LeoLinux tempfile]# tar tvf img.tar.bz2
-rw-r--r-- root/root         0 2019-02-21 13:59 img1.png
-rw-r--r-- root/root         0 2019-02-21 13:59 img2.png

4、更新

如果tar包中的某个文件更新了内容需要重新打进压缩包中

[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:16 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
[root@LeoLinux tempfile]# tar uf img.tar img1.png
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:17 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png

5、追加

将新文件追加到tar包中

[root@LeoLinux tempfile]# touch img3.png
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root   0 2月  21 14:18 img3.png
-rw-r--r--. 1 root root 10K 2月  21 14:17 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
[root@LeoLinux tempfile]# tar -rf img.tar img3.png
[root@LeoLinux tempfile]# tar -tf img.tar
img1.png
img2.png
img3.png

 6、解压

  • 解压全部
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
[root@LeoLinux tempfile]# tar xf img.tar
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root   0 2月  21 14:18 img3.png
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
  • 解压某个文件
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
[root@LeoLinux tempfile]# tar xf img.tar img1.png
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png
  • 解压gzip压缩或bzip2压缩,需要加z或者j参数
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
[root@LeoLinux tempfile]# tar xzf img.tar.gz
[root@LeoLinux tempfile]# ls -lht
总用量 20K
-rw-r--r--. 1 root root 10K 2月  21 14:18 img.tar
-rw-r--r--. 1 root root 136 2月  21 14:00 img.tar.bz2
-rw-r--r--. 1 root root 127 2月  21 14:00 img.tar.gz
-rw-r--r--. 1 root root   0 2月  21 13:59 img2.png
-rw-r--r--. 1 root root   0 2月  21 13:59 img1.png

7、-C参数

用法:tar temp.tar file -C fileDir

说明:-C参数用来切换工作目录,解压和压缩时都可以用,就相当于将当前工作路径切换到-C后面给的路径上。

  • 压缩
[root@LeoLinux tempfile]# ls -lht
总用量 4.0K
drwxr-xr-x. 2 root root 4.0K 2月  21 14:41 tmp
[root@LeoLinux tempfile]# tar -cf img.tar * -C tmp  # 等价于tar cf img.tar tmp/*
[root@LeoLinux tempfile]# ls -lht
总用量 24K
-rw-r--r--. 1 root root  20K 2月  21 14:46 img.tar
drwxr-xr-x. 2 root root 4.0K 2月  21 14:41 tmp
[root@LeoLinux tempfile]# tar tf img.tar
tmp/
tmp/img2
tmp/img1
  • 解压
[root@LeoLinux tempfile]# mkdir tmp1
[root@LeoLinux tempfile]# tar xf img.tar -C ./tmp1
[root@LeoLinux tempfile]# cd tmp1
[root@LeoLinux tmp1]# ls -lht
总用量 4.0K
drwxr-xr-x. 2 root root 4.0K 2月  21 14:41 tmp

三、zip命令

1、参数

-A:调整可执行的自动解压缩文件;
-b<工作目录>:指定暂时存放文件的目录;
-c:替每个被压缩的文件加上注释;
-d:从压缩文件内删除指定的文件;
-D:压缩文件内不建立目录名称;
-f:此参数的效果和指定“-u”参数类似,但不仅更新既有文件,如果某些文件原本不存在于压缩文件内,使用本参数会一并将其加入压缩文件中;
-F:尝试修复已损坏的压缩文件;
-g:将文件压缩后附加在已有的压缩文件之后,而非另行建立新的压缩文件;
-h:在线帮助;
-i<范本样式>:只压缩符合条件的文件;
-j:只保存文件名称及其内容,而不存放任何目录名称;
-J:删除压缩文件前面不必要的数据;
-k:使用MS-DOS兼容格式的文件名称;
-l:压缩文件时,把LF字符置换成LF+CR字符;
-ll:压缩文件时,把LF+cp字符置换成LF字符;
-L:显示版权信息;
-m:将文件压缩并加入压缩文件后,删除原始文件,即把文件移到压缩文件中;
-n<字尾字符串>:不压缩具有特定字尾字符串的文件;
-o:以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同;
-q:不显示指令执行过程;
-r:递归处理,将指定目录下的所有文件和子目录一并处理;
-S:包含系统和隐藏文件;
-t<日期时间>:把压缩文件的日期设成指定的日期;
-T:检查备份文件内的每个文件是否正确无误;
-u:更换较新的文件到压缩文件内;
-v:显示指令执行过程或显示版本信息;
-V:保存VMS操作系统的文件属性;
-w:在文件名称里假如版本编号,本参数仅在VMS操作系统下有效;
-x<范本样式>:压缩时排除符合条件的文件;
-X:不保存额外的文件属性;
-y:直接保存符号连接,而非该链接所指向的文件,本参数仅在UNIX之类的系统下有效;
-z:替压缩文件加上注释;
-$:保存第一个被压缩文件所在磁盘的卷册名称;
-<压缩效率>:压缩效率是一个介于1~9的数值。

2、实例

  • 压缩
用法
1.将/home/data 这个目录下的所有文件打包压缩为当前目录下的data.zip
zip -q -r data.zip /home/data
2.如果现在在/home 这个目录下,则如下
zip -q -r data.zip data
3.如果在/home/data 这个目录下,则如下
zip -q -r data.zip *
  • 解压,unzip
[root@LeoLinux tempfile]# ls -lht
总用量 24K
-rw-r--r--. 1 root root 540 2月  21 14:56 img.zip
-rw-r--r--. 1 root root 20K 2月  21 14:46 img.tar
[root@LeoLinux tempfile]# unzip img.zip
Archive:  img.zip
   creating: tmp/
 extracting: tmp/img2
  inflating: tmp/img1
[root@LeoLinux tempfile]# ls -lht
总用量 28K
-rw-r--r--. 1 root root  540 2月  21 14:56 img.zip
-rw-r--r--. 1 root root  20K 2月  21 14:46 img.tar
drwxr-xr-x. 2 root root 4.0K 2月  21 14:41 tmp

四、常用压缩包的解压方法

1、*.tar 用 tar –xvf 解压
2、*.gz 用 gzip -d或者gunzip 解压
3、*.tar.gz和*.tgz 用 tar –xzf 解压
4、*.bz2 用 bzip2 -d或者用bunzip2 解压
5、*.tar.bz2用tar –xjf 解压
6、*.Z 用 uncompress 解压
7、*.tar.Z 用tar –xZf 解压
8、*.rar 用 unrar x 解压
9、*.zip 用 unzip 解压

参考

Linux tar命令简介

linux中的 tar命令的 -C 参数,以及其它一些参数

linux的zip命令详解

猜你喜欢

转载自blog.csdn.net/lwcaiCSDN/article/details/87859601
今日推荐