zoukankan      html  css  js  c++  java
  • 每天一个Linux命令(24)tar命令

        tar命令可以为linux的文件和目录创建档案。

        (1)用法:

        用法:  tar  [选项]   [文件参数]

        (2)功能:

        功能:  用来压缩和解压文件。tar本身不具有压缩功能。它是调用压缩功能实现的。

        利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。

        要弄清两个概念:打包和压缩

        打包是指将一大堆文件或目录变成一个总的文件;压缩则是将一个大的文件通过一些压缩算法变成一个小文件。

        为什么要区分这两个概念呢?

        这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆文件时,你得先将这一大堆文件先打成一个包(tar命令),然后再用压缩程序进行压缩(gzip bzip2命令)。

        (3)选项参数:

          1) -c   --create             建立新的备份文件

          2) -z                    支持gzip解压文件

          3) -j                    支持bzip2解压文件

          4) -v  --verbose             显示指令执行过程

          5) -f<备份文件> --file=<备份文件>    指定备份文件

          6) -t或--list                列出备份文件的内容
          7) -N<日期格式>--newer=<日期时间>   只将较指定日期更新的文件保存到备份文件里
          8) -x或--extract或--get            从备份文件中还原文件

          9) -C <目录>               这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项

        10) -P  --absolute-names            文件名使用绝对名称,不移除文件名称前的“/”号

        (4)实例:

          1)[root@localhost Documents]# tar -cvf Dir.tar Dir           f选项参数是必不可少的,这里以普通压缩格式压缩文件夹   

    [root@localhost Documents]# tar -cvf Dir.tar Dir
    Dir/
    Dir/head_text
    Dir/less1
    Dir/less2
    [root@localhost Documents]# ll
    总用量 28
    -rwx--xrwx. 1 root root    27 5月  19 04:21 core.log
    dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
    -rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
    -r--r--r--. 1 root root     0 5月  19 04:16 find
    dr--r--r--. 2 root root    84 5月  19 04:57 findDir
    -r--r--r--. 1 root root     0 5月  15 18:21 newlocate
    -rw-r--r--. 1 root root    85 5月  19 04:25 t3.txt
    --w-------. 1 root root   259 5月  12 21:53 tail_text
    --w-------. 1 root root   216 5月  12 22:24 tempory
    --w-------. 1 root root     0 5月  15 18:34 uText

          2)[root@localhost Documents]# tar -czvf Dir.tar.gz Dir        以其他格式压缩有Gzip和bzip2两种格式

    [root@localhost Documents]# tar -czvf Dir.tar.gz Dir
    Dir/
    Dir/head_text
    Dir/less1
    Dir/less2
    [root@localhost Documents]# tar -cjvf Dir.tar.bz2 Dir
    Dir/
    Dir/head_text
    Dir/less1
    Dir/less2
    [root@localhost Documents]# ll
    总用量 40
    -rwx--xrwx. 1 root root    27 5月  19 04:21 core.log
    dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
    -rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
    -rw-r--r--. 1 root root   646 5月  21 06:35 Dir.tar.bz2                 //公认的后缀名,以bzip2压缩
    -rw-r--r--. 1 root root   656 5月  21 06:30 Dir.tar.gz                  //公认的后缀名,以Gzip压缩
    -r--r--r--. 1 root root     0 5月  19 04:16 find
    dr--r--r--. 2 root root    84 5月  19 04:57 findDir
    -r--r--r--. 1 root root     0 5月  15 18:21 newlocate
    -rw-r--r--. 1 root root    85 5月  19 04:25 t3.txt
    --w-------. 1 root root   259 5月  12 21:53 tail_text
    --w-------. 1 root root   216 5月  12 22:24 tempory
    --w-------. 1 root root     0 5月  15 18:34 uText
    -rw-r--r--. 1 root root   105 5月  21 06:35 vf

          3)[root@localhost Documents]# tar -ztvf Dir.tar.gz          查阅上述tar包内有哪些文件,按什么格式压缩就要按照什么格式解压    

    [root@localhost Documents]# tar -ztvf Dir.tar.gz
    dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
    -r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
    -r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
    -r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2
    [root@localhost Documents]# tar -jtvf Dir.tar.bz2
    dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
    -r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
    -r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
    -r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2

          4)[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir          解压tar.gz压缩包,到指定名的文件夹,必须是Dir,不能变

    [root@localhost findDir]# tar -zxvf Dir.tar.gz Dir
    Dir/
    Dir/head_text
    Dir/less1
    Dir/less2
    [root@localhost findDir]# ll
    总用量 20
    dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
    -rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
    -rw-r--r--. 1 root root   646 5月  21 06:35 Dir.tar.bz2
    -rw-r--r--. 1 root root   656 5月  21 06:30 Dir.tar.gz
    -r--r--r--. 1 root root     0 5月  17 04:18 p1.pdf
    -r--r--r--. 1 root root     0 5月  17 04:18 p2.pdf
    -r--r--r--. 1 root root     0 5月  17 03:50 t1.txt
    -r--r--r--. 1 root root     0 5月  17 04:02 T1.txt
    -r--r--r--. 1 root root     0 5月  19 04:58 t2.txt
    -r--r--r--. 1 root root     0 5月  17 04:02 T2.txt

          5)[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1    只将tar内的部分文件解压出来,要进行匹配,如果不匹配会报错“归档找不到”   

    [root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1
    Dir/less1

          6)[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText     文件备份下来,并且保存其权限

    [root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText
    find
    t3.txt
    vf
    uText

          7)[root@localhost Documents]# tar -pzxvf P.tar.gz -C Pdir         指定解压的目录

    [root@localhost Documents]# tar -zcvf P1.tar.gz find t3.txt vf uText               //没有p参数的打包并压缩
    find
    t3.txt
    vf
    uText
    [root@localhost Documents]# ll
    总用量 32
    -rwx--xrwx. 1 root root   27 5月  19 04:21 core.log
    dr-xr-xr-x. 2 root root   46 5月  19 23:29 Dir
    -r--r--r--. 1 root root    0 5月  19 04:16 find
    dr--r--r--. 3 root root 4096 5月  21 06:52 findDir
    -r--r--r--. 1 root root    0 5月  15 18:21 newlocate
    -rw-r--r--. 1 root root  317 5月  21 07:06 P1.tar.gz                    //没有p参数
    -rw-r--r--. 1 root root  317 5月  21 07:05 P.tar.gz                     //有p参数
    -rw-r--r--. 1 root root   85 5月  19 04:25 t3.txt
    --w-------. 1 root root  259 5月  12 21:53 tail_text
    --w-------. 1 root root  216 5月  12 22:24 tempory
    --w-------. 1 root root    0 5月  15 18:34 uText
    -rw-r--r--. 1 root root  105 5月  21 06:35 vf
    [root@localhost Documents]# mkdir Pdir
    [root@localhost Documents]# tar -zxvf P.tar.gz -C Pdir                  //指定解压缩的路径
    find
    t3.txt
    vf
    uText
    [root@localhost Documents]# mkdir NoPdir
    [root@localhost Documents]# tar -zxvf P1.tar.gz -C NoPdir
    find
    t3.txt
    vf
    uText
    [root@localhost Documents]# ls -l Pdir                         //查看有p无p的区别
    总用量 8
    -r--r--r--. 1 root root   0 5月  19 04:16 find
    -rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
    --w-------. 1 root root   0 5月  15 18:34 uText
    -rw-r--r--. 1 root root 105 5月  21 06:35 vf
    [root@localhost Documents]# ls -l NoPdir
    总用量 8
    -r--r--r--. 1 root root   0 5月  19 04:16 find
    -rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
    --w-------. 1 root root   0 5月  15 18:34 uText
    -rw-r--r--. 1 root root 105 5月  21 06:35 vf          //并没有发现有什么区别

          8)[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*      在文件夹当中,比某个日期新的文件才备份

    [root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*
    tar: 选项 --after-date: 将日期 ‘2016/05/20’ 当作 2016-05-20 00:00:00
    ./520.tar.gz
    tar: ./520.tar.gz:文件缩小 45 字节;用零填充
    ./core.log
    ./Dir/
    ./Dir/head_text
    ./Dir/less1
    ./Dir/less2
    ./find
    ./findDir/
    ./findDir/t1.txt
    ./findDir/T1.txt
    ./findDir/T2.txt
    ./findDir/p1.pdf
    ./findDir/p2.pdf
    ./findDir/t2.txt
    ./findDir/Dir.tar
    ./findDir/Dir.tar.gz
    ./findDir/Dir.tar.bz2
    ./findDir/Dir/
    ./findDir/Dir/head_text
    ./findDir/Dir/less2
    ./findDir/Dir/less1
    ./log17.tar.gz
    ./newlocate
    ./NoPdir/
    ./NoPdir/find
    ./NoPdir/t3.txt
    ./NoPdir/vf
    ./NoPdir/uText
    ./P1.tar.gz
    ./Pdir/
    ./Pdir/find
    ./Pdir/t3.txt
    ./Pdir/vf
    ./Pdir/uText
    ./P.tar.gz
    tar: ./t3.txt: 文件未改变;未输出
    tar: ./tail_text: 文件未改变;未输出
    tar: ./tempory: 文件未改变;未输出
    tar: ./uText: 文件未改变;未输出
    ./vf

          9)[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir      打包时不包括特定目录下的文件

    [root@localhost Documents]# tar --exclude ./Dir/less1 -zcvf Dir.test.tar.gz Dir       //这里目录加个./,经后续验证,没有达到不包括的效果
    Dir/
    Dir/head_text
    Dir/less1
    Dir/less2
    [root@localhost Documents]# tar -ztvf Dir.test.tar.gz
    dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
    -r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
    -r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
    -r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2
    [root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir
    Dir/
    Dir/head_text
    Dir/less2
    [root@localhost Documents]# tar -ztvf Dir.test2.tar.gz
    dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
    -r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
    -r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2

          (5)其他:

          利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件。tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案。 

  • 相关阅读:
    ubuntu安装mysql并使用Navicat连接
    二叉树基础知识点
    Javascript 16进制转有符号的10进制整数
    Node.js 学习笔记(二)
    API测试如何演变为应用程序开发的常规部分
    API在线文档
    API文档自动生成的方法
    快速对比API版本
    如何使用OPENAPI进行质量检查
    【分享】什么是API网关?大公司为什么都有API网关?
  • 原文地址:https://www.cnblogs.com/MenAngel/p/5515792.html
Copyright © 2011-2022 走看看