easy decompress

I've done this way too many times:

  • make a temp dir in /tmp
  • copy a compressed file to that temp dir
  • decompress the file
  • cd into the temp dir
  • run ls to see what was in it

Here's a handy function I wrote to do that for me, for a variety of file types.

So now, I just type decompress filename and it does it for me.

To "install" this function, put it in a file, and then "source" that file. For example, if it's in bash_functions.sh, you'd do this:

source bash_functions.sh

# or (equivalent)

. bash_functions.sh

You can source the file manually or in your .bashrc file.

code

function decompress {
    #Given a filename, make a temp dir in the
    #temp folder, copy the file there, and
    #decompress it. Works with .zip and
    #tarballs compressed with gzip or bzip2.

    local dirname
    local filename

    if [ "$1" = "" ]; then
        echo "no file passed"
        return 1
    fi

    dirname="$(tempfile)"
    rm $dirname #we're going to make a directory with that name

    mkdir $dirname

    cp $1 $dirname
    cd $dirname

    for filename in *.zip; do 
        if [ -f "$filename" ]; then
            unzip "$filename" && rm "$filename"
        fi
    done

    for filename in *tar.bz2; do 
        if [ -f "$filename" ]; then
            tar -xjf "$filename" && rm "$filename"
        fi
    done

    for filename in *tar.bz2; do 
        if [ -f "$filename" ]; then
            bunzip2 "$filename" && rm "$filename"
        fi
    done

    for filename in *tar.gz; do 
        if [ -f "$filename" ]; then
            tar -xzf "$filename" && rm "$filename"
        fi
    done

    for filename in *.tgz; do 
        if [ -f "$filename" ]; then
            tar -xzf "$filename" && rm "$filename"
        fi
    done

    for filename in *tar.xz; do 
        if [ -f "$filename" ]; then
            tar -xJf "$filename" && rm "$filename"
        fi
    done

    ls

}

Comments !

blogroll

social