如何一次解压多个文件?

发布于 2024-07-14 12:24:45 字数 135 浏览 10 评论 0原文

我的目录中有一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖哒 2024-07-21 12:24:46

如果所有 tar 文件都在同一文件夹中,那么我在 tcsh shell 中执行此操作。 一直有效。

find -iname \*.tar -exec tar -xvf {} \;

这与上面的答案相同,我认为更简洁一些。

If all the tar files are in the same folder, then I do this in tcsh shell. Works all the time.

find -iname \*.tar -exec tar -xvf {} \;

This is same as the above answer, a little bit more concise I think.

我很OK 2024-07-21 12:24:46
find . -maxdepth 1 -name '*.tar' -exec tar -xf '{}' \;
find . -maxdepth 1 -name '*.tar' -exec tar -xf '{}' \;
自在安然 2024-07-21 12:24:45

这里发生了什么?

最初,tar 命令旨在与磁带设备一起使用。 由于一次只在一台设备上执行 tar 才有意义,因此该语法被设计为假定只有一台设备。 假定传递的第一个文件或目录是保存相关存档的设备,以及操作中包含存档内容的任何其他文件或目录。 因此,对于 tar 提取(x 选项),传递的第一个文件将是存档,所有其他文件将是要提取的文件。 因此,如果有两个 *.tar 文件(例如 a.tarb.tar),您的命令将扩展为:

$ tar xf a.tar b.tar

除非 a.tar 包含一个名为 b.tar 的文件,tar 命令无所事事并安静退出。 令人烦恼的是,Solaris 版本的 tar 不会报告返回代码或详细选项 (v) 中的任何问题。 同时,即使关闭了详细选项,GNU tar也会返回2和垃圾邮件STDERR

tar: b.tar: Not found in archive
tar: Exiting with failure status due to previous errors

如何一次解压一堆文件?

重写 tar 来接受多个存档文件作为输入为时已晚,但解决该限制并不难。

对于大多数人来说,多次运行 tar 来获取多个存档是最方便的选择。 仅将一个文件名传递给 tar xf 就会按照预期提取所有存档文件。 一种方法是使用 shell for 循环:

$ for f in *.tar; do tar xf "$f"; done

另一种方法是使用 xargs

$ ls *.tar | xargs -i tar xf {}

或者,您可以使用多个 替代 tar 文件读取器。 最后,真正敬业的程序员可以轻松编写完全按照预期工作的 tar 替代品。 格式很简单,许多编程语言都有可供读取的库tar 文件。 例如,如果您是 Perl 程序员,请查看 Archive::Tar< /code>模块。

警告

盲目解压一堆文件可能会导致意想不到的问题。 最明显的是,一个特定的文件名可能包含在多个 tar 文件中。 由于 tar 默认情况下会覆盖文件,因此最终得到的文件的确切版本将取决于存档的处理顺序。 更麻烦的是,如果您尝试这种“聪明”的优化,您最终可能会得到一个损坏的文件副本:

for f in *.tar; do
  tar xf "$f" &
done
wait

如果 a.tarb.tar 都包含相同的文件并同时尝试提取它,结果是不可预测的。

一个相关的问题是tarbomb的可能性,尤其是从不受信任的来源获取档案时。

一种部分解决方案是自动创建一个新目录以提取到其中:

for f in *.tar; do 
  d=`basename "$f" .tar`
  mkdir "$d"
  (cd "$d" && tar xf "../$f")
done

如果在存档中使用绝对路径指定文件(这通常是恶意意图的迹象),这将无济于事。 添加此类检查留给读者作为练习。

What's going on here?

Originally, the tar command was intended for use with magnetic tape devices. Since it only made sense to execute tar 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 for tar extraction (the x 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 (say a.tar and b.tar) your command would expand to:

$ tar xf a.tar b.tar

Unless a.tar contains a file named b.tar, the tar command has nothing to do and exits quietly. Annoyingly, the Solaris version of tar does not report any problems either in the return code or with the verbose option (v). Meanwhile, GNU tar returns 2 and spams STDERR even with the verbose option off:

tar: b.tar: Not found in archive
tar: Exiting with failure status due to previous errors

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 to tar xf will extract all the archived files as one would expect. One approach is to use a shell for loop:

$ for f in *.tar; do tar xf "$f"; done

Another method is to use xargs:

$ ls *.tar | xargs -i tar xf {}

Alternatively, you can use one of a number of alternative tar file readers. Finally, the truly dedicated programmer could easily write an tar replacement that works exactly as desired. The format is straightforward and many programming languages have libraries available to read tar files. If you are a Perl programmer, for instance, take a look at the Archive::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. Since tar 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:

for f in *.tar; do
  tar xf "$f" &
done
wait

If both a.tar and b.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:

for f in *.tar; do 
  d=`basename "$f" .tar`
  mkdir "$d"
  (cd "$d" && tar xf "../$f")
done

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文