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

时间:2021-12-12 23:18:21

I try to tar.gz a directory and use

我试着焦油。一个目录和使用

tar -czf workspace.tar.gz *

The resulting tar includes .svn directories 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:

焦油-czf workspace.tar。广州。而是因为。在阅读时发生了变化:

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上使用bash) (2.6.27.19)

15 个解决方案

#1


42  

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 - bash is 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。执行物理更改目录。愚蠢的是,在我看来,这并不是默认的行为——至少对那些患有乳糜泻的人来说,这是令人困惑的。从来没有其他的意思。


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 - /tmp is one possible location - and then move it back to the original location after it is complete.
  2. 要么:在其他地方创建文件——/tmp是一个可能的位置——然后在文件完成后将其移回原始位置。
  3. Or: if you are using GNU Tar, use the --exclude=workspace.tar.gz option. The string after the = is a pattern - the example is the simplest pattern - an exact match. You might need to specify --exclude=./workspace.tar.gz if you are working in the current directory contrary to recommendations; you might need to specify --exclude=workspace/workspace.tar.gz if you are working up one level as suggested. If you have multiple tar files to exclude, use '*', as in --exclude=./*.gz.
  4. 或者:如果您正在使用GNU Tar,请使用—排除=workspace.tar。广州的选择。=后面的字符串是一个模式——示例是最简单的模式——一个精确的匹配。您可能需要指定——排除=./workspace.tar。如果您在当前目录下工作,与建议相反;您可能需要指定——排除=workspace/workspace.tar。如果你按照建议做一个级别的话。如果您要排除多个tar文件,请使用'*',如在——rejection =./*.gz中。

#2


39  

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

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

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

#3


14  

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

在想要压缩的目录(当前目录)中尝试:

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

#4


9  

There are a couple of steps to take:

有几个步骤可以采取:

  1. Replace * by . to include hidden files as well.
  2. 取代*。也包括隐藏文件。
  3. To create the archive in the same directory a --exclude=workspace.tar.gz can be used to exclude the archive itself.
  4. 要在相同的目录a中创建归档文件——排除=workspace.tar。gz可用于排除归档文件本身。
  5. To prevent the tar: .: file changed as we read it error when the archive is not yet created, make sure it exists (e.g. using touch), so the --exclude matches with the archive filename. (It does not match it the file does not exists)
  6. 为了防止tar: .:文件在未创建归档文件时读取错误时发生更改,请确保它存在(例如使用touch),因此—排除与归档文件名的匹配。(它不匹配文件不存在)

Combined this results in the following script:

将此结果与以下脚本结合在一起:

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

#5


6  

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

你可以修复。形式使用——排除:

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

#6


5  

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的目录被称为workspace。

#7


4  

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

#8


4  

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.

实际上,问题在于压缩选项。诀窍在于将焦油结果输送给压缩机,而不是使用内置选项。顺便说一句,这也可以给你更好的压缩,因为你可以设置额外的压缩选项。

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和Cygwin 7上进行了测试。这是一个社区wiki的答案,所以如果你发现了错误,请随时编辑。

#9


1  

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中,您可以使用.*显式地包含dotfiles:

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中也有一些方法可以使*匹配dotfiles。

#10


0  

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.

指定。? ?*将包含“点”文件和至少有两个字符的目录。缺点是它不会包含在点后只有一个字符的文件/目录,例如。a,如果有的话。

#11


0  

If disk space space is not an issue, this could also be a very easy thing to do:

如果磁盘空间不是问题,这也可以是一件非常容易的事情:

mkdir backup
cp -r ./* backup
tar -zcvf backup.tar.gz ./backup

#12


0  

The problem with the most solutions provided here is that tar contains ./ at the begging of every entry. So this results in having . directory when opening it through GUI compressor. So what I ended up doing is:

这里提供的大多数解决方案的问题是tar包含/请求每个条目。结果是。通过GUI压缩器打开目录。所以我最后做的是:

ls -1A | xargs -d "\n" tar cfz my.tar.gz

ls -1A | xargs -d“\n”tar cfz .tar.gz

If you already have my.tar.gz in current directory you may want to grep this out:

如果你已经有我的。在当前目录下的gz,您可能想要提示:

ls -1A | grep -v my.tar.gz | xargs -d "\n" tar cfz my.tar.gz

ls -1A | grep -v my.tar。gz | xargs -d“\n”tar cfz .gz

Be aware of that xargs has certain limit (see xargs --show-limits). So this solution would not work if you are trying to create a package which has lots of entries (directories and files) on a directory which you are trying to tar.

注意xargs有一定的极限(参见xargs—show-limit)。因此,如果您试图创建一个包含大量条目(目录和文件)的包,而您正在尝试使用tar,那么这个解决方案将不起作用。

#13


0  

Yet another solution, assuming the number of items in the folder is not huge:

另一种解决方案是,假设文件夹中条目的数量不是很大:

tar -czf workspace.tar.gz `ls -A`

(ls -A prints normal and hidden files but not "." and ".." as ls -a does.)

(ls -A打印普通文件和隐藏文件,但不像ls -A打印“.”和“.”。)

#14


0  

Had a similar situation myself. I think it is best to create the tar elsewhere and then use -C to tell tar the base directory for the compressed files. Example:

我也有过类似的经历。我认为最好在其他地方创建tar,然后使用-C告诉tar压缩文件的基本目录。例子:

tar -cjf workspace.tar.gz -C <path_to_workspace> $(ls -A <path_to_workspace>)

This way there is no need to exclude your own tarfile. As noted in other comments, -A will list hidden files.

这样就不需要排除您自己的tarfile。如其他评论所指出的,-A将列出隐藏的文件。

#15


-1  

Using find is probably the easiest way:

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

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

find . -maxdepth 1 will 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 here and \+ means that the command will be run as:

找到。-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

#1


42  

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 - bash is 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。执行物理更改目录。愚蠢的是,在我看来,这并不是默认的行为——至少对那些患有乳糜泻的人来说,这是令人困惑的。从来没有其他的意思。


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 - /tmp is one possible location - and then move it back to the original location after it is complete.
  2. 要么:在其他地方创建文件——/tmp是一个可能的位置——然后在文件完成后将其移回原始位置。
  3. Or: if you are using GNU Tar, use the --exclude=workspace.tar.gz option. The string after the = is a pattern - the example is the simplest pattern - an exact match. You might need to specify --exclude=./workspace.tar.gz if you are working in the current directory contrary to recommendations; you might need to specify --exclude=workspace/workspace.tar.gz if you are working up one level as suggested. If you have multiple tar files to exclude, use '*', as in --exclude=./*.gz.
  4. 或者:如果您正在使用GNU Tar,请使用—排除=workspace.tar。广州的选择。=后面的字符串是一个模式——示例是最简单的模式——一个精确的匹配。您可能需要指定——排除=./workspace.tar。如果您在当前目录下工作,与建议相反;您可能需要指定——排除=workspace/workspace.tar。如果你按照建议做一个级别的话。如果您要排除多个tar文件,请使用'*',如在——rejection =./*.gz中。

#2


39  

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

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

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

#3


14  

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

在想要压缩的目录(当前目录)中尝试:

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

#4


9  

There are a couple of steps to take:

有几个步骤可以采取:

  1. Replace * by . to include hidden files as well.
  2. 取代*。也包括隐藏文件。
  3. To create the archive in the same directory a --exclude=workspace.tar.gz can be used to exclude the archive itself.
  4. 要在相同的目录a中创建归档文件——排除=workspace.tar。gz可用于排除归档文件本身。
  5. To prevent the tar: .: file changed as we read it error when the archive is not yet created, make sure it exists (e.g. using touch), so the --exclude matches with the archive filename. (It does not match it the file does not exists)
  6. 为了防止tar: .:文件在未创建归档文件时读取错误时发生更改,请确保它存在(例如使用touch),因此—排除与归档文件名的匹配。(它不匹配文件不存在)

Combined this results in the following script:

将此结果与以下脚本结合在一起:

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

#5


6  

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

你可以修复。形式使用——排除:

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

#6


5  

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的目录被称为workspace。

#7


4  

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

#8


4  

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.

实际上,问题在于压缩选项。诀窍在于将焦油结果输送给压缩机,而不是使用内置选项。顺便说一句,这也可以给你更好的压缩,因为你可以设置额外的压缩选项。

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和Cygwin 7上进行了测试。这是一个社区wiki的答案,所以如果你发现了错误,请随时编辑。

#9


1  

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中,您可以使用.*显式地包含dotfiles:

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中也有一些方法可以使*匹配dotfiles。

#10


0  

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.

指定。? ?*将包含“点”文件和至少有两个字符的目录。缺点是它不会包含在点后只有一个字符的文件/目录,例如。a,如果有的话。

#11


0  

If disk space space is not an issue, this could also be a very easy thing to do:

如果磁盘空间不是问题,这也可以是一件非常容易的事情:

mkdir backup
cp -r ./* backup
tar -zcvf backup.tar.gz ./backup

#12


0  

The problem with the most solutions provided here is that tar contains ./ at the begging of every entry. So this results in having . directory when opening it through GUI compressor. So what I ended up doing is:

这里提供的大多数解决方案的问题是tar包含/请求每个条目。结果是。通过GUI压缩器打开目录。所以我最后做的是:

ls -1A | xargs -d "\n" tar cfz my.tar.gz

ls -1A | xargs -d“\n”tar cfz .tar.gz

If you already have my.tar.gz in current directory you may want to grep this out:

如果你已经有我的。在当前目录下的gz,您可能想要提示:

ls -1A | grep -v my.tar.gz | xargs -d "\n" tar cfz my.tar.gz

ls -1A | grep -v my.tar。gz | xargs -d“\n”tar cfz .gz

Be aware of that xargs has certain limit (see xargs --show-limits). So this solution would not work if you are trying to create a package which has lots of entries (directories and files) on a directory which you are trying to tar.

注意xargs有一定的极限(参见xargs—show-limit)。因此,如果您试图创建一个包含大量条目(目录和文件)的包,而您正在尝试使用tar,那么这个解决方案将不起作用。

#13


0  

Yet another solution, assuming the number of items in the folder is not huge:

另一种解决方案是,假设文件夹中条目的数量不是很大:

tar -czf workspace.tar.gz `ls -A`

(ls -A prints normal and hidden files but not "." and ".." as ls -a does.)

(ls -A打印普通文件和隐藏文件,但不像ls -A打印“.”和“.”。)

#14


0  

Had a similar situation myself. I think it is best to create the tar elsewhere and then use -C to tell tar the base directory for the compressed files. Example:

我也有过类似的经历。我认为最好在其他地方创建tar,然后使用-C告诉tar压缩文件的基本目录。例子:

tar -cjf workspace.tar.gz -C <path_to_workspace> $(ls -A <path_to_workspace>)

This way there is no need to exclude your own tarfile. As noted in other comments, -A will list hidden files.

这样就不需要排除您自己的tarfile。如其他评论所指出的,-A将列出隐藏的文件。

#15


-1  

Using find is probably the easiest way:

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

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

find . -maxdepth 1 will 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 here and \+ means that the command will be run as:

找到。-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