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

Linux下tar命令exclude选项排除指定文件或目录

  • test 文件夹有如下文件
[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
  • 打包
[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文件可以这么弄

[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后缀的文件都排除了,包括子目录!

  • 如果是多个后缀类型需要被排除可以在后面添加,无限制
[root@lee ~]#  tar -cvf test.tgz test/ --exclude *.txt --exclude *.jpg
test/
test/dir2/
test/b.log
test/dir1/
test/a.log
[root@lee ~]#
  • 以上是匹配排除某个文件类型后缀,也可以直接指定文件名
[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 ~]#
或者指定目录
[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 ~]#
  • 也可以排除目录与文件一起混合使用,如:
[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 ~]#

最新文章

  1. Android开发5:应用程序窗口小部件App Widgets的实现
  2. 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法
  3. JQuery移除事件
  4. 【英语】Bingo口语笔记(6) - 表示“迷茫”
  5. node express
  6. JS数组删除一个元素(根据值删)
  7. PHP面试题之算法解析
  8. ASP.NET - 编写让别人能读懂的代码
  9. kNN(K-Nearest Neighbor)最近的分类规则
  10. 使用rdesktop远程连接Windows桌面
  11. (二) V4L2引入(含浅析UVC)
  12. Android中的分层----service 层,domain层,dao 层,action层等设计
  13. 群等变网络的pytorch实现
  14. IDEA引入Gradle工程小记
  15. Android开发环境内容汇总
  16. js 转义
  17. 怎么才能将文件流或者图片转化为base64,传到前台展示
  18. 问题-XE10.2开发Datasnap时提示"provider not exported datasetprovider1"
  19. SwiftMailer 发送邮件时 提示fsockopen() 被禁用
  20. React开发博客系统的总结

热门文章

  1. 【AIX】在命令前显示完整路径
  2. Ubuntu中的Gif动画录制工具
  3. java while循环语句
  4. 转:体积阴影(Shadow Volumes)生成算法
  5. Linux经常使用命令-权限管理命令-其它权限管理命令
  6. JavaScript 风格指南
  7. (原)tensorflow中finetune某些层
  8. MATLAB 向量
  9. java hibernate session create
  10. 命令行参数解析函数getopt和getopt_long函数【转】