Extract file using bash script

ぃ、小莉子 提交于 2019-12-08 14:15:31

问题


I created a script which will extract all *.tar.gz file. This file is decompressed five times .tar.gz file, but the problem is that only the first *.tar.gz file is being extracted.

for file in *.tar.gz; do
        gunzip -c "$file" | tar xf -
done
rm -vf "$file"

What should I do this? Answers are greatly appreciated.


回答1:


If your problem is that the tar.gz file contains another tar.gz file which should be extracted as well, you need a different sort of loop. The wildcard at the top of the for loop is only evaluated when the loop starts, so it doesn't include anything extracted from the tar.gz

You could try something like

while true; do
    for f in *.tar.gz; do
        case $f in '*.tar.gz') exit 0;; esac
        tar zxf "$f"
        rm -v "$f"
    done
done

The case depends on the fact that (by default) when no files match the wildcard, it remains unexpanded. You may have to change your shell's globbing options if they differ from the default.

If you really mean that it is compressed (not decompressed) five times, despite the single .gz extension, perhaps you need instead

for i in 1 2 3 4; do
    gunzip file.tar.gz
    mv file.tar file.tar.gz
done
tar zxf file.tar.gz


来源:https://stackoverflow.com/questions/23507381/extract-file-using-bash-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!