Boost Your Productivity with Essential Bash Aliases and Functions
This article presents a curated collection of practical Bash aliases and functions that streamline repetitive command‑line tasks, explains how to use them—including a tip for invoking original commands when aliases shadow them—and provides ready‑to‑copy code snippets for productivity, system information, networking, and fun.
Frequent repetition of the same commands in the terminal can waste valuable time; using Bash aliases and functions lets you assign short, memorable names to commands or groups of commands, with functions handling parameters when needed.
Aliases are simple shortcuts and cannot accept arguments, whereas functions can accept any number of parameters. If an alias masks the original command, prefix the command with a backslash (e.g., \ls) to bypass the alias.
Productivity Aliases
alias ls="ls --color=auto"– enable colored output for ls. alias ll="ls --color -al" – detailed colored directory listing. alias grep='grep --color=auto' – colored matches in grep. mcd() { mkdir -p "$1"; cd "$1"; } – create a directory and immediately enter it. cls() { cd "$1"; ls; } – change to a directory and list its contents. backup() { cp "$1"{,.bak}; } – create a .bak backup of a file. md5check() { md5sum "$1" | grep "$2"; } – compute an MD5 checksum and compare it. alias makescript="fc -rnl | head -1 >" – turn the last executed command into a script file.
alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '
'; echo"– generate a strong random password. alias c="clear" – clear the terminal screen. alias histg="history | grep" – search command history for a keyword. alias ..='cd ..' – go up one directory level. alias ...='cd ../..' – go up two directory levels.
Archive Extraction Function
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}This function handles extraction of many common archive formats.
System Information Aliases
alias cmount="mount | column -t"– display mounted filesystems in aligned columns.
alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"– show directory structure as a tree.
sbs() { du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'; }– list files in the current directory sorted by size, with human‑readable units.
alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"– attach strace to a process to monitor its writes. alias meminfo='free -m -l -t' – show detailed memory usage. alias ps?="ps aux | grep" – quickly find a process by name.
alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"– display the current audio volume.
Network Aliases
alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"– recursively download an entire website. alias listen="lsof -P -i -n" – list all network connections. alias port='netstat -tulanp' – show listening ports and associated programs.
gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/
/' | sed -e 's/.*fullcount>//'; }– display the number of unread Gmail messages for a user. alias ipinfo="curl ifconfig.me && curl ifconfig.me/host" – reveal your public IP address and hostname.
getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1 | grep address | egrep 'city|state|country' | awk '{print $3,$4,$5,$6,$7,$8}' | sed 's\ip address flag \\' | sed 's\My\\'; }– obtain geolocation information for a given IP.
Fun Aliases
kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {"; <>; while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"
" for split/,/,$_[3]} print "}"' | dot -Tpng | display -; }– generate a visual graph of kernel module dependencies. alias busy="cat /dev/urandom | hexdump -C | grep 'ca fe'" – produce seemingly random output to appear busy.
The above aliases and functions were gathered from the author’s personal .bashrc and well‑known online resources; copying the plain‑text version into your own .bashrc instantly provides the described productivity benefits.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
