Skip to content

Instantly share code, notes, and snippets.

@kyletimmermans
Last active February 26, 2024 02:42
Show Gist options
  • Save kyletimmermans/88350c00be2176c5d06a897fb9292588 to your computer and use it in GitHub Desktop.
Save kyletimmermans/88350c00be2176c5d06a897fb9292588 to your computer and use it in GitHub Desktop.
Collection of useful / helpful Bash aliases and functions I've found / edited / created
alias ..='cd ..' # Faster
alias cls='clear' # Faster
alias ll='ls -lhFA' # Better ls
alias vi='vim' # Use vim instead
alias neofetch='fastfetch' # Better neofetch
alias psa='ps aux | grep --color=auto' # View process(es) by name e.g. psa 'sudo find'
alias hs='history | grep --color=auto' # Search through command history e.g. 'git diff'
alias open='xdg-open' # Linux version of OSX open (Open directory in file explorer)
alias pbcopy='xclip -selection clipboard' # Linux version of OSX pbcopy (Copy text to clipboard)
alias pbpaste='xclip -selection clipboard -o' # Linux version of OSX pbpaste (Paste text from clipboard)
alias rds='find . -name ".DS_Store" -type f -delete' # Remove .DS_Store files in dir recursively
alias upgrade='sudo apt-get update && sudo apt-get upgrade -y' # Update and upgrade (Debian based)
alias dups='git diff @{upstream}' # "Diff upstream" - Git diff local copy w/ upstream branch (remote branch)
alias serve='python3 -m http.server' # Start simple http server to serve files in current dir @ localhost:8000
alias partytime='curl parrot.live' # Not helpful, just fun
# Find and replace recursively in file(s)
# This will replace all found strings in-
# -current dir and dirs below current dir
replace() {
if [ "$#" -ne 2 ]; then
echo "Usage: replace <string to replace> <new string>"
return
fi
if [[ $(uname) == *"Darwin"* ]]; then
find . -type f -exec sed -i '' "s/$1/$2/g" {} +
else
find . -type f -exec sed -i "s/$1/$2/g" {} +
fi
}
# Print your public IP address (IPv4)
getip() {
output=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com 2>&1)
if echo "$output" | grep -q "couldn't get address"; then
echo "No internet"
else
echo "$output" | awk -F'"' '{ print $2 }'
fi
}
# Get the weather forecast of a location
weather() {
# Usage: weather <location> or just "weather" and use given IP location
if [ $# -eq 0 ]; then
curl -s wttr.in
else
echo ${*:1} | tr ' ' '+' | xargs -I {} curl -s wttr.in/{}
fi
}
# Install software
install() {
# Debian-based
if [ -e /etc/debian_version ]; then
sudo apt-get install -y $1
return
# CentOS / RHEL / Fedora
elif [ -e /etc/redhat-release ]; then
sudo dnf install -y "$1"
return
# Arch / Manjaro
elif [ -e /etc/arch-release ]; then
sudo pacman -S --noconfirm "$1"
return
# OpenSUSE
elif [ -e /etc/SuSE-release ]; then
sudo zypper -n install $1
return
# MacOS
elif [[ $(uname) == *"Darwin"* ]]; then
if brew info "$1" | grep -q "homebrew-cask"; then
brew install --cask "$1"
else
brew install "$1"
fi
return
else
echo "Package manager not supported"
return 1
fi
}
# Make dir and cd into it
mkcd() {
mkdir -p $1
cd $1
}
# Count number of files and directories
count() {
if [[ "$1" == "-r" ]] || [[ "$1" == "--recursive" ]]; then
echo "Files: $(find . -type f | wc -l | tr -d '[:space:]')"
echo "Directories: $(find . -type d | grep -v '^.$' | wc -l | tr -d '[:space:]')"
else
echo "Files: $(find . -maxdepth 1 -type f | wc -l | tr -d '[:space:]')"
echo "Directories: $(find . -maxdepth 1 -type d | grep -v '^.$' | wc -l | tr -d '[:space:]')"
fi
}
# Interface with the cheat.sh website
# Learn about command with: cheat <command>
# Learn about programming with <programming lang>/<topic>
cheat() {
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "-u" ]] || [ $# -eq 0 ]; then
echo "Usage: cheat <command>"
echo " cheat <programming language>/<topic>"
echo "\n"
echo "Examples: cheat xxd"
echo " cheat python/dictionary+comprehension"
return
else
curl cheat.sh/$1
fi
}
# Function to extract common compressed file types
extract () {
if [ -f "$1" ]; then
case $1 in
(*.tar.xz) tar -xvf "$1" ;;
(*.tar.bz2) tar -jxvf "$1" ;;
(*.tar.gz) tar -zxvf "$1" ;;
(*.bz2) bunzip2 "$1" ;;
(*.gz) gunzip "$1" ;;
(*.tar) tar -xvf "$1" ;;
(*.tbz2) tar -jxvf "$1" ;;
(*.tgz) tar -zxvf "$1" ;;
(*.zip|*.cbz|*.epub) unzip "$1" ;;
(*.pax) cat "$1" | pax -r ;;
(*.pax.z) uncompress "$1" --stdout | pax -r ;;
(*.rar|*.cbr) unrar "$1" ;;
(*.z|*.Z) uncompress "$1" ;;
(*.arc) arc e "$1" ;;
(*.zpaq) zpaq x "$1" ;;
(*.exe) cabextract "$1" ;;
(*.lzma) unlzma x "$1" ;;
(*.ace|*.cba) unace x "$1" ;;
(*.7z|*.iso|*.chm|*.wim|*.deb|*.lzh|*.msi|*.cab|*.rpm|*.xar|*.udf|*.arj) 7z x "$1" ;;
(*.dmg) volume_name=$(hdiutil mount "$1" | awk '/\/Volumes/ {print $NF}')
cp -r "$volume_name" .
hdiutil unmount "$volume_name" ;;
(*) echo "'$1' cannot be extracted/mounted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Provide a function to compress common compressed file types (ultimate compressor)
uc() {
if [ $# -lt 2 ]; then
echo "Usage: uc <archive.xyz> file1 file2 directory1 ..."
else
File=$1
shift
case "${File}" in
(*.tar) tar -cf "${File}" "$@" ;;
(*.tar.bz2) tar -cjf "${File}" "$@" ;;
(*.tar.gz) tar -czf "${File}" "$@" ;;
(*.tar.xz) tar -cJf "${File}" "$@" ;;
(*.bz2) bzip2 "${File}" "$@" ;;
(*.dmg) hdiutil create -volname "${File%.dmg}" -srcfolder "$@" -ov -format UDZO "${File}" ;;
(*.gz) gzip "${File}" "$@" ;;
(*.tbz2) tar -cjf "${File}" "$@" ;;
(*.pax) pax -w -f "${File}" "$@";;
(*.pax.z) pax -w -f "${File}" "$@" && compress "${File}" ;;
(*.z|*.Z) compress "${File}" "$@" ;;
(*.7z) 7z a "${File}" "$@" ;;
(*.tgz) tar -czf "${File}" "$@" ;;
(*.zip) zip "${File}" "$@" ;;
(*.rar) rar "${File}" "$@" ;;
(*.arc) arc a "${File}" "$@" ;;
(*.lzma) tar -cf "${File}" --lzma "$@" ;;
(*.ace) unace a "${File}" "$@" ;;
(*.zpaq) zpaq a "${File}" "$@" ;;
(*) echo "Filetype not recognized" ;;
esac
fi
}
# Find file locally (recursive)
fl() {
if [ $# -eq 0 ]; then
echo "Filename required"
return
fi
find . -name "$1" 2>&1 | grep -v 'Permission denied' | grep -v 'No such file or directory'
}
# Find file everywhere (recursive)
fe() {
if [ $# -eq 0 ]; then
echo "Filename required"
return
fi
sudo find / -name "$1" 2>&1 | grep -v 'Permission denied' | grep -v 'No such file or directory'
}
# Check current open ports
ports() {
if [ $# -eq 0 ] || [[ "$1" == "-s" ]] || [[ "$1" == "--simple" ]]; then
nmap localhost
elif [[ "$1" == "-a" ]] || [[ "$1" == "--advanced" ]]; then
if [[ $(uname) == *"Darwin"* ]]; then
sudo netstat -vanp tcp | grep LISTEN && netstat -vanp udp | grep LISTEN
else
sudo netstat -tulpn | grep LISTEN
fi
fi
}
# Better diff with coloring & side-by-side view (Usage: superdiff file1 file2)
# npm install -g git-split-diffs
superdiff() {
if git rev-parse --is-inside-work-tree | grep -q "fatal"; then
git diff --no-index "$1" "$2" | git-split-diffs --color | less -RFX
else
git diff "$1" "$2" | git-split-diffs --color | less -RFX
fi
}
# Regex escape a string
escape() {
printf '%s\n' "$1" | sed 's/[.[\(*^$+?{|]/\\&/g'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment