即时解压缩大量文件

发布于 2024-09-17 20:26:52 字数 148 浏览 11 评论 0原文

我有一个脚本,需要在扩展名为 **.tar.gz* 的大量文件上运行。

我希望能够在运行命令时解压缩它们,然后处理未压缩的文件夹,而不是解压缩它们然后运行脚本,所有这些都只需一个命令。

我认为管道是一个很好的解决方案,但我以前没有使用过。我该怎么做?

I have a script that I need to run on a large number of files with the extension **.tar.gz*.

Instead of uncompressing them and then running the script, I want to be able to uncompress them as I run the command and then work on the uncompressed folder, all with a single command.

I think a pipe is a good solution for this but i haven't used it before. How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

海夕 2024-09-24 20:26:52

-v 命令 tar 在提取每个文件时打印文件名:

tar -xzvf file.tar.gz | xargs -I {} -d\\n myscript "{}"

这样脚本将包含处理单个文件的命令,并作为参数传递(感谢 < code>xargs) 到您的脚本(脚本上下文中的 $1)。

编辑: -I {} -d\\n 部分将使其适用于文件名中的空格。

The -v orders tar to print filenames as it extracts each file:

tar -xzvf file.tar.gz | xargs -I {} -d\\n myscript "{}"

This way the script will contain commands to deal with a single file, passed as a parameter (thanks to xargs) to your script ($1 in the script context).

Edit: the -I {} -d\\n part will make it work with spaces in filenames.

倾其所爱 2024-09-24 20:26:52

以下三行 bash...

for archive in *.tar.gz; do
    tar zxvf "${archive}" 2>&1 | sed -e 's!x \([^/]*\)/.*!\1!' | sort -u | xargs some_script.sh
done

...将迭代当前目录中的每个 gzipped tarball,解压缩它,获取解压缩内容的最顶层目录并将它们作为参数传递给 somescript.sh.这可能使用比您预期更多的管道,但似乎满足您的要求。

注意:tar xf 每次调用只能获取一个文件。

The following three lines of bash...

for archive in *.tar.gz; do
    tar zxvf "${archive}" 2>&1 | sed -e 's!x \([^/]*\)/.*!\1!' | sort -u | xargs some_script.sh
done

...will iterate over each gzipped tarball in the current directory, decompress it, grab the top-most directories of the decompressed contents and pass those as arguments to somescript.sh. This probably uses more pipes than you were expecting but seems to do what you are asking for.

N.B: tar xf can only take one file per invocation.

遥远的绿洲 2024-09-24 20:26:52

您可以使用 for 循环:

for file in *.tar.gz; do tar -xf "$file"; your commands here; done

或扩展:

for file in *.tar.gz; do
    tar -xf "$file"
    # your commands here
done

You can use a for loop:

for file in *.tar.gz; do tar -xf "$file"; your commands here; done

Or expanded:

for file in *.tar.gz; do
    tar -xf "$file"
    # your commands here
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文