Skip to content

Instantly share code, notes, and snippets.

@jacius
Created October 5, 2009 20:34
Show Gist options
  • Save jacius/202439 to your computer and use it in GitHub Desktop.
Save jacius/202439 to your computer and use it in GitHub Desktop.

Revisions

  1. jacius revised this gist May 14, 2011. No changes.
  2. jacius revised this gist May 14, 2011. 1 changed file with 37 additions and 15 deletions.
    52 changes: 37 additions & 15 deletions untar
    Original file line number Diff line number Diff line change
    @@ -16,37 +16,39 @@
    # 2009-10-05: Initial version. (JC)
    # 2009-10-06: Added -v/--verbose flag. (JC)
    # Improved file type detection, w/ support for symlinks. (JC)
    # 2011-05-13: Added -l/-t/--list and -x/--extract flags. (JC)
    # Added detection for .tar.xz files.
    #

    script=$(basename $0)


    opts=`getopt -n "$script" -o "v" -l "verbose" -- "$@"`
    opts=`getopt -n "$script" -o "vltx" -l "verbose" -l "list" -l "extract" -- "$@"`
    if [ ! $? -eq 0 ]; then exit 1; fi # getopt failed
    # Overwrite the script's positional parameters with getopt's output:
    set -- $opts


    verbose=false
    action=extract

    while [ $# -gt 0 ]; do
    case "$1" in
    -v) verbose=true;;
    --verbose) verbose=true;;
    --) shift; break;;
    *) break;
    -v | --verbose)
    verbose=true;;
    -l | -t | --list)
    action=list;;
    -x | --extract)
    action=extract;;
    --)
    shift; break;;
    *)
    break;
    esac
    shift
    done


    flags=

    if [ true = $verbose ]; then
    flags="-v $flags"
    fi


    function file_type {
    if [ ! -e $1 ]; then
    echo "missing"; return 1;
    @@ -57,6 +59,8 @@ function file_type {
    echo "tar.gz";;
    application/x-tar*application/x-bzip2*)
    echo "tar.bz2";;
    application/x-tar*application/x-xz*)
    echo "tar.xz";;
    application/x-tar*)
    echo "tar";;
    *)
    @@ -68,20 +72,38 @@ function file_type {
    function handle_file {
    target=`eval echo $1` # strip 'quotes' that getopt made

    tarflags=

    if [ true = $verbose ]; then
    tarflags="-v $flags"
    fi

    case $action in
    extract)
    tarflags="$tarflags -x";;
    list)
    tarflags="$tarflags -t";;
    esac

    case $(file_type $target) in
    *) ;;
    missing)
    echo "$script: $target: No such file"
    return 1;;
    tar.gz)
    tar $flags -xzf $target;;
    tarflags="$tarflags -z";;
    tar.bz2)
    tar $flags -xjf $target;;
    tarflags="$tarflags -j";;
    tar.xz)
    tarflags="$tarflags -J";;
    tar)
    tar $flags -xf $target;;
    ;;
    *)
    echo "$script: $target: That's not a tarball, silly"
    return 1;;
    esac

    tar $tarflags -f $target;
    }


  3. jacius revised this gist Oct 9, 2009. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion untar
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@
    # For times when you don't care whether it was compressed
    # with bzip2 or gzip, you just want to untar it already!
    #
    # Usage: untar [-v|--verbose] file
    #
    # Author: John Croisant
    #
    # License: Do whatever you want.
    @@ -31,7 +33,7 @@ while [ $# -gt 0 ]; do
    case "$1" in
    -v) verbose=true;;
    --verbose) verbose=true;;
    --) shift; break;;
    --) shift; break;;
    *) break;
    esac
    shift
  4. jacius revised this gist Oct 9, 2009. 1 changed file with 73 additions and 23 deletions.
    96 changes: 73 additions & 23 deletions untar
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,86 @@
    #!/bin/bash

    #
    # A simple shell script to untar a tarball.
    # A simple command to untar any tarball.
    #
    # For times when you don't care whether it was compressed
    # with bzip2 or gzip, you just want to untar it already!
    #
    # Author: John Croisant
    # Created: 2009-10-05
    #
    # License: Do whatever you want.
    #
    # History:
    # 2009-10-05: Initial version. (JC)
    # 2009-10-06: Added -v/--verbose flag. (JC)
    # Improved file type detection, w/ support for symlinks. (JC)
    #

    script=$(basename $0)


    opts=`getopt -n "$script" -o "v" -l "verbose" -- "$@"`
    if [ ! $? -eq 0 ]; then exit 1; fi # getopt failed
    # Overwrite the script's positional parameters with getopt's output:
    set -- $opts


    verbose=false

    while [ $# -gt 0 ]; do
    case "$1" in
    -v) verbose=true;;
    --verbose) verbose=true;;
    --) shift; break;;
    *) break;
    esac
    shift
    done

    script=`basename $0`

    if [[ ! -e $1 ]]; then
    echo "$script: $1: No such file"
    exit 1
    flags=

    if [ true = $verbose ]; then
    flags="-v $flags"
    fi

    case `file $1` in
    *gzip\ compressed*) # .tar.gz / .tgz (gzip)
    tar -xzf $1
    ;;
    *bzip2\ compressed*) # .tar.bz2 (bzip2)
    tar -xjf $1
    ;;
    *tar\ archive*) # .tar (no compression)
    tar -xf $1
    ;;
    *)
    echo "$script: $1: That's not a tarball, silly"
    exit 1
    ;;
    esac

    exit

    function file_type {
    if [ ! -e $1 ]; then
    echo "missing"; return 1;
    fi

    case $(file --brief --dereference --mime --uncompress $1) in
    application/x-tar*application/x-gzip*)
    echo "tar.gz";;
    application/x-tar*application/x-bzip2*)
    echo "tar.bz2";;
    application/x-tar*)
    echo "tar";;
    *)
    echo "unknown"; return 1;;
    esac
    }


    function handle_file {
    target=`eval echo $1` # strip 'quotes' that getopt made

    case $(file_type $target) in
    missing)
    echo "$script: $target: No such file"
    return 1;;
    tar.gz)
    tar $flags -xzf $target;;
    tar.bz2)
    tar $flags -xjf $target;;
    tar)
    tar $flags -xf $target;;
    *)
    echo "$script: $target: That's not a tarball, silly"
    return 1;;
    esac
    }


    handle_file $1
  5. jacius created this gist Oct 5, 2009.
    36 changes: 36 additions & 0 deletions untar
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    #
    # A simple shell script to untar a tarball.
    #
    # For times when you don't care whether it was compressed
    # with bzip2 or gzip, you just want to untar it already!
    #
    # Author: John Croisant
    # Created: 2009-10-05
    #

    script=`basename $0`

    if [[ ! -e $1 ]]; then
    echo "$script: $1: No such file"
    exit 1
    fi

    case `file $1` in
    *gzip\ compressed*) # .tar.gz / .tgz (gzip)
    tar -xzf $1
    ;;
    *bzip2\ compressed*) # .tar.bz2 (bzip2)
    tar -xjf $1
    ;;
    *tar\ archive*) # .tar (no compression)
    tar -xf $1
    ;;
    *)
    echo "$script: $1: That's not a tarball, silly"
    exit 1
    ;;
    esac

    exit