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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/984204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:25:02  来源:igfitidea点击:

Shell command to tar directory excluding certain files/folders

linuxshellarchivetar

提问by deepwell

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=PATTERNcommand 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. I have tens of thousands.

我还可以使用 find 命令创建文件列表并排除我不想存档的文件并将列表传递给 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=fileto copy all the files to a tmp directory, and then use tar to archive that directory.

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

Can anybody think of a better/more efficient solution?

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

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

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

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

采纳答案by Charles Ma

You can have multiple exclude options for tar so

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

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

etc will work. Make sureto put --excludebeforethe source and destination items.

等会起作用。请务必--exclude之前的源和目标项目。

回答by Alex B

Use the find command in conjunction with the tar append (-r) option. This way you can add files to an existing tar in a single step, instead of a two pass solution (create list of files, create tar).

将 find 命令与 tar append (-r) 选项结合使用。通过这种方式,您可以一步将文件添加到现有的 tar,而不是两遍的解决方案(创建文件列表,创建 tar)。

find /dir/dir -prune ... -o etc etc.... -exec tar rvf ~/tarfile.tar {} \;

回答by Joe

Your best bet is to use find with tar, via xargs (to handle the large number of arguments). For example:

最好的办法是通过 xargs 将 find 与 tar 一起使用(以处理大量参数)。例如:

find / -print0 | xargs -0 tar cjf tarfile.tar.bz2

回答by Johan Soderberg

You can exclude directories with --excludefor tar.

您可以使用--excludefor tar排除目录。

If you want to archive everything except /usryou can use:

如果您想存档除/usr可以使用的所有内容之外的所有内容:

tar -zcvf /all.tgz / --exclude=/usr

In your case perhaps something like

在你的情况下,也许像

tar -zcvf archive.tgz arc_dir --exclude=dir/ignore_this_dir

回答by camh

You can use cpio(1) to create tar files. cpio takes the files to archive on stdin, so if you've already figured out the find command you want to use to select the files the archive, pipe it into cpio to create the tar file:

您可以使用 cpio(1) 创建 tar 文件。cpio 将文件归档到标准输入中,因此如果您已经找到了要用于选择归档文件的 find 命令,请将其通过管道传输到 cpio 以创建 tar 文件:

find ... | cpio -o -H ustar | gzip -c > archive.tar.gz

回答by Rob

I found this somewhere else so I won't take credit, but it worked better than any of the solutions above for my mac specific issues (even though this is closed):

我在其他地方发现了这个,所以我不会相信,但对于我的 mac 特定问题,它比上面的任何解决方案都更好(即使这是关闭的):

tar zc --exclude __MACOSX --exclude .DS_Store -f <archive> <source(s)>

回答by carlo

To avoid possible 'xargs: Argument list too long'errors due to the use of find ... | xargs ...when processing tens of thousands of files, you can pipe the output of finddirectly to tarusing find ... -print0 | tar --null ....

为避免在处理数万个文件时'xargs: Argument list too long'由于使用而可能出现的错误find ... | xargs ...,您可以将 的输出find直接tar通过管道传输到使用find ... -print0 | tar --null ....

# archive a given directory, but exclude various files & directories 
# specified by their full file paths
find "$(pwd -P)" -type d \( -path '/path/to/dir1' -or -path '/path/to/dir2' \) -prune \
   -or -not \( -path '/path/to/file1' -or -path '/path/to/file2' \) -print0 | 
   gnutar --null --no-recursion -czf archive.tar.gz --files-from -
   #bsdtar --null -n -czf archive.tar.gz -T -

回答by frommelmak

You can also use one of the "--exclude-tag" options depending on your needs:

您还可以根据需要使用“--exclude-tag”选项之一:

  • --exclude-tag=FILE
  • --exclude-tag-all=FILE
  • --exclude-tag-under=FILE
  • --exclude-tag=文件
  • --exclude-tag-all=文件
  • --exclude-tag-under=文件

The folder hosting the specified FILE will be excluded.

将排除托管指定 FILE 的文件夹。

回答by Stephen Donecker

Possible options to exclude files/directories from backup using tar:

使用 tar 从备份中排除文件/目录的可能选项:

Exclude files using multiple patterns

使用多种模式排除文件

tar -czf backup.tar.gz --exclude=PATTERN1 --exclude=PATTERN2 ... /path/to/backup

Exclude files using an exclude file filled with a list of patterns

使用填充了模式列表的排除文件排除文件

tar -czf backup.tar.gz -X /path/to/exclude.txt /path/to/backup

Exclude files using tags by placing a tag file in any directory that should be skipped

通过将标记文件放置在应跳过的任何目录中来排除使用标记的文件

tar -czf backup.tar.gz --exclude-tag-all=exclude.tag /path/to/backup

回答by George

Possible redundant answer but since I found it useful, here it is:

可能是多余的答案,但因为我发现它很有用,所以这里是:

While a FreeBSD root (i.e. using csh) I wanted to copy my whole root filesystem to /mnt but without /usr and (obviously) /mnt. This is what worked (I am at /):

虽然是 FreeBSD 根目录(即使用 csh),但我想将整个根文件系统复制到 /mnt 但没有 /usr 和(显然)/mnt。这是有效的(我在/):

tar --exclude ./usr --exclude ./mnt --create --file - . (cd /mnt && tar xvd -)

My whole point is that it was necessary (by putting the ./) to specifyto tar that the excluded directories where part of the greater directory being copied.

我的重点是有必要(通过放置./指定tar 排除目录,其中部分目录被复制。

My 0.02

我的 0.02