如何一次解压多个文件?
我的目录中有一堆 tar
文件,我想立即从中提取所有文件。 但这似乎没有任何作用:
$ tar xf *.tar
这里发生了什么? 如何一次解压一堆文件?
I have a bunch of tar
files in a directory and I want to extract all the files from them at once. But this doesn't seem to do anything:
$ tar xf *.tar
What's going on here? How do I untar a bunch of files at once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果所有 tar 文件都在同一文件夹中,那么我在 tcsh shell 中执行此操作。 一直有效。
这与上面的答案相同,我认为更简洁一些。
If all the tar files are in the same folder, then I do this in tcsh shell. Works all the time.
This is same as the above answer, a little bit more concise I think.
这里发生了什么?
最初,
tar
命令旨在与磁带设备一起使用。 由于一次只在一台设备上执行tar
才有意义,因此该语法被设计为假定只有一台设备。 假定传递的第一个文件或目录是保存相关存档的设备,以及操作中包含存档内容的任何其他文件或目录。 因此,对于tar
提取(x
选项),传递的第一个文件将是存档,所有其他文件将是要提取的文件。 因此,如果有两个*.tar
文件(例如a.tar
和b.tar
),您的命令将扩展为:除非
a.tar
包含一个名为b.tar
的文件,tar
命令无所事事并安静退出。 令人烦恼的是,Solaris 版本的tar
不会报告返回代码或详细选项 (v
) 中的任何问题。 同时,即使关闭了详细选项,GNU tar
也会返回2
和垃圾邮件STDERR
:如何一次解压一堆文件?
重写
tar
来接受多个存档文件作为输入为时已晚,但解决该限制并不难。对于大多数人来说,多次运行
tar
来获取多个存档是最方便的选择。 仅将一个文件名传递给tar xf
就会按照预期提取所有存档文件。 一种方法是使用 shellfor
循环:另一种方法是使用
xargs
:或者,您可以使用多个 替代
tar
文件读取器。 最后,真正敬业的程序员可以轻松编写完全按照预期工作的tar
替代品。 格式很简单,许多编程语言都有可供读取的库tar
文件。 例如,如果您是 Perl 程序员,请查看Archive::Tar< /code>
模块。
警告
盲目解压一堆文件可能会导致意想不到的问题。 最明显的是,一个特定的文件名可能包含在多个
tar
文件中。 由于tar
默认情况下会覆盖文件,因此最终得到的文件的确切版本将取决于存档的处理顺序。 更麻烦的是,如果您尝试这种“聪明”的优化,您最终可能会得到一个损坏的文件副本:如果
a.tar
和b.tar
都包含相同的文件并同时尝试提取它,结果是不可预测的。一个相关的问题是tarbomb的可能性,尤其是从不受信任的来源获取档案时。
一种部分解决方案是自动创建一个新目录以提取到其中:
如果在存档中使用绝对路径指定文件(这通常是恶意意图的迹象),这将无济于事。 添加此类检查留给读者作为练习。
What's going on here?
Originally, the
tar
command was intended for use with magnetic tape devices. Since it only made sense to executetar
on one device at a time, the syntax was designed to assume one and only one device. The first file or directory passed was assumed to be the device that held the archive in question and any other files or directories where the contents of the archive to be included in the operation. So fortar
extraction (thex
option), the first file passed would be the archive and all other files would be the files to be extracted. So if there are two*.tar
files (saya.tar
andb.tar
) your command would expand to:Unless
a.tar
contains a file namedb.tar
, thetar
command has nothing to do and exits quietly. Annoyingly, the Solaris version oftar
does not report any problems either in the return code or with the verbose option (v
). Meanwhile,GNU tar
returns2
and spamsSTDERR
even with the verbose option off:How do I untar a bunch of files at once?
It's too late rewrite
tar
to accept multiple archive files as input, but it's not too hard to work around the limitation.For most people, running
tar
multiple times for multiple archives is the most expedient option. Passing just one filename totar xf
will extract all the archived files as one would expect. One approach is to use a shellfor
loop:Another method is to use
xargs
:Alternatively, you can use one of a number of alternative
tar
file readers. Finally, the truly dedicated programmer could easily write antar
replacement that works exactly as desired. The format is straightforward and many programming languages have libraries available to readtar
files. If you are a Perl programmer, for instance, take a look at theArchive::Tar
module.A warning
Blindly untarring a bunch of files can cause unexpected problems. The most obvious is that a particular file name may be included in more than one
tar
file. Sincetar
overwrites files by default, the exact version of the file you end up with will depend on the order the archives are processed. More troubling, you may end up with a corrupted copy of the file if you try this "clever" optimization:If both
a.tar
andb.tar
contain the same file and try to extract it at the same time, the results are unpredictable.A related issue, especially when taking archives from an untrusted source, is the possibility of a tarbomb.
One partial solution would be to automatically create a new directory to extract into:
This won't help if a file is specified in the archive with an absolute path (which is normally a sign of malicious intent). Adding that sort of check is left as an exercise for the reader.