#!/bin/bash
# Some aliases to make it easier to deal with archives
#
if command -v tar &> /dev/null;
then
function gztar()
{
[ -d "$1" ] || { echo "usage: gztar
[tar options]" 1>&2; exit 1; }
DIR="$1"
shift
tar "$@" -czvf "$(basename "$DIR").tar.gz" "$DIR"
}
function bztar()
{
[ -d "$1" ] || { echo "usage: bztar [tar options]" 1>&2; exit 1; }
DIR="$1"
shift
tar "$@" -cjvf "$(basename "$DIR").tar.bz2" "$DIR"
}
function xztar()
{
[ -d "$1" ] || { echo "usage: xztar [tar options]" 1>&2; exit 1; }
DIR="$1"
shift
tar "$@" -cJvf "$(basename "$DIR").tar.xz" "$DIR"
}
function untar()
{
[ -f "$1" ] || return
# the 'a' options detects the compression from the file name
# http://petereisentraut.blogspot.it/2012/05/time-to-retrain-fingers.html
tar -xavf "$1"
}
alias gzuntar=untar
alias bzuntar=untar
alias xzuntar=untar
complete -o default -f -X '!*.?(t)bz2' bzuntar
complete -o default -f -X '!*.@(Z|gz|tgz|Gz|dz)' gzuntar
complete -o dirnames gztar bztar
fi
if command -v unzip &> /dev/null;
then
# Unzip files into a directory names as the zip file basename
function dunzip()
{
local FILENAME="$1"
local DIRNAME
DIRNAME="$(basename "$FILENAME" .zip)"
shift;
[ -d "$DIRNAME" ] && { echo "destination exists!"; return 1; }
mkdir "$DIRNAME" && unzip "$@" -d "$DIRNAME" "$FILENAME"
}
complete -o default -f -X '!*.@(zip|ZIP)' dunzip
fi