十二 Linux之tar解压缩

  tar是常用的解压缩指令,常用参数

  • -c 创建一个压缩包
  • v 显示压缩的详细信息
  • -z 压缩,如果没有只是打包
  • -f 压缩后的目标文件名
  • -x解压
  • -C解压到指定目录下

实例

  1. 把所有名为cc*.txt的文件打包到file.tar下
    [root@centoscc cc]# tar -cf file.tar cc*.txt
    [root@centoscc cc]# ls -l
    总用量 60
    ...
    -rw-r--r--. 1 root root  10240 12月 24 19:11 file.tar
    ...
  2. 把文件root1.txt增加到file.tar文件中
    [root@centoscc cc]# tar -rf file.tar root1.txt
  3. 更新file.tar包中的root1.txt文件
    [root@centoscc cc]# tar -uf file.tar root1.txt
  4. 列出包中的文件
    [root@centoscc cc]# tar -tf file.tar
    cc4.txt
    cc.txt
    root1.txt
  5. 解出包中内容到同一目录下
    [root@centoscc cc1]# ls -l
    总用量 12
    drwx--x--x. 3 root root    18 12月 20 16:25 cc1
    drwxr-xr-x. 2 root root     6 12月 20 16:24 cc11
    -rw-r--r--. 1 root root 10240 12月 24 19:11 file.tar
    [root@centoscc cc1]# tar -xf file.tar
    [root@centoscc cc1]# ls -l
    总用量 24
    drwx--x--x. 3 root root    18 12月 20 16:25 cc1
    drwxr-xr-x. 2 root root     6 12月 20 16:24 cc11
    -rw-r--r--. 1 root root    16 12月 20 21:19 cc4.txt
    -rw-r--r--. 1 root root    15 12月 20 21:21 cc.txt
    -rw-r--r--. 1 root root 10240 12月 24 19:11 file.tar
    -rw-r--r--. 1 root root    16 12月 22 20:37 root1.txt
  6. 解出包中内容到指定目录下(该目录要先创建,否则会报错)
    [root@centoscc cc1]# tar -xvf file.tar -C file1
    cc4.txt
    cc.txt
    root1.txt
    [root@centoscc cc1]# cd file1
    [root@centoscc file1]# ls -l
    总用量 12
    -rw-r--r--. 1 root root 16 12月 20 21:19 cc4.txt
    -rw-r--r--. 1 root root 15 12月 20 21:21 cc.txt
    -rw-r--r--. 1 root root 16 12月 22 20:37 root1.txt

tar调用其他压缩程序

常用指令:

  • z:把tar包压缩成.tar.gz格式:

    # tar -czf file1.tar.gz cc*.txt   解压:# tar -xzf file1.tar.gz 

  • Z:把tar包压缩成.tar.Z
  • j:把tar包压缩成 .tar.bz2
原文地址:https://www.cnblogs.com/Cc905/p/14185962.html