Linux命令(十五) 打包或解压文件 tar

目录

[1.命令简介](#1)

2.常用参数介绍

3.实例

4.直达底部

命令简介

tar 命令用于将文件打包或解压,扩展名一般为 ".tar" ,指定特定的参数可以调用 gzip 或 bzip2 制作压缩包或解压压缩包,扩展名为 ".tar.gz" 或 ".tar.bz2"。

返回目录

常用参数介绍

-c 建立新的压缩包

-d 比较存档与当前文件的不同之处

--delete 从压缩包中删除

-r 附加到压缩包结尾

-t 列出压缩包中文件的目录

-u 仅将较新的文件附加到压缩包中

-x 解压压缩包

-C 解压到指定的目录

-f 使用的压缩包名字,f参数之后不能再加参数

-i 忽略存档中的 0 字节块

-v 处理过程中输出相关信息

-z 调用 gzip 来压缩归档文件,与 -x 联用时调用 gzip 完成解压缩

-Z 调用 compress 来压缩归档文件,与 -x 联用时调用 compress 完成解压缩

-j 调用 bzip2 压缩或解压

-p 使用原文件的原来属性

-P 可以使用绝对路径来压缩

--exclude 排除不加入压缩包的文件

tar 命令相关的包一般使用 .tar 作为文件标识名。如果加 z 参数,则以 .tar.gz 或 .taz 来代表 gzip 压缩过的 tar。

返回目录

实例

1.仅打包,不压缩

[hk@localhost ~]$ ls   
kernel  linux  study
[hk@localhost ~]$ tar -cvf study.tar study/    压缩文件夹
study/
study/a.c
study/b.txt
study/a/
study/a/a/
study/a/d/
[hk@localhost ~]$ ls
kernel  linux  study  study.tar
[hk@localhost ~]$ 

2.打包并使用 gzip 压缩

[hk@localhost ~]$ ls
kernel  linux  study  study.tar
[hk@localhost ~]$ tar -zcvf study.tar.gz study/   使用 gzip 压缩
study/
study/a.c
study/b.txt
study/a/
study/a/a/
study/a/d/
[hk@localhost ~]$ ls
kernel  linux  study  study.tar  study.tar.gz
[hk@localhost ~]$ 

3.打包并使用 bzip2 压缩

tar -jcvf study.tar.bz2 study/

4.查看压缩文件列表

[hk@localhost ~]$ ls
kernel  linux  study  study.tar  study.tar.gz
[hk@localhost ~]$ tar -ztvf study.tar.gz 
drwxrwxr-x hk/hk             0 2018-01-22 02:21 study/
-rw-rw-r-- hk/hk            45 2017-12-31 00:00 study/a.c
-rw-rw-r-- hk/hk            78 2018-01-08 06:09 study/b.txt
drwxrwxr-x hk/hk             0 2018-01-22 02:21 study/a/
drwxrwxr-x hk/hk             0 2018-01-22 02:21 study/a/a/
drwxrwxr-x hk/hk             0 2018-01-22 02:21 study/a/d/

5.解压压缩包到当路径

[hk@localhost ~]$ tar -zxvf study.tar.gz 

返回目录

原文地址:https://www.cnblogs.com/ay-a/p/8325087.html