【嵌入式开发 Linux 常用命令系列16.1 -- tar压缩 过滤掉某些类型文件】


请阅读嵌入式及芯片开发学必备专栏


tar压过滤掉某些类型文件

在使用tar 命令压缩时经常需要去掉对某些类型文件及目录的压缩,比如 不压缩 .log .git .o 文件及不压缩 tmp目和 tmp1/tmp2/tmp3 目录 。

在使用 tar 命令压缩时,可以使用 --exclude 选项来排除特定类型的文件和目录。以下是如何排除 .log.git.o 文件以及 tmp 目录和 tmp1/tmp2/tmp3 目录的示例。

tar --exclude='*.log' --exclude='*.git' --exclude='*.o' --exclude='tmp' --exclude='tmp1/tmp2/tmp3' -cvf archive.tar /path/to/directory

以下是对每个选项的解释:

  • --exclude='*.log':排除所有 .log 文件。
  • --exclude='*.git':排除所有 .git 文件或目录。
  • --exclude='*.o':排除所有 .o 文件。
  • --exclude='tmp':排除 tmp 目录及其所有内容。
  • --exclude='tmp1/tmp2/tmp3':排除 tmp1/tmp2/tmp3 目录及其所有内容。
  • -cvf archive.tar /path/to/directory:创建名为 archive.tar 的归档文件,包含 /path/to/directory 目录及其内容。

使用示例

假设目录结构如下:

/path/to/directory/
|-- file1.txt
|-- file2.log
|-- file3.o
|-- .git/
|-- tmp/
|-- tmp1/
    |-- tmp2/
        |-- tmp3/

使用以下命令将会创建一个名为 archive.tar 的归档文件,但不会包含 .log 文件、.git 目录、.o 文件、tmp 目录以及 tmp1/tmp2/tmp3 目录。

tar --exclude='*.log' --exclude='*.git' --exclude='*.o' --exclude='tmp' --exclude='tmp1/tmp2/tmp3' -cvf archive.tar /path/to/directory

可以自己创建个sh脚本,使用脚本来执行:

tar --exclude='*.log' --exclude='*.git' --exclude='*.o' \
    --exclude='*.xml' --exclude='*.bin' --exclude='*.out' \
    --exclude='*.files' --exclude='tags' --exclude='*.zip' \
    --exclude='*.pack' --exclude='*.idx' --exclude='*.out' \
    --exclude='*.rev' --exclude='*.txt' --exclude='*.hex' \
    --exclude='*.TXT' --exclude='*.html' --exclude='*.dummy' \
    --exclude='demo/Build' \
    --exclude='demo/out' \
    -cvf demo.tar demo

Summary

  • 使用相对路径时,确保在正确的目录中执行命令。
  • 使用绝对路径时,确保指定了正确的路径。
  • --exclude 选项可以重复使用来排除多个文件和目录。

这样就可以灵活地排除不需要压缩的文件和目录了。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主公CodingCos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值