Linux tar:添加当前目录下的所有文件和目录,包括 .svn 等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3651791/
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 20:25:53  来源:igfitidea点击:

tar: add all files and directories in current directory INCLUDING .svn and so on

linuxbashdesign-patternstar

提问by Micha

I try to tar.gz a directory and use

我尝试 tar.gz 一个目录并使用

tar -czf workspace.tar.gz *

The resulting tar includes .svndirectories in subdirs but NOT in the current directory (as *gets expanded to only 'visible' files before it is passed to tar

生成的 tar 包括.svn子目录中的目录,但不在当前目录中(因为*在传递给 tar 之前被扩展为仅“可见”文件

I tried to

我试过了

tar -czf workspace.tar.gz .instead but then I am getting an error because '.' has changed while reading:

tar -czf workspace.tar.gz .相反,但后来我收到一个错误,因为 '.' 阅读时发生了变化:

tar: ./workspace.tar.gz: file changed as we read it

Is there a trick so that *matches all files (including dot-prefixed) in a directory?

有没有技巧可以*匹配目录中的所有文件(包括点前缀)?

(using bash on Linux SLES-11 (2.6.27.19)

(在 Linux SLES-11 (2.6.27.19) 上使用 bash)

回答by bramp

You can include the hidden directories by going back a directory and doing:

您可以通过返回目录并执行以下操作来包含隐藏目录:

cd ..
tar czf workspace.tar.gz workspace

Assuming the directory you wanted to gzip was called workspace.

假设您想要 gzip 的目录称为工作区。

回答by rkhayrov

If you reallydon't want to include top directory in the tarball (and that's generally bad idea):

如果您真的不想在 tarball 中包含顶级目录(这通常是个坏主意):

tar czf workspace.tar.gz -C /path/to/workspace .

回答by loevborg

A good question. In ZSH you can use the globbing modifier (D), which stands for "dotfiles". Compare:

一个好问题。在 ZSH 中,您可以使用 globbing 修饰符(D),它代表“dotfiles”。相比:

ls $HOME/*

and

ls $HOME/*(D)

This correctly excludes the special directory entries .and ... In Bash you can use .*to include the dotfiles explicitly:

这正确地排除了特殊目录条目.... 在 Bash 中,您可以使用.*显式包含点文件:

ls $HOME/* $HOME/.*

But that includes .and ..as well, so it's not what you were looking for. I'm sure there's some way to make *match dotfiles in bash, too.

但是,包括...为好,所以它不是你所期待的。我相信也有一些方法可以*在 bash 中匹配点文件。

回答by Peter Jaric

Update:I added a fix for the OP's comment.

更新:我为 OP 的评论添加了一个修复程序。

tar -czf workspace.tar.gz .

will indeed change the current directory, but why not place the file somewhere else?

确实会改变当前目录,但为什么不把文件放在其他地方呢?

tar -czf somewhereelse/workspace.tar.gz .
mv somewhereelse/workspace.tar.gz . # Update

回答by caf

You can fix the .form by using --exclude:

您可以.使用--exclude以下方法修复表单:

tar -czf workspace.tar.gz --exclude=workspace.tar.gz .

回答by Scott Thomson

tar -czf workspace.tar.gz .??* *

Specifying .??*will include "dot" files and directories that have at least 2 characters after the dot. The down side is it will not include files/directories with a single character after the dot, such as .a, if there are any.

指定.??*将包括点后至少有 2 个字符的“点”文件和目录。不利的一面是它不会包含点后带有单个字符的文件/目录,例如.a,如果有的话。

回答by Jonathan Leffler

Don't create the tar file in the directory you are packing up:

不要在你正在打包的目录中创建 tar 文件:

tar -czf /tmp/workspace.tar.gz .

does the trick, except it will extract the files all over the current directory when you unpack. Better to do:

这样做的技巧,除了它会在您解压缩时提取当前目录中的所有文件。最好这样做:

cd ..
tar -czf workspace.tar.gz workspace

or, if you don't know the name of the directory you were in:

或者,如果您不知道所在目录的名称:

base=$(basename $PWD)
cd ..
tar -czf $base.tar.gz $base

(This assumes that you didn't follow symlinks to get to where you are and that the shell doesn't try to second guess you by jumping backwards through a symlink - bashis not trustworthy in this respect. If you have to worry about that, use cd -P ..to do a physical change directory. Stupid that it is not the default behaviour in my view - confusing, at least, for those for whom cd ..never had any alternative meaning.)

(这假设您没有按照符号链接到达您所在的位置,并且 shell 不会尝试通过符号链接向后跳转来再次猜测您 -bash在这方面不值得信赖。如果您必须担心,用于cd -P ..进行物理更改目录。愚蠢的是,在我看来,这不是默认行为 - 至少对于那些cd ..从未有任何其他含义的人来说,这令人困惑。)



One comment in the discussion says:

讨论中的一条评论说:

I [...] need to exclude the top directory and I [...] need to place the tar in the base directory.

我 [...] 需要排除顶级目录,我 [...] 需要将 tar 放在基本目录中。

The first part of the comment does not make much sense - if the tar file contains the current directory, it won't be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).

注释的第一部分没有多大意义 - 如果 tar 文件包含当前目录,则从该存档中提取文件时不会创建它,因为根据定义,当前目录已经存在(除非在非常奇怪的情况下) )。

The second part of the comment can be dealt with in one of two ways:

可以通过以下两种方式之一处理注释的第二部分:

  1. Either: create the file somewhere else - /tmpis one possible location - and then move it back to the original location after it is complete.
  2. Or: if you are using GNU Tar, use the --exclude=workspace.tar.gzoption. The string after the =is a pattern - the example is the simplest pattern - an exact match. You might need to specify --exclude=./workspace.tar.gzif you are working in the current directory contrary to recommendations; you might need to specify --exclude=workspace/workspace.tar.gzif you are working up one level as suggested. If you have multiple tar files to exclude, use '*', as in --exclude=./*.gz.
  1. 要么:在其他地方创建文件 -/tmp是一个可能的位置 - 然后在完成后将其移回原始位置。
  2. 或者:如果您使用的是 GNU Tar,请使用该--exclude=workspace.tar.gz选项。后面的字符串=是一个模式——这个例子是最简单的模式——完全匹配。您可能需要指定--exclude=./workspace.tar.gz您是否在与建议相反的当前目录中工作;您可能需要指定--exclude=workspace/workspace.tar.gz您是否按照建议的方式工作到一个级别。如果要排除多个 tar 文件,请使用“ *”,如--exclude=./*.gz.

回答by Andrew Gunnerson

Using findis probably the easiest way:

使用find可能是最简单的方法:

find . -maxdepth 1 -exec tar zcvf workspace.tar.gz {} \+

find . -maxdepth 1will find all files/directories/symlinks/etc in the current directory and run the command specified by -exec. The {}in the command means file list goes hereand \+means that the command will be run as:

find . -maxdepth 1将找到当前目录中的所有文件/目录/符号链接/等并运行指定的命令-exec。将{}在指挥手段在这里文件不胜枚举\+手段,该命令将运行为:

tar zcvf workspace.tar.gz .file1 .file2 .dir3

instead of

代替

tar zcvf workspace.tar.gz .file1
tar zcvf workspace.tar.gz .file2
tar zcvf workspace.tar.gz .dir3

回答by s3v1

Actually the problem is with the compression options. The trick is the pipe the tar result to a compressor instead of using the built-in options. Incidentally that can also give you better compression, since you can set extra compresion options.

实际上问题在于压缩选项。诀窍是将 tar 结果通过管道传送到压缩器,而不是使用内置选项。顺便说一句,这也可以为您提供更好的压缩,因为您可以设置额外的压缩选项。

Minimal tar:

最小焦油:

tar --exclude=*.tar* -cf workspace.tar .

Pipe to a compressor of your choice. This example is verbose and uses xz with maximum compression:

管道连接到您选择的压缩机。这个例子很冗长,并使用 xz 进行最大压缩:

tar --exclude=*.tar* -cv . | xz -9v >workspace.tar.xz

Solution was tested on Ubuntu 14.04 and Cygwin on Windows 7. It's a community wiki answer, so feel free to edit if you spot a mistake.

解决方案在 Ubuntu 14.04 和 Windows 7 上的 Cygwin 上进行了测试。这是一个社区 wiki 答案,因此如果您发现错误,请随时进行编辑。

回答by empugandring

in directory want to compress (current directory) try this :

在目录中要压缩(当前目录)试试这个:

tar -czf workspace.tar.gz . --exclude=./*.gz