Shell命令到tar目录,不包括某些文件/文件夹

喜你入骨 提交于 2020-08-13 13:44:20

问题:

Is there a simple shell command/script that supports excluding certain files/folders from being archived? 是否有一个简单的shell命令/脚本支持将某些文件/文件夹排除在存档之外?

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. 我有一个目录,该目录需要与一个子目录一起存档,该子目录包含许多不需要备份的非常大的文件。

Not quite solutions: 不完全解决方案:

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. tar --exclude=PATTERN命令匹配给定的模式并排除了这些文件,但是我需要忽略特定的文件和文件夹(完整的文件路径),否则可能会排除有效的文件。

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. 我还可以使用find命令创建文件列表,并排除我不想存档的文件并将该列表传递给tar,但这仅适用于少量文件。 I have tens of thousands. 我有成千上万。

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

Can anybody think of a better/more efficient solution? 谁能想到更好/更有效的解决方案?

EDIT: Charles Ma 's solution works well. 编辑: Charles Ma的解决方案效果很好。 The big gotcha is that the --exclude='./folder' MUST be at the beginning of the tar command. 最大的难题是--exclude='./folder' 必须位于tar命令的开头。 Full command (cd first, so backup is relative to that directory): 完整命令(首先是cd,因此备份是相对于该目录的):

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

解决方案:

参考一: https://stackoom.com/question/482G/Shell命令到tar目录-不包括某些文件-文件夹
参考二: https://oldbug.net/q/482G/Shell-command-to-tar-directory-excluding-certain-files-folders
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!