Commonly Used Bash Codes#

  • Check File Size
# Show the file size in Gb
stat --printf="%s GB\n" *.fna | awk '{print $1/1000000}'
ls -lh | awk '{print $5, $9}'
du -a -h --max-depth=1 | sort -hr
du -h -d 1 | sort -n #for macOS
  • Unzip .tar.gz tar xvzf file.tar.gz

  • Print lines after match

# With sed
sed -n '/Read2 aftering/{N;N;N;N;H;x;p;d}' $(find $DIR -type d -name WEM155)/fastp/log

output:
Read2 aftering filtering:
total reads: 12403525
total bases: 1764893182
Q20 bases: 1726073328(97.8004%)
Q30 bases: 1671656623(94.7172%)

# With Grep
grep -A5 "pattern" <file>
  • Print lines for match in a specific column
#awk 'BEGIN { FS = "\t" }{print $4}' <file> #For printing the 4th column
awk -F "\t" '$4 == "S"' <file> #Print line if column matches S

Argparse#

usage()
{
    echo "usage: cent.sh [[[-d cent_directory ] [-n index_path] [-i input] [-o output] [-p prefix]] | [-h]]"
}

if [[ ! $# -gt 0 ]]; then
usage
fi

while [ "$1" != "" ]; do
    case $1 in
        -d | --dir )           shift
                                CENT_BIN_DIR=$1
                                ;;
        -n | --index )         shift
                                CENTRIFUGE_INDEXES=$1
                                ;;
        -i | --input )         shift
                                INPUT=$1
                                ;;
        -o | --output )        shift
                                OUTPUT=$1
                                ;;
        -p | --prefix )        shift
                                PREFIX=$1
                                ;;
        -h | --help )           usage
                                exit
                                ;;
        * )                     usage
                                exit 1
    esac
    shift
done

USERADD#

link * -u user id * -g user group * -m creates a user with specified home directory * -d specifies where the home directory is * -r create a system accoun * -s login shell *

groupadd -r macadish
useradd -r -m -s /bin/bash -g macadish jon
cat /etc/passwd | grep jon

a#

Cheatsheet

# Add session
# Attach session
#