Linux tar命令:压缩目录但排除子目录

时间:2021-03-21 00:11:23

Scenario: I have a directory on a server that hosts my website that contains hundreds of user-submitted images. I am creating a backup script that takes all the images from the directory and compresses them into one .tar.gz file. The command I have so far is:

tar -czpf /path/to/backups/my_backup.tar.gz path/to/images/

Problem: No inside my path/to/images/ I have a directory called tmp/. When I run the command, I get a .tar.gz file containing all the image in the path/to/images/ directory and a subdirectory called tmp/.

Question: How can I get the command to skip/not include the tmp/ subdirectory in the .tar.gz file.

场景:我在托管我的网站的服务器上有一个目录,其中包含数百个用户提交的图像。我正在创建一个备份脚本,它从目录中获取所有图像并将它们压缩成一个.tar.gz文件。我到目前为止的命令是:tar -czpf /path/to/backups/my_backup.tar.gz path / to / images /问题:我的路径中没有/ / images /我有一个名为tmp /的目录。当我运行命令时,我得到一个.tar.gz文件,其中包含path / to / images /目录中的所有图像和一个名为tmp /的子目录。问题:如何让命令跳过/不包含.tar.gz文件中的tmp /子目录。

Thanks a million in advance.

提前一百万谢谢。

3 个解决方案

#1


You are looking for the --exclude argument.

您正在寻找--exclude参数。

--exclude=PATTERN

Some users find exclude options confusing. Here are some common pitfalls:

一些用户发现排除选项令人困惑。以下是一些常见的陷阱:

  • The main operating mode of tar does not act on a path name explicitly listed on the command line if one of its file name components is excluded. In the example above, if you create an archive and exclude files that end with *.o, but explicitly name the file dir.o/foo after all the options have been listed, dir.o/foo will be excluded from the archive.

    如果排除了其中一个文件名组件,则tar的主要操作模式不会对命令行中明确列出的路径名执行操作。在上面的示例中,如果您创建存档并排除以* .o结尾的文件,但在列出所有选项后明确命名文件dir.o / foo,则dir.o / foo将从存档中排除。

  • You can sometimes confuse the meanings of --exclude=PATTERN and --exclude-from=FILE-OF-PATTERNS (-X FILE-OF-PATTERNS). Be careful: use --exclude=PATTERN when files to be excluded are given as a pattern on the command line. Use --exclude-from=FILE-OF-PATTERNS to introduce the name of a file which contains a list of patterns, one per line; each of these patterns can exclude zero, one, or many files.

    有时你可能会混淆--exclude = PATTERN和--exclude-from = FILE-OF-PATTERNS(-X FILE-OF-PATTERNS)的含义。注意:当要排除的文件在命令行中作为模式给出时,请使用--exclude = PATTERN。使用--exclude-from = FILE-OF-PATTERNS来引入包含模式列表的文件名,每行一个;这些模式中的每一个都可以排除零个,一个或多个文件。

  • When you use --exclude=PATTERN, be sure to quote the PATTERN parameter, so GNU tar sees wildcard characters like *. If you do not do this, the shell might expand the * itself using files at hand, so tar might receive a list of files instead of one pattern, or none at all, making the command somewhat illegal. This might not correspond to what you want.

    当您使用--exclude = PATTERN时,请务必引用PATTERN参数,因此GNU tar会看到像*这样的通配符。如果你不这样做,shell可能会使用手头的文件扩展*本身,因此tar可能会收到一个文件列表而不是一个模式,或者根本没有,这使得命令有些违法。这可能与您想要的不符。

For example, write:

例如,写:

$ tar -c -f ARCHIVE.TAR --exclude '*.o' DIRECTORY

rather than:

$ tar -c -f ARCHIVE.TAR --exclude *.o DIRECTORY
  • You must use use shell syntax, or globbing, rather than regexp syntax, when using exclude options in tar. If you try to use regexp syntax to describe files to be excluded, your command might fail.
  • 在tar中使用排除选项时,必须使用use shell语法或globbing,而不是regexp语法。如果您尝试使用regexp语法来描述要排除的文件,那么您的命令可能会失败。

#2


tar -czpf /path/to/backups/my_backup.tar.gz --exclude path/to/images/tmp path/to/images/

#3


This should work (assuming GNU tar):

这应该工作(假设GNU tar):

tar -czpf /path/to/backups/my_backup.tar.gz --exclude=tmp path/to/images/

#1


You are looking for the --exclude argument.

您正在寻找--exclude参数。

--exclude=PATTERN

Some users find exclude options confusing. Here are some common pitfalls:

一些用户发现排除选项令人困惑。以下是一些常见的陷阱:

  • The main operating mode of tar does not act on a path name explicitly listed on the command line if one of its file name components is excluded. In the example above, if you create an archive and exclude files that end with *.o, but explicitly name the file dir.o/foo after all the options have been listed, dir.o/foo will be excluded from the archive.

    如果排除了其中一个文件名组件,则tar的主要操作模式不会对命令行中明确列出的路径名执行操作。在上面的示例中,如果您创建存档并排除以* .o结尾的文件,但在列出所有选项后明确命名文件dir.o / foo,则dir.o / foo将从存档中排除。

  • You can sometimes confuse the meanings of --exclude=PATTERN and --exclude-from=FILE-OF-PATTERNS (-X FILE-OF-PATTERNS). Be careful: use --exclude=PATTERN when files to be excluded are given as a pattern on the command line. Use --exclude-from=FILE-OF-PATTERNS to introduce the name of a file which contains a list of patterns, one per line; each of these patterns can exclude zero, one, or many files.

    有时你可能会混淆--exclude = PATTERN和--exclude-from = FILE-OF-PATTERNS(-X FILE-OF-PATTERNS)的含义。注意:当要排除的文件在命令行中作为模式给出时,请使用--exclude = PATTERN。使用--exclude-from = FILE-OF-PATTERNS来引入包含模式列表的文件名,每行一个;这些模式中的每一个都可以排除零个,一个或多个文件。

  • When you use --exclude=PATTERN, be sure to quote the PATTERN parameter, so GNU tar sees wildcard characters like *. If you do not do this, the shell might expand the * itself using files at hand, so tar might receive a list of files instead of one pattern, or none at all, making the command somewhat illegal. This might not correspond to what you want.

    当您使用--exclude = PATTERN时,请务必引用PATTERN参数,因此GNU tar会看到像*这样的通配符。如果你不这样做,shell可能会使用手头的文件扩展*本身,因此tar可能会收到一个文件列表而不是一个模式,或者根本没有,这使得命令有些违法。这可能与您想要的不符。

For example, write:

例如,写:

$ tar -c -f ARCHIVE.TAR --exclude '*.o' DIRECTORY

rather than:

$ tar -c -f ARCHIVE.TAR --exclude *.o DIRECTORY
  • You must use use shell syntax, or globbing, rather than regexp syntax, when using exclude options in tar. If you try to use regexp syntax to describe files to be excluded, your command might fail.
  • 在tar中使用排除选项时,必须使用use shell语法或globbing,而不是regexp语法。如果您尝试使用regexp语法来描述要排除的文件,那么您的命令可能会失败。

#2


tar -czpf /path/to/backups/my_backup.tar.gz --exclude path/to/images/tmp path/to/images/

#3


This should work (assuming GNU tar):

这应该工作(假设GNU tar):

tar -czpf /path/to/backups/my_backup.tar.gz --exclude=tmp path/to/images/