tar:打包和压缩文件

xiaohai 2021-05-05 20:51:29 2415人围观 标签: Linux 
简介tar命令是将多个文件打包在一起,并且可以实现解压打包的文件。tar命令不仅可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。
说明

  tar命令是将多个文件打包在一起,并且可以实现解压打包的文件。tar命令不仅可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。

格式

  tar [option] [file]

常用参数说明
  • z:通过gzip压缩或解压
  • c:创建新的tar包
  • v:详细显示tar命令执行的过程
  • f:指定压缩文件的名字
  • t:不解压查看tar包的内容
  • p:保持文件的原有属性
  • P(大写):以绝对路径打包,危险参数
  • j:通过bzip2命令压缩或解压
  • x:解开tar包
  • C:指定解压的目录路径
  • —exclude=PATTERN:打包时排除不需要处理的文件或目录
  • -X 文件名:从指定文件读取不需要处理的文件或目录列表
  • -N 日期:仅打包比指定日期新的文件,可用于增量打包备份
  • -h:打包软链接文件指向的真实源文件
  • —hard-dereference:打包硬链接文件

注意:

  • tar命令的选项有点特殊,加不加”-“符号都可以
  • 若需要打包的目录为相对路径,则—exclude后只能接相对路径
  • 若需要打包的目录为绝对路径,则—exclude后既能是绝对路径也可以是相对路径
  • 未来方便,尽量统一,要么都是绝对,要么都是相对路径
示例

1、打包目录test

[root@localhost ~]# tar zcvf test.tar.gz ./test/
./test/
./test/dir1/
./test/dir1/test1.txt
./test/dir1/test2.txt
./test/dir1/test3.txt
./test/test.txt
./test/test_02.txt
./test/test_03.txt
./test/test_04.txt
./test/test_05.txt
./test/test_06.txt
./test/test_07.txt
./test/test_08.txt
./test/test_09.txt
./test/test_10.txt
./test/test_11.txt
./test/test_12.txt
./test/test_01.txt
[root@localhost ~]# ll test.tar.gz 
-rw-r--r--. 1 root root 359 May 14 08:19 test.tar.gz

2、查看压缩包的内容

[root@localhost ~]# tar ztvf test.tar.gz #使用选项t不解压就可以查看压缩包的内容,选项v可以显示文件的属性
drwxr-xr-x root/root         0 2018-05-07 22:45 ./test/
d--------- root/root         0 2018-05-07 22:34 ./test/dir1/
---------- root/root         0 2018-05-07 22:34 ./test/dir1/test1.txt
---------- root/root         0 2018-05-07 22:34 ./test/dir1/test2.txt
---------- root/root         0 2018-05-07 22:34 ./test/dir1/test3.txt
-rw-r--r-- root/root         0 2018-05-06 07:24 ./test/test.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_02.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_03.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_04.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_05.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_06.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_07.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_08.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_09.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_10.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_11.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 ./test/test_12.txt
-rw-r----x root/root        11 2018-05-07 22:45 ./test/test_01.txt

[root@localhost ~]# tar ztf test.tar.gz  #去掉v选项
./test/
./test/dir1/
./test/dir1/test1.txt
./test/dir1/test2.txt
./test/dir1/test3.txt
./test/test.txt
./test/test_02.txt
./test/test_03.txt
./test/test_04.txt
./test/test_05.txt
./test/test_06.txt
./test/test_07.txt
./test/test_08.txt
./test/test_09.txt
./test/test_10.txt
./test/test_11.txt
./test/test_12.txt
./test/test_01.txt

[root@localhost ~]# tar tf test.tar.gz  #去掉z选项,命令也会自动判断压缩包的类型,自动调用gzip命令
./test/
./test/dir1/
./test/dir1/test1.txt
./test/dir1/test2.txt
./test/dir1/test3.txt
./test/test.txt
./test/test_02.txt
./test/test_03.txt
./test/test_04.txt
./test/test_05.txt
./test/test_06.txt
./test/test_07.txt
./test/test_08.txt
./test/test_09.txt
./test/test_10.txt
./test/test_11.txt
./test/test_12.txt
./test/test_01.txt

3、解压压缩包

[root@localhost ~]# tar zxvf test.tar.gz -C /tmp/ #使用-C参数,指定解压目录,不加-C就解压到当前目录
./test/
./test/dir1/
./test/dir1/test1.txt
./test/dir1/test2.txt
./test/dir1/test3.txt
./test/test.txt
./test/test_02.txt
./test/test_03.txt
./test/test_04.txt
./test/test_05.txt
./test/test_06.txt
./test/test_07.txt
./test/test_08.txt
./test/test_09.txt
./test/test_10.txt
./test/test_11.txt
./test/test_12.txt
./test/test_01.txt

[root@localhost ~]# ll /tmp/test
total 4
d---------. 2 root root 54 May  7 22:34 dir1
-rw-r----x. 1 root root 11 May  7 22:45 test_01.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_02.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_03.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_04.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_05.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_06.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_07.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_08.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_09.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_10.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_11.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_12.txt
-rw-r--r--. 1 root root  0 May  6 07:24 test.txt

4、排除打包

[root@localhost ~]# tar zcvf test.tar.gz ./test/ --exclude=test/dir1 --exclude=test/test.txt 
./test/
./test/test_02.txt
./test/test_03.txt
./test/test_04.txt
./test/test_05.txt
./test/test_06.txt
./test/test_07.txt
./test/test_08.txt
./test/test_09.txt
./test/test_10.txt
./test/test_11.txt
./test/test_12.txt
./test/test_01.txt

注意:如果是目录,如test/dir1那么这后面一定不要加/,否则不能排除成功,排除多个就使用多个--exclude参数

5、使用指定文件排除打包参数-X使用

[root@localhost ~]# cat list.txt  #排除文件内容
test/test_02.txt
test/test_03.txt
test/test_04.txt
test/test_05.txt
test/test_06.txt
test/test_07.txt
test/test_08.txt
test/test_09.txt
test/test_10.txt
test/test_11.txt
test/test_12.txt
test/test_01.txt

[root@localhost ~]# tar zcvfX test.tar.gz list.txt ./test/ #执行打包
./test/
./test/dir1/
./test/dir1/test1.txt
./test/dir1/test2.txt
./test/dir1/test3.txt
./test/test.txt

6、打包软链接文件

[root@localhost ~]# ln -s test test_ln #创建一个软链接
[root@localhost ~]# ll
....
drwxr-xr-x.  3 root  root       4096 May  7 22:45 test
lrwxrwxrwx.  1 root  root          4 May 14 08:32 test_ln -> test
....

[root@localhost ~]# tar zcf test_ln.tar.gz test_ln/ #使用常规方式打包软链接
[root@localhost ~]# tar tfv test_ln.tar.gz  #不解压查看文件内容,里面是个软链接文件,所以这里不能进行常规方式打包
lrwxrwxrwx root/root         0 2018-05-14 08:32 test_ln -> test

[root@localhost ~]# tar zcfh test_ln.tar.gz test_ln/ #加上h参数来打包软链接,下的显示正常了
[root@localhost ~]# tar tfv test_ln.tar.gz 
drwxr-xr-x root/root         0 2018-05-07 22:45 test_ln/
d--------- root/root         0 2018-05-07 22:34 test_ln/dir1/
---------- root/root         0 2018-05-07 22:34 test_ln/dir1/test1.txt
---------- root/root         0 2018-05-07 22:34 test_ln/dir1/test2.txt
---------- root/root         0 2018-05-07 22:34 test_ln/dir1/test3.txt
-rw-r--r-- root/root         0 2018-05-06 07:24 test_ln/test.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_02.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_03.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_04.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_05.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_06.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_07.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_08.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_09.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_10.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_11.txt
-rw-r--r-- root/root         0 2018-05-06 07:22 test_ln/test_12.txt
-rw-r----x root/root        11 2018-05-07 22:45 test_ln/test_01.txt

7、指定文件进行打包

[root@localhost ~]# tar zcvf test01.tar.gz test/test_01.txt test/test_02.txt
test/test_01.txt
test/test_02.txt
[root@localhost ~]# tar ztf test01.tar.gz 
test/test_01.txt
test/test_02.txt

8、打包/etc下的所有普通文件

[root@localhost ~]# tar zcvf etc.tar.gz `find /etc/ -type f`