Linux 使用正则表达式时出现 Shell 'tar: not found in archive' 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16933221/
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
Shell 'tar: not found in archive' error when using regular expression
提问by Zhiwen Fang
When I use tar -xzf *.gz
to extract all the .gz
files in the current directory, I get Not found in archive
error. However, it works fine if I extract one by one or use a for-loop like
当我使用tar -xzf *.gz
提取.gz
当前目录中的所有文件时,Not found in archive
出现错误。但是,如果我一一提取或使用类似的 for 循环,它就可以正常工作
for file in `ls *.gz`; do tar -xzf $file; done
What is the reason for this error?
这个错误的原因是什么?
采纳答案by Jonathan Leffler
When you write:
当你写:
tar -xf *.gz
the tar
command sees (for example):
该tar
命令看到(例如):
tar -xf abc.tar.gz def.tar.gz ghi.tar.gz
This is interpreted as a request to extract def.tar.gz
and ghi.tar.gz
from the archive abc.tar.gz
. Since the files aren't in there, you get the warning message.
这被解释为从档案中提取def.tar.gz
和提取的请求。由于文件不在那里,您会收到警告消息。ghi.tar.gz
abc.tar.gz
In other words, tar
operates on a single tar
file (possibly compressed) at a time (in one invocation). It does not operate on multiple tar files.
换句话说,一次(在一次调用中)tar
对单个tar
文件(可能是压缩的)进行操作。它不会对多个 tar 文件进行操作。
Note that if abc.tar.gz
contains a file pqr/xyz/important.c
, you can extract just the one file by specifying:
请注意,如果abc.tar.gz
包含一个文件pqr/xyz/important.c
,您可以通过指定只提取一个文件:
tar -xf abc.tar.gz pqr/xyz/important.c
The notation you used is only a variant on this notation.
您使用的符号只是此符号的一个变体。
(And yes, there can be reasons to tar
a tar
file. For example, Gmail does not allow you to ship a tar file or a gzipped tar file which contains a file that is executable. However, if you embed a gzipped tar file inside a non-compressed tar file, it does not look inside the inner file to find the executable file. I use this when I need to ship a tar file with an executable configure
script.)
(是的,文件可能是有原因tar
的tar
。例如,Gmail 不允许您发送 tar 文件或包含可执行文件的 gzipped tar 文件。但是,如果您将 gzipped tar 文件嵌入到非-compressed tar 文件,它不会在内部文件中查找可执行文件。当我需要将 tar 文件与可执行configure
脚本一起发送时,我会使用它。)
回答by Igor Chubin
When you write
当你写
tar -xzf *.gz
your shell expands it to the string:
您的外壳将其扩展为字符串:
tar -xzf 1.gz 2.gz 3.gz
(assuming 1.gz, 2.gz and 3.gz are in you current directory).
(假设 1.gz、2.gz 和 3.gz 在您的当前目录中)。
tar
thinks that you want to extract 2.gz
and 3.gz
from 1.gz
; it can't find these files in the archives and that causes the error message.
tar
认为要提取2.gz
和3.gz
从1.gz
; 它在档案中找不到这些文件,这会导致错误消息。
You need to use loop for
of command xargs
to extract your files.
您需要使用for
命令循环xargs
来提取文件。
ls *.gz |xargs -n1 tar -xzf
That means: run me tar -xzf
for every gz
-file in the current directory.
这意味着:tar -xzf
为gz
当前目录中的每个-file运行我。
回答by abasu
the main reason is tar
takes a single filename argument. When you are calling tar *.tgz
bash extends the *
to all file names with tgz formant and it becomes tar file1 file2 file3
which is not accepted by tar
. So either you use a for loop like for i in *.tgz; do tar -zxvf $i ;done
or use some other command which executes tar
as a side effect like shown in comments, is ls
or find . -maxdepth 1 -name \*tgz -exec tar -zxvf {} \;
(ls
output is always risky)
主要原因是tar
采用单个文件名参数。当你调用tar *.tgz
bash的扩展*
与TGZ共振所有文件名,并成为tar file1 file2 file3
其不被接受tar
。因此,要么使用类似 for 循环,for i in *.tgz; do tar -zxvf $i ;done
要么使用其他一些tar
作为副作用执行的命令,如注释中所示,是ls
或find . -maxdepth 1 -name \*tgz -exec tar -zxvf {} \;
(ls
输出总是有风险的)