zoukankan      html  css  js  c++  java
  • Linux下tar命令exclude选项排除指定文件或目录

    在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子

    test 文件夹有如下文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@lee ~]# ll test
    总用量 8
    -rw-r--r--. 1 root root    0 4月  14 22:18 a.jpg
    -rw-r--r--. 1 root root    0 4月  14 22:25 a.log
    -rw-r--r--. 1 root root    0 4月  14 22:18 a.txt
    -rw-r--r--. 1 root root    0 4月  14 22:18 b.jpg
    -rw-r--r--. 1 root root    0 4月  14 22:25 b.log
    -rw-r--r--. 1 root root    0 4月  14 22:18 b.txt
    drwxr-xr-x. 2 root root 4096 4月  14 22:18 dir1
    drwxr-xr-x. 2 root root 4096 4月  14 22:18 dir2

    打包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@lee ~]#  tar -cvf test.tgz test/
    test/
    test/b.jpg
    test/b.txt
    test/dir2/
    test/b.log
    test/dir1/
    test/dir1/b.txt
    test/dir1/a.txt
    test/a.jpg
    test/a.txt
    test/a.log

    这样是打包全部文件,我们需要排除jpg文件可以这么弄

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@lee ~]#  tar -cvf test.tgz test/ --exclude *.jpg
    test/
    test/b.txt
    test/dir2/
    test/b.log
    test/dir1/
    test/dir1/b.txt
    test/dir1/a.txt
    test/a.txt
    test/a.log
    [root@lee ~]#

    这样,就会把jpg后缀的文件都排除了,包括子目录!

    如果是多个后缀类型需要被排除可以在后面添加,无限制

    1
    2
    3
    4
    5
    6
    7
    [root@lee ~]#  tar -cvf test.tgz test/ --exclude *.txt --exclude *.jpg
    test/
    test/dir2/
    test/b.log
    test/dir1/
    test/a.log
    [root@lee ~]#

    以上是匹配排除某个文件类型后缀,也可以直接指定文件名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@lee ~]#  tar -cvf test.tgz test/ --exclude a.txt
    test/
    test/b.jpg
    test/b.txt
    test/dir2/
    test/b.log
    test/dir1/
    test/dir1/b.txt
    test/a.jpg
    test/a.log
    [root@lee ~]#

    或者指定目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@lee ~]#  tar -cvf test.tgz test/ --exclude dir1
    test/
    test/b.jpg
    test/b.txt
    test/dir2/
    test/b.log
    test/a.jpg
    test/a.txt
    test/a.log
    [root@lee ~]#

    也可以排除目录与文件一起混合使用,如:

    1
    2
    3
    4
    5
    6
    7
    [root@lee ~]#  tar -cvf test.tgz test/ --exclude dir1 --exclude a.log --exclude *.jpg
    test/
    test/b.txt
    test/dir2/
    test/b.log
    test/a.txt
    [root@lee ~]#
  • 相关阅读:
    [转贴]Nexus7的编译过程。
    Office:SmartArt使用
    jira:导出的Excel无法打开
    TDT:关键字驱动测试方法
    SVN:“SVN”不是内部命令,解决方法
    VS:如何在VS2010中运行控制台程序时停留在控制台显示窗口
    Total Cammander中容合SVN
    NSIS:NSIS基础语法
    AppTimer.exe:程序启动时间测试工具
    MySql安装与GUi安装
  • 原文地址:https://www.cnblogs.com/you-jia/p/4337021.html
Copyright © 2011-2022 走看看