Skip to content

bash - enhanced compress and decompress

Again two functions you can add alias for. The first function provides a wrapper for the task to create tar.gz file. If you provide only one argument (like a filename or a directory), the function creates a $name.tar.gz file from the first argument. If you provide more the one argument, the function uses the first argument as name of the *.tar.gz file and all others as files/directories to compress.

####
# compress given directories into tar.gz
#
# @author stev leibelt
# @since 2013-02-02
####
function net_bazzline_compress ()
{
  if [[ $# -lt 1 ]]; then
    echo 'No valid arguments supplied.'

    exit 1
  fi

  FILENAME_TAR="$1".tar.gz

  if [[ $# -gt 1 ]]; then
    shift
  fi

  tar -zcf "$FILENAME_TAR" "$@"
}
The second function provides a wrapper to get the files of a *.tar.gz file. If you provide one argument, the functions is using this as the *.tar.gz filename. If you provide two arguments, the second argument is used as output directory.
####
# compress given directories into tar.gz
#
# @author stev leibelt
# @since 2013-02-02
####
function net_bazzline_decompress ()
{
  if [[ $# -lt 1 ]]; then
    echo 'No valid arguments supplied.'
    echo 'Try net_bazzline_decompress $nameOfCompressedFile [$pathToDecompress]'

    exit 1
  fi

  if [[ $# -eq 1 ]]; then
    tar -zxf "$1"
  else
    tar -zxf "$1" -C "$2"
  fi
}
As general, they are also in my shell function file available on github.com.