在 Solaris 中 gz 时如何排除文件;我想要 gz 目录包含子目录,但排除 /temp 目录和 *.gz 文件。这是我到目前为止所使用的;
tar -cvf api_v2.x.tar.gz --exclude *.gz --exclude ./temp api
其中仍然包括排除文件夹。
答案1
Solaristar
命令具有X
排除文件/目录的开关。请参阅手册页man tar
了解用法。
从手册页:
X
Exclude. Use the exclude-file argument as a file containing a list of relative path names for files (or directories) to be excluded from the tarfile when using the functions c, x, or t. Be careful of trailing white spaces. Also beware of leading white spaces, since, for each line in the excluded file, the entire line (apart from the newline) is used to match against the initial string of files to exclude. Lines in the exclude file are matched exactly, so an entry like "/var" does not exclude the /var directory if tar is backing up relative pathnames. The entry should read "./var" under these circumstances. The tar command does not expand shell metacharacters in the exclude file, so specifying entries like "*.o" does not have the effect of excluding all files with names suffixed with ".o". If a complex list of files is to be excluded, the exclude file should be generated by some means such as the find(1) command with appro- priate conditions. Multiple X arguments can be used, with one exclude-file per argu- ment. In the case where included files (see -I include-file oper- and) are also specified, the excluded files take precedence over all included files. If a file is specified in both the exclude-file and the include-file (or on the command line), it is excluded.
请注意第一段的最后一句:
如果要排除复杂的文件列表,则应通过某种方式(例如具有适当条件的 find(1) 命令)生成排除文件。
在最新的 Solaris 版本上,还有 GNU tar,可以与gtar
中的相应手册页一起使用man gtar
。该版本的特点是--exclude=PATTERN
例子
使用示例tar
:
/tmp$ find api/
api/
api/a.gz
api/temp
api/temp/b
api/b
/tmp$ cat api/exclude_from_tar
api/a.gz
api/temp
/tmp$ tar cvfX api.tar api/exclude_from_tar api
a api/ 0K
a api/a.gz excluded
a api/temp excluded
a api/exclude_from_tar 1K
a api/b 0K
/tmp$ tar tf api.tar
api/
api/exclude_from_tar
api/b
使用示例gtar
:
/tmp$ gtar cvf api.tar --exclude="*.gz" --exclude=temp api
api/
api/exclude_from_tar
api/b
/tmp$ tar tf api.tar
api/
api/exclude_from_tar
api/b
答案2
如果当前目录包含 .gz 文件,则需要引用*.gz
模式 ( "*.gz"
),否则 shell 将在 tar 看到它之前扩展该模式。
z
如果您希望 tar 生成 gzip 压缩档案,您还需要添加到参数列表中。