سؤال

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.

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top