Shell 命令到 tar 目录,不包括某些文件/文件夹 [英] Shell command to tar directory excluding certain files/folders

查看:30
本文介绍了Shell 命令到 tar 目录,不包括某些文件/文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单的 shell 命令/脚本支持从存档中排除某些文件/文件夹?

Is there a simple shell command/script that supports excluding certain files/folders from being archived?

我有一个目录需要与一个子目录一起存档,该子目录中有许多我不需要备份的非常大的文件.

I have a directory that need to be archived with a sub directory that has a number of very large files I do not need to backup.

不是完全的解决方案:

tar --exclude=PATTERN 命令匹配给定的模式并排除这些文件,但我需要特定的文件 &要忽略的文件夹(完整文件路径),否则可能会排除有效文件.

The tar --exclude=PATTERN command matches the given pattern and excludes those files, but I need specific files & folders to be ignored (full file path), otherwise valid files might be excluded.

我还可以使用 find 命令创建文件列表并排除我不想存档的文件并将列表传递给 tar,但这仅适用于少量文件.我有几万.

I could also use the find command to create a list of files and exclude the ones I don't want to archive and pass the list to tar, but that only works with for a small amount of files. I have tens of thousands.

我开始认为唯一的解决方案是创建一个包含要排除的文件/文件夹列表的文件,然后使用带有 --exclude-from=file 的 rsync 复制所有文件到 tmp 目录,然后使用 tar 将该目录归档.

I'm beginning to think the only solution is to create a file with a list of files/folders to be excluded, then use rsync with --exclude-from=file to copy all the files to a tmp directory, and then use tar to archive that directory.

谁能想到更好/更有效的解决方案?

Can anybody think of a better/more efficient solution?

Charles Ma 的解决方案效果很好.最大的问题是 --exclude='./folder' 必须 位于 tar 命令的开头.完整命令(先cd,所以备份是相对于那个目录的):

Charles Ma's solution works well. The big gotcha is that the --exclude='./folder' MUST be at the beginning of the tar command. Full command (cd first, so backup is relative to that directory):

cd /folder_to_backup
tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

推荐答案

您可以为 tar 设置多个排除选项

You can have multiple exclude options for tar so

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

等会起作用.确保确保--exclude 放在源和目标项之前.

etc will work. Make sure to put --exclude before the source and destination items.

这篇关于Shell 命令到 tar 目录,不包括某些文件/文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆