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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:07:50  来源:igfitidea点击:

Shell 'tar: not found in archive' error when using regular expression

linuxshelltar

提问by Zhiwen Fang

When I use tar -xzf *.gzto extract all the .gzfiles in the current directory, I get Not found in archiveerror. 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 tarcommand 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.gzand ghi.tar.gzfrom the archive abc.tar.gz. Since the files aren't in there, you get the warning message.

这被解释为从档案中提取def.tar.gz和提取的请求。由于文件不在那里,您会收到警告消息。ghi.tar.gzabc.tar.gz

In other words, taroperates on a single tarfile (possibly compressed) at a time (in one invocation). It does not operate on multiple tar files.

换句话说,一次(在一次调用中)tar对单个tar文件(可能是压缩的)进行操作。它不会对多个 tar 文件进行操作。

Note that if abc.tar.gzcontains 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 tara tarfile. 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 configurescript.)

(是的,文件可能是有原因tartar。例如,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 在您的当前目录中)。

tarthinks that you want to extract 2.gzand 3.gzfrom 1.gz; it can't find these files in the archives and that causes the error message.

tar认为要提取2.gz3.gz1.gz; 它在档案中找不到这些文件,这会导致错误消息。

You need to use loop forof command xargsto extract your files.

您需要使用for命令循环xargs来提取文件。

ls *.gz |xargs -n1 tar -xzf

That means: run me tar -xzffor every gz-file in the current directory.

这意味着:tar -xzfgz当前目录中的每个-file运行我。

回答by abasu

the main reason is tartakes a single filename argument. When you are calling tar *.tgzbash extends the *to all file names with tgz formant and it becomes tar file1 file2 file3which is not accepted by tar. So either you use a for loop like for i in *.tgz; do tar -zxvf $i ;doneor use some other command which executes taras a side effect like shown in comments, is lsor find . -maxdepth 1 -name \*tgz -exec tar -zxvf {} \;(lsoutput is always risky)

主要原因是tar采用单个文件名参数。当你调用tar *.tgzbash的扩展*与TGZ共振所有文件名,并成为tar file1 file2 file3其不被接受tar。因此,要么使用类似 for 循环,for i in *.tgz; do tar -zxvf $i ;done要么使用其他一些tar作为副作用执行的命令,如注释中所示,是lsfind . -maxdepth 1 -name \*tgz -exec tar -zxvf {} \;ls输出总是有风险的)