Linux 如何使用 shell-script 解压所有 .tar.gz?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4263156/
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
How to untar all .tar.gz with shell-script?
提问by fifty arashi
I tried this:
我试过这个:
DIR=/path/tar/*.gz
if [ "$(ls -A $DIR 2> /dev/null)" == "" ]; then
echo "not gz"
else
tar -zxvf /path/tar/*.gz -C /path/tar
fi
If the folder has one tar, it works. If the folder has many tar, I get an error.
如果文件夹有一个 tar,它就可以工作。如果文件夹中有很多 tar,我会收到错误消息。
How can I do this?
我怎样才能做到这一点?
I have an idea to run a loop to untar, but I don't know how to solve this problem
我有一个运行循环来解压的想法,但我不知道如何解决这个问题
采纳答案by Ignacio Vazquez-Abrams
for f in *.tar.gz
do
tar zxvf "$f" -C /path/tar
done
回答by Matt Joiner
for a in /path/tar/*.gz
do
tar -xzvf "$a" -C /path/tar
done
Notes
笔记
- This presumes that files ending in
.gz
are gzipped tar files. Usually.tgz
or.tar.gz
is used to signifythis, howevertar
will fail if something is not right. - You may find it easier to
cd /path/tar
first, then you can drop the-C /path/tar
from the untar command, and the/path/tar/
in the loop.
- 这假定以 结尾的文件
.gz
是 gzipped tar 文件。通常.tgz
or.tar.gz
用于表示这一点,但是tar
如果出现问题,则会失败。 - 您可能会发现首先更容易
cd /path/tar
,然后您可以-C /path/tar
从 untar 命令中删除 ,然后/path/tar/
在循环中删除。
回答by Joshua Martell
I find the find
exec syntax very useful:
我发现find
exec 语法非常有用:
find . -name '*.tar.gz' -exec tar -xzvf {} \;
find . -name '*.tar.gz' -exec tar -xzvf {} \;
{}
gets replaced with each file found and the line is executed.
{}
被找到的每个文件替换并执行该行。
回答by Shamsa
The accepted answer worked for me with a slight modification
接受的答案对我有用,稍作修改
for f in *.tar.gz
do
tar zxvf "$f" -C \name_of_destination_folder_inside_current_path
done
I had to change the forward slash to a backslash and then it worked for me.
我不得不将正斜杠更改为反斜杠,然后它对我有用。