User Tools

Site Tools


bash_scripts

Bash Scripts

Extracting compressed files

Taken from a reddit post on r/commandline:

extract.sh
#!/bin/sh
 
export XZ_OPT=-T4
 
if [ -z "$1" ]; then
    echo "Usage: extract filename"
    echo "Extract a given file based on the extension."
    exit 1
elif [ ! -f "$1" ]; then
    echo "Error: '$1' is not a valid file for extraction"
    exit 1
fi
 
case "$1" in
    *.tbz2 | *.tar.bz2) tar -xvjf  "$1"     ;;
    *.txz | *.tar.xz)   tar -xvJf  "$1"     ;;
    *.tgz | *.tar.gz)   tar -xvzf  "$1"     ;;
    *.tar | *.cbt)      tar -xvf   "$1"     ;;
    *.tar.zst)          tar -xvf   "$1"     ;;
    *.zip | *.cbz)      unzip      "$1"     ;;
    *.rar | *.cbr)      unrar x    "$1"     ;;
    *.arj)              unarj x    "$1"     ;;
    *.ace)              unace x    "$1"     ;;
    *.bz2)              bunzip2    "$1"     ;;
    *.xz)               unxz       "$1"     ;;
    *.gz)               gunzip     "$1"     ;;
    *.7z)               7z x       "$1"     ;;
    *.Z)                uncompress "$1"     ;;
    *.gpg)        gpg -d "$1" | tar -xvzf - ;;
    *) echo "Error: failed to extract '$1'" ;;
esac
pk.sh
#!/bin/sh
case $1 in
    txz)    tar cJvf "$2.tar.xz" "$2"               ;;
    tbz)    tar cjvf "$2.tar.bz2" "$2"              ;;
    tgz)    tar czvf "$2.tar.gz" "$2"               ;;
    tar)    tar cpvf "$2.tar" "$2"                  ;;
    bz2)    bzip2 "$2"                              ;;
    gz)     gzip -c -9 -n "$2" > "$2.gz"            ;;
    zip)    zip -r "$2.zip" "$2"                    ;;
    7z)     7z a "$2.7z" "$2"                       ;;
    *)      echo "'$1' cannot be packed via pk()"   ;;
esac
bash_scripts.txt · Last modified: 2021/08/17 01:54 by admin