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
选项可以重复使用来排除多个文件和目录。
这样就可以灵活地排除不需要压缩的文件和目录了。