maketar() { if [ -e "$1" ]; then tar cvzf "$1.tgz" "$@" else if [ -e "$2" ]; then x="" for var in "$@" do if [ -e "$var" ]; then x+=$var x+=" " fi done tar cvzf "$1.tgz" $x else if [ -n "$1" ]; then tar cvzf "$1.tgz" * else tar cvzf all.tgz * fi fi fi errstatus=$? if [ -z "$1" ]; then NAME=all else NAME=$1 fi if [ $errstatus -eq 0 ]; then echo "Successfully, made tar archive called $NAME.tgz" return 0 else echo "Failed, to make tar archive called $NAME.tgz" return $errstatus fi } bk() { local outbase="" local verbose=1 # Parse options while [[ $# -gt 0 ]]; do case "$1" in -o|--output) outbase="$2"; shift 2 ;; -q|--quiet) verbose=0; shift ;; -h|--help|help) echo "Usage: bk -o NAME file/dir [file/dir ...]" echo "Creates NAME.YYYYMMDD-HHMMSS.tgz from the given paths." return 0 ;; --) shift; break ;; -*) echo "Unknown option: $1" >&2 return 1 ;; *) break ;; esac done if [[ -z "$outbase" || $# -lt 1 ]]; then echo "Usage: bk -o NAME file/dir [file/dir ...]" >&2 return 1 fi local stamp stamp="$(date +%Y%m%d-%H%M%S)" local myoutfile="${outbase%.tgz}.$stamp.tgz" if [[ -e "$myoutfile" ]]; then echo "Refusing to overwrite existing file: $myoutfile" >&2 return 1 fi # Exclude the archive itself in case you’re archiving CWD local exclude_arg=(--exclude="./$(basename "$myoutfile")") (( verbose )) && echo "Making tar file $outfile" tar -czf "$myoutfile" "${exclude_arg[@]}" "$@" } function extract { if [ -z "$1" ]; then echo "Usage: extract ." return 0 fi for n in "$@"; do if [ ! -f "$n" ]; then echo "'$n' - File doesn't exist" return 1 fi done # Initialize an array exit_statuses=() for n in "$@"; do NAME=${n%.*} echo -e "\n Extacting from: $NAME ..." case "${n%,}" in *.deb) sudo nice -n 17 dpkg -i $n ;; *.rpm) sudo nice -n 17 rpm -ivh $n ;; *.tar.bz2) nice -n 17 tar xvjf "$n" ;; *.tar.gz) nice -n 17 tar xvzf "$n" ;; *.tar.xz) nice -n 17 tar xvJf "$n" ;; *.lzma) nice -n 17 unlzma "$n" ;; *.bz2) nice -n 17 bunzip2 "$n" ;; *.rar) nice -n 17 unrar x -ad "$n" ;; *.gz) nice -n 17 gunzip "$n" ;; *.tar) nice -n 17 tar xvf "$n" ;; *.tbz2) nice -n 17 tar xvjf "$n" ;; *.tgz) nice -n 17 tar xvzf "$n" ;; *.zip) nice -n 17 unzip "$n" ;; *.Z) nice -n 17 uncompress "$n" ;; *.7z) nice -n 17 7z x "$n" ;; *.xz) nice -n 17 unxz "$n" ;; *.exe) nice -n 17 cabextract "$n" ;; *) echo "Extract: $n - Unknown archive method!"; return 1 ;; esac exit_statuses+=($?) done # Check for error status codes for status in "${exit_statuses[@]}"; do if (( status > 0 )); then echo "Unable to Extract something...!" return $status fi done echo "Success! Extracted all files..." return 0 }