Make more function variables local to avoid polluting the environment
[config/bash.git] / .bash / aliases.d / archives
1 #!/bin/bash
2
3 # Some aliases to make it easier to deal with archives
4 #
5 if command -v tar &> /dev/null;
6 then
7   function gztar()
8   {
9     [ -d "$1" ] || { echo "usage: gztar <dir> [tar options]" 1>&2; exit 1; }
10     local DIR="$1"
11     shift
12     tar "$@" -czvf "$(basename "$DIR").tar.gz" "$DIR"
13   }
14
15   function bztar()
16   {
17     [ -d "$1" ] || { echo "usage: bztar <dir> [tar options]" 1>&2; exit 1; }
18     local DIR="$1"
19     shift
20     tar "$@" -cjvf "$(basename "$DIR").tar.bz2" "$DIR"
21   }
22
23   function xztar()
24   {
25     [ -d "$1" ] || { echo "usage: xztar <dir> [tar options]" 1>&2; exit 1; }
26     local DIR="$1"
27     shift
28     tar "$@" -cJvf "$(basename "$DIR").tar.xz" "$DIR"
29   }
30
31   function untar()
32   {
33     [ -f "$1" ] || return
34     # the 'a' options detects the compression from the file name
35     # http://petereisentraut.blogspot.it/2012/05/time-to-retrain-fingers.html
36     tar -xavf "$1"
37   }
38
39   alias gzuntar=untar
40   alias bzuntar=untar
41   alias xzuntar=untar
42
43   complete -o default -f -X '!*.?(t)bz2' bzuntar
44   complete -o default -f -X '!*.@(Z|gz|tgz|Gz|dz)' gzuntar
45   complete -o dirnames gztar bztar
46 fi
47
48 if command -v unzip &> /dev/null;
49 then
50   # Unzip files into a directory names as the zip file basename
51   function dunzip()
52   {
53     local FILENAME="$1"
54     local DIRNAME
55     DIRNAME="$(basename "$FILENAME" .zip)"
56     shift;
57
58     [ -d "$DIRNAME" ] && { echo "destination exists!"; return 1; }
59     mkdir "$DIRNAME" && unzip "$@" -d "$DIRNAME" "$FILENAME"
60   }
61
62   complete -o default -f -X '!*.@(zip|ZIP)' dunzip
63 fi