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

查看:267
本文介绍了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.

我开始认为唯一的解决方案是创建一个包含要排除的文件/文件夹列表的文件,然后将rsync与--exclude-from=file一起使用,将所有文件复制到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?

查尔斯·马的解决方案效果很好.最大的难题是--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天全站免登陆