再帰的UNTAR/圧縮解除

再帰的UNTAR/圧縮解除

処理するzipファイルまたはtarファイルを取得します。

zip /tar ファイルには複数のディレクトリとサブディレクトリがあり、tar ファイル/zip ファイルを含めることができます。

親のTar / Zipのファイルを適切なディレクトリの場所に抽出し、tar / zipファイルを削除する必要があります。

以下は私が達成できるものですが、問題は、親tar / zipだけを解凍/圧縮解除し、zip / tarの内容は解凍しないことです。

found=1

while [ $found -eq 1 ]
do
    found=0
    for compressfile in *.tar *.zip
    do     
        found=1
        echo "EXTRACT THIS:"$compressfile
        tar xvf "$compressfile" && rm -rf "$compressfile"
        unzip "$compressfile" && rm -rf "$compressfile"
        exc=$?

        if [ $exc -ne 0 ]
        then
            exit $exc
        fi
    done
done

注:Tarファイルには、TarファイルとZipファイルの両方を含めることができます。同様に、Zip には Zip または Tar ファイルを含めることができます。

ベストアンサー1

これはテストされていませんが、あなたが探しているものと似ている可能性があります。

#!/bin/bash

doExtract() {
    local compressfile="${1}"
    local rc=0

    pushd "$(dirname "${compressfile}")" &> /dev/null
    if [[ "${compressfile}" == *.tar ]]; then
        echo "Extracting TAR: ${compressfile}"
        tar -xvf "$(basename ${compressfile})"
        rc=$?
    elif [[ "${compressfile}" == *.zip ]]; then
        echo "Extracting ZIP: ${compressfile}"
        unzip "$(basename "${compressfile}")"
        rc=$?
    fi
    popd &> /dev/null

    if [[ ${rc} -eq 0 ]]; then
        # You can remove the -i when you're sure this is doing what you want
        rm -i "${compressfile}"
    fi

    return ${rc}
}

found=1

while [[ ${found} -eq 1 ]]; do
    found=0

    for compressfile in $(find . -type f -name '*.tar' -o -name '*.zip'); do
        found=1
        doExtract "${compressfile}"
        rc=$?
        if [[ $rc -ne 0 ]]; then
             exit ${rc}
        fi
    done
done

.tar編集:このスクリプトは、またはで終わるファイルを繰り返し検索します.zip。 optionsなしで-C/をtar使用してファイルが含まれているディレクトリに変更し、そのディレクトリにファイルを抽出してから以前のディレクトリに戻ります。pushdpopd

おすすめ記事