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

test 文件夹有如下文件

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

打包

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

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

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

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

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

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

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

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

或者指定目录

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

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

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

Linux tar命令exclude选项排除指定文件或目录的更多相关文章

  1. find tar排除指定文件或目录操作及查找文件内容关键字

    1.find查找排除单个目录 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk find . -path "./sk" -prune -o -name "*. ...

  2. (转)使用 linux tar 命令压缩与解压文件

    原文链接 http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r: ...

  3. Rsync多模块复制、排除指定文件及目录以及数据无差异复制的应用实例

    在我的博客<Rsync 数据复制软件应用>中,拉取数据访问的都是服务器端的/backup 目录,当然我们在其他目录下拉取数据.而实现这种操作就是指多模块复制. 要实现多模块复制首先需要修改 ...

  4. linux基础命令学习(二)文件和目录操作

    1.变换当前目录(change directory)     cd /home 进入 '/ home' 目录'  (change directory)   cd .. 返回上一级目录    cd .. ...

  5. Linux tar命令之--exclude参数 排除指定的文件或目录

    https://my.oschina.net/u/3285916/blog/1632552 参数: --exclude 打包时排除不需要处理的文件或目录 说明: tar -zcf a.tar.gz 打 ...

  6. [转载] linux下tar命令解压到指定的目录

    参考 http://blog.sina.com.cn/s/blog_62449fcf0100nfar.html linux下tar命令解压到指定的目录 :   #tar zxvf /bbs.tar.z ...

  7. linux下tar命令解压到指定的目录

    linux下tar命令解压到指定的目录 : #tar zxvf xx.tar.gz -C /xx//把根目录下的xx.tar.gz解压到/xx/下,前提要保证存在/xx这个目录 这个和cp命令有点不同 ...

  8. Linux tar命令高级用法——备份数据

    Linux tar命令高级用法——备份数据 2015-12-31 Linux学习 Linux上有功能强大的tar命令,tar最初是为了制作磁带备份(tape archive)而设计的,它的作用是把文件 ...

  9. Linux操作系统常用命令合集——第一篇-文件和目录操作(40个命令)

    一.选项和参数的区别        在经过上一次的系统安装后我们已经成功登陆,登陆LInux后,我们就可以在#或$符后面去输入命令,有的时候命令后面还会跟着“选项”(英文名:options)或“参数” ...

随机推荐

  1. iBATIS sqlMapConfig配置详解

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC & ...

  2. hdu4547 lca tarjan

    比较直接. #include<map> #include<queue> #include<stack> #include<cmath> #include ...

  3. POJ2437 Muddy roads

    Description Farmer John has a problem: the dirt road from his farm to town has suffered in the recen ...

  4. junit加载

    Run as junit 不会出现,把junit 包倒入lib文件夹中,在类的后面加上extends TestCase,此时上方会导入一个包:import junit.framework.TestCa ...

  5. ci控制器写规范

    不需要后缀名 文件名全部小写 所有控制器需要直接或者间接继承CI_Controller 以下划线开头的方法为私有方法,不能被请求 protected private的方法不能被浏览器请求 ci方法名不 ...

  6. ECSHOP用户协议字体颜色更改

    ECSHOP用户协议字体颜色更改 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-12   ECSHOP用户协议模板文件article_pro.dwt 这 ...

  7. 漂亮的title提示信息

    <HTML> <HEAD> <title>一种很酷的文字提示效果演示</title> <style> .tableBorder7{width ...

  8. C++ 中map 中迭代器的简单使用:

    public member function <map> std::map::find iterator find (const key_type& k); const_itera ...

  9. jQuery1.11源码分析(6)-----jQuery结构总揽

    (在看以下内容之前请先对原型链有一定的了解,比如:prototype是对象还是函数?) 在看jQuery的其他源码之前,必须对jQuery的数据结构有一定的了解. jQuery的核心很简单,jQuer ...

  10. [整理]Code::Blocks使用遇到的问题

    在使用其编写C小程序的过程总会遇到些问题,特整理如下: 1.无法调试 注意的是项目所在的文件路径不能包含中文. 2.头文件接口函数申明引用无效 查看头文件是否处于可编译状态,左侧项目文件列表里是文件名 ...