🐚 Bash Profile Cheat Sheet

Complete reference for your ~/.bashrc configuration

Quick Navigation

🎨 Ultimate Prompt

Prompt Features

Your custom prompt displays:

  • Error Code - Shows exit code if last command failed
  • Time - Current time in HH:MM format
  • CPU Usage - Real-time CPU percentage
  • User@Host - Green for normal user, Red for root
  • Working Directory - Full path in yellow
  • Git Branch - Current branch if in a git repo

Color Legend

ERROR - Error exit code
12:34 - Time
CPU: 45% - CPU Usage
user@host - Normal user
root@host - Root user
/path/to/dir - Working directory
(main) - Git branch
$ - Prompt symbol

Example Prompt

(ERROR 1)- (14:32)- (CPU: 23.4%)- john@myserver:/home/john/projects (feature-branch)
$ _

Prompt Function Code

function __setprompt {
    local LAST_COMMAND=$?
    local RED="\033[0;31m"
    local GREEN="\033[0;32m"
    local YELLOW="\033[1;33m"
    local BLUE="\033[0;34m"
    local MAGENTA="\033[0;35m"
    local CYAN="\033[0;36m"
    local DARKGRAY="\033[1;30m"
    local NOCOLOR="\033[0m"
    
    PS1=""
    
    # Error Exit Code
    if [[ $LAST_COMMAND != 0 ]]; then
        PS1+="\[${DARKGRAY}\](\[${RED}\]ERROR ${LAST_COMMAND}\[${DARKGRAY}\])-"
    fi
    
    # Date & Time
    PS1+="\[${DARKGRAY}\](\[${CYAN}\]\$(date +%H:%M)\[${DARKGRAY}\])-"
    
    # CPU & Net Stats
    PS1+="(\[${MAGENTA}\]CPU: $(cpu_usage)%\[${DARKGRAY}\])-"
    
    # User & Host
    if [[ $EUID -ne 0 ]]; then
        PS1+="\[${GREEN}\]\u@\h"
    else
        PS1+="\[${RED}\]\u@\h"
    fi
    
    # Working Directory
    PS1+="\[${DARKGRAY}\]:\[${YELLOW}\]\w\[${NOCOLOR}\]"
    
    # Git Branch
    if command -v git &> /dev/null; then
        local branch=$(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/')
        if [ ! -z "$branch" ]; then
            PS1+="\[${BLUE}\]${branch}\[${NOCOLOR}\]"
        fi
    fi
    
    PS1+="\n"
    if [[ $EUID -ne 0 ]]; then
        PS1+="\[${GREEN}\]$\[${NOCOLOR}\] "
    else
        PS1+="\[${RED}\]#\[${NOCOLOR}\] "
    fi
}

PROMPT_COMMAND='__setprompt'

📋 Listing Aliases

Command Expands To Description
ls ls -aFh --color=always All files, formatted, human-readable, colored
ll ls -alF Long format, all files including hidden
la ls -Alh Long format, human-readable, excludes . and ..
lx ls -lXBh Sort by extension
lk ls -lSrh Sort by size (smallest first)
lc ls -lcrh Sort by change time
lu ls -lurh Sort by access time
lr ls -lRh Recursive listing
lt ls -ltrh Sort by date (newest last)
lm ls -alh | more Pipe through 'more'
lw ls -xAh Wide listing format
ldir ls -l | egrep '^d' Directories only
lf ls -l | egrep -v '^d' Files only (excludes directories)

🛠️ File Operations

Command Expands To Description
cp cp -i Interactive mode (prompts before overwrite)
mv mv -i Interactive mode (prompts before overwrite)
rm rm -iv Interactive and verbose delete
mkdir mkdir -p Create directories with parents
rmd /bin/rm --recursive --force --verbose Forceful recursive verbose delete

💻 System & Monitoring

Command Expands To Description
ps ps auxf Full process tree
ping ping -c 10 Ping with 10 packets
df df -hT Disk space (human-readable, shows type)
free free -m Memory usage in megabytes
topcpu /bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10 Top 10 CPU-consuming processes
openports netstat -nape --inet List open internet ports and PIDs
ipview Complex pipeline Count and sort unique IPs connecting to port 80

📦 Package Management

Command Expands To Description
update sudo apt update && sudo apt upgrade Update and upgrade system packages
apt-get sudo apt-get Run apt-get with sudo
freshclam sudo freshclam Update ClamAV virus definitions

✏️ Editor Aliases

Command Expands To Description
vi vim Use vim instead of vi
vim vim Open vim
svi sudo vim Edit files with sudo privileges
edit edit Default editor (Neovim)
sedit sudo edit Edit with sudo using default editor
ebrc edit ~/.bashrc Quick edit bashrc file

🔧 Utilities

Command Expands To Description
cls clear Clear terminal screen
h history | grep Search bash history
p ps aux | grep Search running processes
f find . | grep Search for filenames in current dir
da date "+%Y-%m-%d %A %T %Z" Formatted date output
sha1 openssl sha1 Generate SHA1 hash
multitail multitail --no-repeat -c Colorized multitail without repeat
less less -R Less with raw control characters
more less Use less instead of more

Archive Management

Command Expands To Description
mktar tar -cvf Create tar archive
mkbz2 tar -cvjf Create bzip2 compressed tar
mkgz tar -cvzf Create gzip compressed tar
untar tar -xvf Extract tar archive
unbz2 tar -xvjf Extract bzip2 compressed tar
ungz tar -xvzf Extract gzip compressed tar

💾 Disk Space

Command Description
diskspace du -S | sort -n -r | more - Show disk usage sorted by size
folders du -h --max-depth=1 - Show folder sizes in current directory
folderssort find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn - Sorted folder sizes
tree tree -CAhF --dirsfirst - Colorized tree view, dirs first
treed tree -CAFd - Tree view (directories only)

📜 Logs & System Control

Command Description
logs Find and tail all text-based log files in /var/log
rebootsafe sudo shutdown -r now - Safe reboot
rebootforce sudo shutdown -r -n now - Force reboot (no sync)
alert Desktop notification for long-running commands

⚡ Custom Functions

extract FUNCTION

Smart extraction function that auto-detects archive type

Usage: extract file.zip or extract file.tar.gz

Supports: .tar.bz2, .tar.gz, .bz2, .rar, .gz, .tar, .tbz2, .tgz, .zip, .Z, .7z

ftext FUNCTION

Search text in files recursively

Usage: ftext "search term"

Features: Case-insensitive, colored output, line numbers, less pager

cpp FUNCTION

Copy files with progress bar

Usage: cpp source_file destination

cpg FUNCTION

Copy and go to directory

Usage: cpg file.txt /destination/path

mvg FUNCTION

Move and go to directory

Usage: mvg file.txt /destination/path

mkdirg FUNCTION

Create directory and cd into it

Usage: mkdirg new_directory

up FUNCTION

Go up N directories

Usage: up 3 (goes up 3 levels)

rot13 FUNCTION

ROT13 encode/decode text

Usage: echo "hello" | rot13 or rot13 "hello"

cpu_usage FUNCTION

Get current CPU usage percentage

Usage: cpu_usage

distribution FUNCTION

Detect Linux distribution

Usage: distribution

Returns: ubuntu, debian, redhat, suse, gentoo, etc.

ver FUNCTION

Show OS version

Usage: ver

netinfo FUNCTION

Display network information

Usage: netinfo

Shows: IP addresses, broadcast, netmask, MAC address

whatsmyip FUNCTION

Show internal and external IP addresses

Usage: whatsmyip

apachelog FUNCTION

View Apache logs with multitail

Usage: apachelog

Auto-detects: /var/log/httpd or /var/log/apache2

apacheconfig FUNCTION

Edit Apache configuration with sudo

Usage: apacheconfig

phpconfig FUNCTION

Edit PHP configuration with sudo

Usage: phpconfig

mysqlconfig FUNCTION

Edit MySQL configuration with sudo

Usage: mysqlconfig

🌍 Environment Variables

Variable Value Description
EDITOR edit Default text editor (Neovim)
VISUAL edit Visual editor
PAGER less Default pager program
LANG en_US.UTF-8 System language
LC_ALL en_US.UTF-8 Locale settings
HISTFILESIZE 10000 Max lines in history file
HISTSIZE 5000 Max lines in memory history
HISTCONTROL erasedups:ignoredups:ignorespace History filtering rules
CLICOLOR 1 Enable color output
LS_COLORS Complex color scheme Custom colors for ls command

History Configuration

export HISTFILESIZE=10000
export HISTSIZE=5000
export HISTCONTROL=erasedups:ignoredups:ignorespace
shopt -s histappend
PROMPT_COMMAND='history -a'

PATH Additions

  • $HOME/bin - Personal binaries
  • /home/linuxbrew/.linuxbrew - Linuxbrew packages
  • /home/linuxbrew/.linuxbrew/opt/imagemagick-full/bin - ImageMagick
  • $HOME/.cargo/env - Rust/Cargo binaries

🎨 Colors & Formatting

Terminal Settings

  • Bell Style: Visible bell (no sound)
  • Completion: Case-insensitive
  • Show All: Auto-show completions when ambiguous
  • Ctrl-S: Enabled for history navigation

Color Aliases

Command Option
grep --color=auto
fgrep --color=auto
egrep --color=auto
diff --color=auto

Man Page Colors (LESS_TERMCAP)

  • mb (blink): Red bold
  • md (bold): Red bold
  • me (end): Reset
  • se (end standout): Reset
  • so (standout): Yellow on blue
  • ue (end underline): Reset
  • us (underline): Green bold

⚙️ Shell Options

Option Description
shopt -s histappend Append to history file instead of overwriting
shopt -s checkwinsize Check window size after each command
stty -ixon Disable flow control (free Ctrl-S)
bind "set completion-ignore-case on" Case-insensitive tab completion
bind "set show-all-if-ambiguous On" Show completions immediately

📂 Sourced Files

Your bashrc sources the following files if they exist:

  • /etc/bashrc - Global bash definitions
  • /usr/share/bash-completion/bash_completion or /etc/bash_completion - Programmable completion
  • /home/linuxbrew/.linuxbrew/bin/brew shellenv bash - Linuxbrew environment
  • ~/.bash_aliases - Personal aliases