2

i get zip files or tar files to work upon.

The zip /tar file may have a several directories and sub-directories which in-turn may contain tar files / zip files

I need to untar / unzip those which are present inside the parent Tar / Zip at their respective directory locations and then delete the tar / zip file.

Below is what i could achieve but the problem is it untars / unzips only the parent tar/zip and not the those which are present inside the 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

Note: the Tar file may contain both Tar and Zip files. Likewise The Zip may contain Zip or Tar files.

0

Please note, this is untested, but this might be close to what you're looking for:

#!/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

Edit: This script recursively looks for files ending in .tar or .zip. Without the availability of the -C option to tar, I use pushd/popd to change to the directory containing the files, extract them in that directory, then return to the previous directory.

  • Changed ind . -type f -name '.tar' '.zip' to ind . -type f -name '.tar' -o -name '.zip' to get over the error. Also tar -C is not supported with my tar. One more issue i see is that it does not seem to be deleting tar files in sub-folders. It deletes only those in the parent tar ball directory. uname -a is: SunOS 5.10 sun4v sparc – Know Your Tech Nov 3 '17 at 21:14
  • I modified the script to address your comments above. Do you get any errors from rm? It could simply be that the tar or unzip is failing -- the file is only removed if those operations are successful. – Andy Dalton Nov 3 '17 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.