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

Spell checking a website

This was by request. A user had a website and wanted to run spell check on it. This is simple, just run:

aspell -H -c filename.php

The -c asks aspell to check, and the -H prepares it for HTML (it skips class names and php tags etc).

aspell only takes one filename at a time, but you can run through each file in a loop using:

for i in *.php; do aspell -c $i; done
bash_scripts.txt · Last modified: 2022/04/07 14:54 by admin