zoukankan      html  css  js  c++  java
  • tar

    tar
    语法格式:  tar  [选项]  [文件或目录]
     
    参数选项:
    z  通过gzip压缩或解压。
    c  创建新的tar包。
    v  显示详细的tar命令执行过程。
    f  指定压缩文件的名字。
    t  不解压,仅查看tar包的内容。
    p(小写)  保持文件的原有属性。
    P(大写)  以绝对路径打包,危险参数!!!
    j  通过bzip2命令压缩或压缩。
    x  解开tar包。
    C(大写)  指定解压的目录路径。
    --exclude=PATTERN  打包时不需要处理的文件或目录。若需要打包的目录为相对路径,则 --exclude后只能接相对路径。若需要打包的目录为绝对路径,则 --exclude后只能接绝对路径。
    -X 文件名   从指定文件读取不需要处理的文件或目录列表。
    -N 日期   仅打包比指定日期新的文件,可用于增量打包备份。
    -h  打包软链接文件指向的真实源文件。
    --hard-dereference  打包硬链接文件。
     
     
    备份目录下所有文件
    [root@testdb ~]# mkdir logs
    [root@testdb ~]# touch logs/{1..10}.log
    [root@testdb ~]# ls logs
    10.log  1.log  2.log  3.log  4.log  5.log  6.log  7.log  8.log  9.log
    [root@testdb ~]# tar -zcvf logs.tar.gz ./logs/
    ./logs/
    ./logs/2.log
    ./logs/10.log
    ./logs/1.log
    ./logs/3.log
    ./logs/7.log
    ./logs/6.log
    ./logs/5.log
    ./logs/4.log
    ./logs/8.log
    ./logs/9.log
    [root@testdb ~]# ls -lh
    total 8.0K
    drwxr-xr-x 2 root root 4.0K Dec 17 17:22 logs
    -rw-r--r-- 1 root root  215 Dec 17 17:23 logs.tar.gz
     
    
    查看压缩包的内容
    [root@testdb ~]# ls -lh logs.tar.gz
    -rw-r--r-- 1 root root  215 Dec 17 17:23 logs.tar.gz
    [root@testdb ~]# 
    [root@testdb ~]# tar -ztvf logs.tar.gz 
    drwxr-xr-x root/root         0 2020-12-17 17:22 ./logs/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/2.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/10.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/1.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/3.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/7.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/6.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/5.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/4.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/8.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/9.log
    
    
    解开压缩包到指定目录
    [root@testdb ~]# tar -zxvf logs.tar.gz -C /tmp/ 
    ./logs/
    ./logs/2.log
    ./logs/10.log
    ./logs/1.log
    ./logs/3.log
    ./logs/7.log
    ./logs/6.log
    ./logs/5.log
    ./logs/4.log
    ./logs/8.log
    ./logs/9.log
    [root@testdb ~]# 
    [root@testdb ~]# ll /tmp/logs/
    total 0
    -rw-r--r-- 1 root root 0 Dec 17 17:22 10.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 1.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 2.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 3.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 4.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 5.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 6.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 7.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 8.log
    -rw-r--r-- 1 root root 0 Dec 17 17:22 9.log
    
    
    排除打包
    注意:logs/testdir  目录结尾不要加 /  ,否则不会成功。
    排除2个以上目录的方法:并列使用多个 --exclude 
    排除一个目录
    [root@testdb ~]# mkdir logs/{testdir1,testdir2}
    [root@testdb ~]# ll logs/
    total 8
    -rw-r--r-- 1 root root    0 Dec 17 17:22 10.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 1.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 2.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 3.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 4.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 5.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 6.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 7.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 8.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 9.log
    drwxr-xr-x 2 root root 4096 Dec 17 18:37 testdir1
    drwxr-xr-x 2 root root 4096 Dec 17 18:37 testdir2
    [root@testdb ~]# tar -zcvf logs2.tar.gz ./logs/ --exclude=logs/testdir2 
    ./logs/
    ./logs/2.log
    ./logs/10.log
    ./logs/1.log
    ./logs/3.log
    ./logs/7.log
    ./logs/6.log
    ./logs/testdir1/
    ./logs/5.log
    ./logs/4.log
    ./logs/8.log
    ./logs/9.log
    [root@testdb ~]# ls -lh logs2.tar.gz 
    -rw-r--r-- 1 root root 243 Dec 17 18:38 logs2.tar.gz
    [root@testdb ~]# tar ztvf logs2.tar.gz 
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/2.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/10.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/1.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/3.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/7.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/6.log
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/testdir1/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/5.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/4.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/8.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/9.log
    
    
    排除多个文件
    [root@testdb ~]# ll ./logs/
    total 8
    -rw-r--r-- 1 root root    0 Dec 17 17:22 10.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 1.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 2.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 3.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 4.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 5.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 6.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 7.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 8.log
    -rw-r--r-- 1 root root    0 Dec 17 17:22 9.log
    drwxr-xr-x 2 root root 4096 Dec 17 18:37 testdir1
    drwxr-xr-x 2 root root 4096 Dec 17 18:37 testdir2
    [root@testdb ~]# tar -zcvf logs3.tar.gz ./logs/ --exclude=logs/9.log --exclude=logs/10.log 
    ./logs/
    ./logs/2.log
    ./logs/1.log
    ./logs/3.log
    ./logs/7.log
    ./logs/6.log
    ./logs/testdir1/
    ./logs/5.log
    ./logs/testdir2/
    ./logs/4.log
    ./logs/8.log
    [root@testdb ~]# tar ztvf logs3.tar.gz 
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/2.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/1.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/3.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/7.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/6.log
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/testdir1/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/5.log
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/testdir2/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/4.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/8.log
    
    
    排除多个目录
    [root@testdb ~]# tar -zcvf logs4.tar.gz ./logs/ --exclude=logs/testdir1 --exclude=logs/testdir2
    ./logs/
    ./logs/2.log
    ./logs/10.log
    ./logs/1.log
    ./logs/3.log
    ./logs/7.log
    ./logs/6.log
    ./logs/5.log
    ./logs/4.log
    ./logs/8.log
    ./logs/9.log
    [root@testdb ~]# tar ztvf logs4.tar.gz  
    drwxr-xr-x root/root         0 2020-12-17 18:37 ./logs/
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/2.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/10.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/1.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/3.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/7.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/6.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/5.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/4.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/8.log
    -rw-r--r-- root/root         0 2020-12-17 17:22 ./logs/9.log
    
    
    使用-h参数打包链接文件
    [root@testdb tmp]# cd /etc
    [root@testdb etc]# tar zcf local.tar.gz ./rc.local
    [root@testdb etc]# tar tvf local.tar.gz
    lrwxrwxrwx root/root         0 2018-08-08 11:11 ./rc.local -> rc.d/rc.local
    [root@testdb etc]# tar zchf my_local.tar.gz  ./rc.local 
    [root@testdb etc]# tar tfv my_local.tar.gz
    -rwxr-xr-x root/root       749 2020-11-29 18:23 ./rc.local
    
    说明:如果一个文件是链接文件,不实用-h参数打包的话只会对链接文件本身打包,而不是对链接文件指向的真实打包。
    
    
    打包/etc目录下所有的普通文件
    [root@testdb etc]# cd
    [root@testdb ~]# cd /etc/
    [root@testdb etc]# tar zcvf etc.tar.gz `find /etc/ -type f`



  • 相关阅读:
    如何查看openssl支持的所有TLS/SSL版本
    讲故事,学(AHK)设计模式—观察者模式
    React Hooks 详解 【近 1W 字】+ 项目实战
    为什么要在函数组件中使用React.memo?
    js防抖函数
    JS 深度优先遍历与广度优先遍历 实现查找
    你不知道的 requestIdleCallback
    RE:ゼロから始める文化課生活
    开学考小记 & 新生活的开始
    JS中:数组和对象的区别,以及遍历数组和遍历对象的区别
  • 原文地址:https://www.cnblogs.com/l10n/p/9416732.html
Copyright © 2011-2022 走看看