Linux tar命令:压缩目录,但排除子目录 [英] Linux tar command: compressing a directory but exluding subdirectories
问题描述
场景:我在托管我的网站的服务器上有一个目录,其中包含数百个用户提交的图片。我创建一个备份脚本,从目录中的所有图像,并将其压缩到一个.tar.gz文件。我到目前为止的命令是:
tar -czpf /path/to/backups/my_backup.tar.gz path / to / images /
问题:没有在我的路径/到/ images /我有一个名为tmp /的目录。当我运行命令时,我得到一个.tar.gz文件,其中包含路径/ to / images /目录中的所有映像和一个名为tmp /的子目录。
问题:如何获取命令跳过/不在.tar.gz文件中包含tmp /子目录。
提前100万美元。
您正在寻找 -exclude
参数。
- exclude = PATTERN
$ b b
有些用户发现 exclude
选项混乱。这里有一些常见的陷阱: c $> $ <$> 不会作用于在命令行上显式列出的路径名
,如果其文件名
组件之一被排除。在上面的示例中,如果创建一个
归档文件并排除以 *。o
结尾的文件,但显式地命名
文件 dir.o / foo
在列出所有选项后,
dir.o / foo
有时您可能会混淆 - exclude = PATTERN
和
- exclude-from = FILE-OF-PATTERNS
( -X FILE-OF-PATTERNS
)。 Be
注意:当要排除的文件是
时,使用 - exclude = PATTERN
作为命令行中的模式。使用
- exclude-from = FILE-OF-PATTERNS
引入文件名称
,其中包含模式列表,每行一个;
当您使用 - exclude = PATTERN时,这些
,请务必引用PATTERN
模式中的每一个都可以排除零个,一个或多个文件。
参数,因此GNU tar
会看到通配符,例如 *
。如果
不这样做,shell可能会使用文件
扩展 *
本身,因此 tar
可能会收到一个文件列表,而不是一个
模式,或者根本没有,使命令有点违法。
这可能与您想要的不一致。
例如,写:
$ tar -c -f ARCHIVE.TAR --exclude'* .o'DIRECTORY
而不是:
$ tar -c -f ARCHIVE .TAR --exclude * .o DIRECTORY
- 必须使用use shell语法或globbing,而不是
tar
中使用exclude选项时,regexp
语法。如果您尝试使用
regexp
语法来描述要排除的文件,您的命令
可能会失败。
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.
Thanks a million in advance.
You are looking for the --exclude
argument.
--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 filedir.o/foo
after all the options have been listed,dir.o/foo
will be excluded from the archive.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.When you use
--exclude=PATTERN
, be sure to quote the PATTERN parameter, so GNUtar
sees wildcard characters like*
. If you do not do this, the shell might expand the*
itself using files at hand, sotar
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.
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 intar
. If you try to useregexp
syntax to describe files to be excluded, your command might fail.
这篇关于Linux tar命令:压缩目录,但排除子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!