简体   繁体   English

将文件夹中的所有.tgz 文件解压缩到特定文件夹

[英]unzipping all .tgz files in folder to a specific folder

I am new to linux and I am trying the following:我是 linux 的新手,我正在尝试以下操作:

I have directory data.我有目录数据。 In this directory there are x files that end with *.tgz.在这个目录中有 x 个以 *.tgz 结尾的文件。 I want to unpack each, and store the output in a new folder.我想解压每个,并将 output 存储在一个新文件夹中。 So as an example.举个例子。 I start with:我开始:

ls data

folder_a.tgz    folder_b.tgz    folder_c.tgz

I want want to end up with:我想结束:

ls /data/test/
folder_a    folder_b    folder_c

I have tried the following:我尝试了以下方法:

ls *.tgz | xargs -n1 tar zxvf -C /data/test/

Which gives the error:这给出了错误:

-C cannot open: no such file or directory

I do not understand why this does not work, since these both work as expected:我不明白为什么这不起作用,因为它们都按预期工作:

tar zxvf folder_a.tgz -C /data/test/
ls *.tgz | xargs -n1 tar zxvf

But combining these does not work.但是将这些结合起来是行不通的。 Can anyone help me to achieve the goal, and maybe explain why my solution does not work?谁能帮助我实现目标,也许可以解释为什么我的解决方案不起作用?

Your problem is that xargs puts the argument at the end, but tar requires the filename after the f flag.您的问题是 xargs 将参数放在最后,但 tar 需要f标志之后的文件名。 The following should work:以下应该有效:

ls *.tgz | xargs -n1 tar zxv -C /data/test/ -f

But maybe just using a loop will be the better solution here:但也许在这里使用循环将是更好的解决方案:

for ARCHIVE in *.tgz; do tar xvzf "$ARCHIVE" -C /data/test; done

or alternatively:或者:

ls *.tgz | while read ARCHIVE; do tar xvzf "$ARCHIVE" -C /data/test; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM