在 Linux 中使用 tar 命令打包时,有多种方法可以过滤和排除指定的目录或文件。以下是各种排除方法的详细说明。
1. 基础排除方法
1.1 使用 --exclude
参数(单次排除)
bash
# 排除单个目录
tar -czvf backup.tar.gz --exclude='node_modules' /path/to/directory
# 排除多个目录
tar -czvf backup.tar.gz --exclude='node_modules' --exclude='.git' --exclude='tmp' /path/to/directory
1.2 使用 --exclude
参数(模式匹配)
bash
# 使用通配符排除所有匹配的目录
tar -czvf backup.tar.gz --exclude='*/node_modules' --exclude='*.log' /path/to/directory
# 排除所有隐藏目录和文件
tar -czvf backup.tar.gz --exclude='.*' /path/to/directory
2. 使用排除文件列表
2.1 创建排除文件
创建一个文本文件(如 exclude-list.txt
),列出所有要排除的目录和文件:
bash
# 创建排除文件
cat > exclude-list.txt << EOF
node_modules
.git
*.log
tmp/
build/
dist/
.cache
.DS_Store
*.tmp
EOF
2.2 使用排除文件打包
bash
# 使用排除文件
tar -czvf backup.tar.gz --exclude-from=exclude-list.txt /path/to/directory
# 或者使用长格式
tar -czvf backup.tar.gz -X exclude-list.txt /path/to/directory
3. 高级排除技巧
3.1 相对路径与绝对路径排除
bash
# 相对当前目录排除(推荐)
cd /path/to/directory
tar -czvf ../backup.tar.gz --exclude='node_modules' --exclude='.git' .
# 使用绝对路径排除
tar -czvf backup.tar.gz --exclude='/path/to/directory/node_modules' /path/to/directory
3.2 排除特定文件类型
bash
# 排除特定扩展名的文件
tar -czvf backup.tar.gz --exclude='*.log' --exclude='*.tmp' --exclude='*.swp' /path/to/directory
# 排除备份文件
tar -czvf backup.tar.gz --exclude='*~' --exclude='*.bak' /path/to/directory
3.3 使用 find 配合 tar 进行复杂过滤
bash
# 使用 find 查找文件并传递给 tar(排除 node_modules 和 .git)
find /path/to/directory -name '*' -type f ! -path '*/node_modules/*' ! -path '*/.git/*' -print0 | tar -czvf backup.tar.gz --null -T -
4. 实际应用示例
4.1 备份网站目录(排除缓存和日志)
bash
#!/bin/bash
BACKUP_FILE="website-backup-$(date +%Y%m%d).tar.gz"
EXCLUDE_FILE="/home/backup/exclude-list.txt"
# 创建排除列表
cat > $EXCLUDE_FILE << EOF
cache/
logs/
*.log
tmp/
uploads/temp_*
.env
.git
node_modules
EOF
# 执行备份
tar -czvf $BACKUP_FILE -X $EXCLUDE_FILE /var/www/html
4.2 备份用户数据(排除系统文件)
bash
# 备份 home 目录,排除常见不需要备份的目录
tar -czvf home-backup.tar.gz \
--exclude='*/Downloads' \
--exclude='*/.cache' \
--exclude='*/.local/share/Trash' \
--exclude='*/.npm' \
--exclude='*/.docker' \
/home/username
4.3 项目代码备份
bash
#!/bin/bash
PROJECT_DIR="/home/projects/myapp"
BACKUP_NAME="myapp-code-$(date +%Y%m%d).tar.gz"
tar -czvf $BACKUP_NAME \
-C $PROJECT_DIR \
--exclude='node_modules' \
--exclude='dist' \
--exclude='build' \
--exclude='.git' \
--exclude='*.log' \
--exclude='.env' \
--exclude='.DS_Store' \
.
5. 验证和查看排除效果
5.1 先测试排除效果(不实际打包)
bash
# 只列出将要被打包的文件,验证排除效果
tar -czvf /dev/null --exclude='node_modules' --exclude='.git' /path/to/directory 2>&1 | head -20
# 或者使用 tar -t 来测试
tar -czf test.tar.gz --exclude='node_modules' /path/to/directory
tar -tf test.tar.gz | head -20
rm test.tar.gz
5.2 查看打包内容
bash
# 查看打包文件内容,确认排除成功
tar -tzvf backup.tar.gz | less
6. 排除模式参考表
排除模式 | 说明 | 示例 |
---|---|---|
目录名/ |
排除指定目录 | --exclude='node_modules/' |
*/.git |
排除所有 .git 目录 | --exclude='*/.git' |
*.ext |
排除特定扩展名文件 | --exclude='*.log' |
.* |
排除所有隐藏文件 | --exclude='.*' |
目录/*.tmp |
排除目录下特定文件 | --exclude='tmp/*.tmp' |
7. 常见注意事项
7.1 引号使用
bash
# 当排除模式包含通配符时,使用引号
tar -czvf backup.tar.gz --exclude='*/node_modules' /path
# 如果不使用引号,shell可能会先展开通配符
tar -czvf backup.tar.gz --exclude=*/node_modules /path # 可能不会按预期工作
7.2 路径匹配规则
bash
# 这些规则是等价的
tar -czvf backup.tar.gz --exclude='node_modules' /path
tar -czvf backup.tar.gz --exclude='/path/node_modules' /path
tar -czvf backup.tar.gz --exclude='path/node_modules' /path
# 但使用相对路径更安全
cd /path && tar -czvf backup.tar.gz --exclude='node_modules' .
7.3 排除符号链接
bash
# 默认情况下,tar 会跟随符号链接,要排除符号链接可以:
tar -czvf backup.tar.gz --exclude='symlink_name' /path
# 或者使用 -h 选项不跟随符号链接
tar -czvhf backup.tar.gz --exclude='node_modules' /path
8. 实用脚本示例
8.1 智能备份脚本
bash
#!/bin/bash
# smart-backup.sh
BACKUP_DIR=$1
BACKUP_NAME=$(basename $BACKUP_DIR)-$(date +%Y%m%d-%H%M%S).tar.gz
EXCLUDE_LIST=(
"node_modules"
".git"
"*.log"
"tmp"
"build"
"dist"
".cache"
"*.tmp"
)
# 构建排除参数
EXCLUDE_PARAMS=""
for pattern in "${EXCLUDE_LIST[@]}"; do
EXCLUDE_PARAMS+=" --exclude='$pattern'"
done
# 执行备份
echo "Backing up $BACKUP_DIR to $BACKUP_NAME"
eval "tar -czvf $BACKUP_NAME $EXCLUDE_PARAMS $BACKUP_DIR"
echo "Backup completed: $BACKUP_NAME"
使用方式:
bash
chmod +x smart-backup.sh
./smart-backup.sh /path/to/your/project
通过掌握这些 tar 排除技巧,你可以更精确地控制打包内容,避免不必要的数据包含在备份或分发包中,从而节省存储空间和传输时间。