Skip to content

Instantly share code, notes, and snippets.

@corporatepiyush
Last active August 27, 2024 08:59
Show Gist options
  • Save corporatepiyush/c88811520945b6aff315ce3dfd38de39 to your computer and use it in GitHub Desktop.
Save corporatepiyush/c88811520945b6aff315ce3dfd38de39 to your computer and use it in GitHub Desktop.
Useful command line aliases
## Colorize the ls output ##
alias ls='ls --color=auto'
alias ll='ls -lA --color=auto'
alias la='ls -la --color=auto'
alias lt='ls -ltr --color=auto'
# update hosts file
alias update_dns_hosts='sudo chmod 774 /etc/hosts && \
curl https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts > hosts && \
sudo mv hosts /etc/hosts && \
sudo chmod 644 /etc/hosts'
## Colorize the grep command output for ease of use (good for log files)##
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# search for command tried in past
alias a_history_search='history | grep'
alias a_history_clear='history -c'
alias a_history_show='history 1'
# Linux DNS flush
alias a_dns_flush='sudo systemd-resolve --flush-caches'
# Empty trash (not typically applicable on Linux, but a similar command)
alias a_trash_empty='sudo rm -rfv ~/.local/share/Trash/*'
# Process management
alias a_proc_search='ps aux | grep -i'
alias a_proc_kill='pkill -f'
# File search
alias a_file_search='find ~ -iname'
alias a_file_search_here='find . -iname'
# Print user information
alias a_print_username='echo $USER'
alias a_print_user_host='echo $USER@$HOSTNAME'
alias a_print_userid='id -u'
alias a_print_user_groups='groups'
alias a_print_home='echo $HOME'
# Get your public IP
alias a_print_ip='curl -s https://ifconfig.me'
# Package management
alias a_update='sudo apt update && sudo apt upgrade'
alias a_cleanup='sudo apt autoremove && sudo apt clean'
alias a_search='apt search'
alias a_info='apt show'
alias a_install='sudo apt install'
alias a_list='dpkg -l'
alias a_remove='sudo apt remove'
# PostgreSQL aliases (if PostgreSQL is installed via system package manager)
alias a_pg_start='sudo systemctl start postgresql'
alias a_pg_stop='sudo systemctl stop postgresql'
alias a_pg_restart='sudo systemctl restart postgresql'
alias a_pg_status='sudo systemctl status postgresql'
# PostgreSQL common commands
alias a_pg_list_db='sudo -u postgres psql -c "\l"' # List all databases
alias a_pg_list_tables='sudo -u postgres psql -c "\dt"' # List all tables in the current database
alias a_pg_list_users='sudo -u postgres psql -c "\du"' # List all users and their roles
# MongoDB aliases (assuming MongoDB installed via package manager)
alias a_mongo_start='sudo systemctl start mongod'
alias a_mongo_stop='sudo systemctl stop mongod'
alias a_mongo_restart='sudo systemctl restart mongod'
alias a_mongo_status='sudo systemctl status mongod'
alias a_mongo_shell='mongosh' # MongoDB shell
# MongoDB common commands (using mongosh)
alias a_mongo_list_db='mongosh --eval "show dbs"' # List all databases
alias a_mongo_list_collections='mongosh --eval "show collections"' # List all collections in the current database
# Combined database services management
alias a_db_start='a_pg_start && a_mongo_start'
alias a_db_stop='a_pg_stop && a_mongo_stop'
alias a_db_status='a_pg_status && a_mongo_status'
# Homebrew services management equivalent (Debian doesn't have direct equivalent, but can use systemd commands)
alias a_bs_list='systemctl list-unit-files --type=service'
alias a_bs_start='sudo systemctl start'
alias a_bs_stop='sudo systemctl stop'
alias a_bs_restart='sudo systemctl restart'
# alias a_for PostgreSQL server installed from source
alias a_pgs_start='pg_ctl -D /var/lib/postgresql/data start'
alias a_pgs_stop='pg_ctl -D /var/lib/postgresql/data stop'
alias a_pgs_status='pg_ctl -D /var/lib/postgresql/data status'
# List all processes listening on any port
alias a_process_connections='sudo lsof -iTCP -sTCP:LISTEN -n -P'
# List all established connections
alias a_established_connections='sudo lsof -iTCP -sTCP:ESTABLISHED -n -P'
# List all network connections (alternative using netstat)
alias a_all_connections='netstat -tunap | grep -i "tcp"'
# Function to find connections for a specific app
find_app_connections() {
if [ -z "$1" ]; then
echo "Usage: find_app_connections <app_name>"
else
sudo lsof -i -n -P | grep -i "$1"
fi
}
# Function to find which app is using a specific port
find_port_user() {
if [ -z "$1" ]; then
echo "Usage: find_port_user <port_number>"
else
sudo lsof -i :$1 -n -P
fi
}
# Function to show top bandwidth-consuming processes
show_bandwidth_usage() {
sudo iftop -nP
}
# List all open HTTP/HTTPS connections
alias a_http_connections='sudo lsof -i :80 -i :443'
# List all open HTTP/HTTPS connections with process names
alias a_http_connections_detailed='sudo lsof -i :80 -i :443 -sTCP:LISTEN -n -P'
# Basic GET request with headers
alias a_http_get='curl -sSL -D - -o /dev/null'
# POST request with JSON body
alias a_http_post='curl -sSL -X POST -H "Content-Type: application/json" -d'
# Download file with original filename
alias a_http_download='curl -sSLO'
# Show only response headers
alias a_http_headers='curl -sSL -D - -o /dev/null'
# Follow redirects and show final URL
alias a_http_follow='curl -sSL -w "%{url_effective}\n" -o /dev/null'
# Test connection time to a URL
alias a_http_connect_time='curl -sSL -w "Connect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" -o /dev/null'
# Pretty-print JSON response (requires jq, install via: sudo apt install jq)
alias a_http_json='curl -sSL | jq .'
# Function to make a curl request and format the output
http_format() {
curl -sSL -w "\nStatus: %{http_code}\nTime: %{time_total}s\n" "$@"
}
# Function to safely remove files and directories with improved wildcard handling
safe_remove() {
local error_occurred=false
for pattern in "$@"; do
pattern="${pattern/#\~/$HOME}"
find_output=$(find ${pattern%/*} -maxdepth 1 -name "${pattern##*/}" 2>/dev/null)
if [ -z "$find_output" ]; then
echo "No matching items found for pattern: $pattern"
error_occurred=true
continue
fi
while IFS= read -r item; do
if [ -e "$item" ]; then
if [ -d "$item" ] && [ -z "$(ls -A "$item" 2>/dev/null)" ]; then
echo "Directory $item is already empty"
else
if sudo rm -rf "$item" 2>/dev/null; then
echo "Successfully removed $item"
else
echo "Could not remove $item"
error_occurred=true
fi
fi
else
echo "Item $item does not exist"
fi
done <<< "$find_output"
done
if $error_occurred; then
echo "Some items could not be removed or were not found"
return 1
else
echo "All items processed successfully"
return 0
fi
}
# Modified clear_cache_logs function
clear_cache_logs() {
echo "Clearing logs and caches..."
if safe_remove ~/.cache/* /var/log/*; then
echo "Logs and caches cleared successfully"
else
echo "Some logs and caches could not be cleared or were not found"
fi
}
# Modified clear_browser_history function
clear_browser_history() {
echo "Clearing browser histories..."
if safe_remove ~/.mozilla/firefox/*.default-release/places.sqlite \
~/.config/google-chrome/Default/History \
~/.config/chromium/Default/History; then
echo "Browser histories cleared successfully"
else
echo "Some browser histories could not be cleared or were not found"
fi
}
# Define clear_all as an alias
alias a_clear_all='echo "Starting the process to clear all data..."; clear_cache_logs && clear_browser_history && echo "Process completed. All possible data cleared."'
# Kernel
alias a_swap_usage='for file in /proc/*/status ; do awk '"'"'/VmSwap|Name/{printf $2 " " $3}END{ print ""}'"'"' $file; done | sort -k 2 -n -r | head -n 100'
alias a_huge_pages_usage='grep -i AnonHugePages /proc/meminfo'
## Colorize the ls output ##
alias ls='ls -G'
alias ll='ls -lGaf'
alias la='ls -laGf'
alias lt='ls -lGtr'
# update hosts file
alias update_dns_hosts='sudo chmod 774 /private/etc/hosts && \
curl https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts > hosts && \
sudo mv hosts /private/etc/hosts && \
sudo chmod 644 /private/etc/hosts'
## Colorize the grep command output for ease of use (good for log files)##
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# search for command tried in past
alias a_history_search='history | grep'
alias a_history_clear='history -c'
alias a_history_show='history 1'
#Mac OS
alias a_dns_flush='sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder'
alias a_trash_empty='sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl'
alias a_proc_search='ps aux | grep -i'
alias a_proc_kill='pgrep -i -f "$1" | xargs -I{} kill -9 {}'
alias a_file_search='find ~ -iname'
alias a_file_search_here='find . -iname'
alias a_print_username='echo $USER'
alias a_print_user_host='echo $USER@$HOST'
alias a_print_userid='id -u'
alias a_print_user_groups='groups'
alias a_print_home='echo $HOME'
# Get your public IP
alias a_print_ip='curl -sSL https://ifconfig.me'
# Homebrew aliases
alias a_update='brew update && brew upgrade'
alias a_cleanup='brew cleanup'
alias a_search='brew search'
alias a_info='brew info'
alias a_install='brew install'
alias a_list='brew list'
alias a_remove='brew uninstall'
# PostgreSQL aliases
alias a_pg_start='brew services start postgresql'
alias a_pg_stop='brew services stop postgresql'
alias a_pg_restart='brew services restart postgresql'
alias a_pg_status='brew services info postgresql'
# PostgreSQL common commands
alias a_pg_list_db='psql -c "\l"' # List all databases
alias a_pg_list_tables='psql -c "\dt"' # List all tables in the current database
alias a_pg_list_users='psql -c "\du"' # List all users and their roles
# MongoDB aliases
alias a_mongo_start='brew services start mongodb-community'
alias a_mongo_stop='brew services stop mongodb-community'
alias a_mongo_restart='brew services restart mongodb-community'
alias a_mongo_status='brew services info mongodb-community'
alias a_mongo_shell='mongosh' # MongoDB shell
# MongoDB common commands (using mongosh)
alias a_mongo_list_db='mongosh --eval "show dbs"' # List all databases
alias a_mongo_list_collections='mongosh --eval "show collections"' # List all collections in the current database
# Combined database services management
alias a_db_start='a_pg_start && a_mongo_start'
alias a_db_stop='a_pg_stop && a_mongo_stop'
alias a_db_status='a_pg_status && a_mongo_status'
# Homebrew services management
alias a_bs_list='brew services list'
alias a_bs_start='brew services start'
alias a_bs_stop='brew services stop'
alias a_bs_restart='brew services restart'
# alias a_for PostgreSQL server installed from source
alias a_pgs_start='pg_ctl -D /Users/$USER/pgsql/data start'
alias a_pgs_stop='pg_ctl -D /Users/$USER/pgsql/data stop'
alias a_pgs_status='pg_ctl -D /Users/$USER/pgsql/data status'
# List all processes listening on any port
alias a_process_connections='sudo lsof -iTCP -sTCP:LISTEN -n -P'
# List all established connections
alias a_established_connections='sudo lsof -iTCP -sTCP:ESTABLISHED -n -P'
# List all network connections (alternative using netstat)
alias a_all_connections='netstat -av | grep -i "tcp"'
# Function to find connections for a specific app
find_app_connections() {
if [ -z "$1" ]; then
echo "Usage: find_app_connections <app_name>"
else
sudo lsof -i -n -P | grep -i "$1"
fi
}
# Function to find which app is using a specific port
find_port_user() {
if [ -z "$1" ]; then
echo "Usage: find_port_user <port_number>"
else
sudo lsof -i :$1 -n -P
fi
}
# Function to show top bandwidth-consuming processes
show_bandwidth_usage() {
sudo nettop -P -n 1
}
# List all open HTTP/HTTPS connections
alias a_http_connections='sudo lsof -i :80 -i :443'
# List all open HTTP/HTTPS connections with process names
alias a_http_connections_detailed='sudo lsof -i :80 -i :443 -sTCP:LISTEN -n -P'
# Basic GET request with headers
alias a_http_get='curl -sSL -D - -o /dev/null'
# POST request with JSON body
alias a_http_post='curl -sSL -X POST -H "Content-Type: application/json" -d'
# Download file with original filename
alias a_http_download='curl -sSLO'
# Show only response headers
alias a_http_headers='curl -sSL -D - -o /dev/null'
# Follow redirects and show final URL
alias a_http_follow='curl -sSL -w "%{url_effective}\n" -o /dev/null'
# Test connection time to a URL
alias a_http_connect_time='curl -sSL -w "Connect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" -o /dev/null'
# Pretty-print JSON response (requires jq, install via: brew install jq)
alias a_http_json='curl -sSL | jq .'
# macOS-specific: Check SSL certificate of a domain
alias a_http_cert='curl -vI --stderr - | grep "SSL certificate"'
# Function to make a curl request and format the output
http_format() {
curl -sSL -w "\nStatus: %{http_code}\nTime: %{time_total}s\n" "$@"
}
# Function to safely remove files and directories with improved wildcard handling
safe_remove() {
local error_occurred=false
for pattern in "$@"; do
# Expand ~ to $HOME
pattern="${pattern/#\~/$HOME}"
# Use find to handle wildcards and permissions
find_output=$(find ${pattern%/*} -maxdepth 1 -name "${pattern##*/}" 2>/dev/null)
if [ -z "$find_output" ]; then
echo "No matching items found for pattern: $pattern"
error_occurred=true
continue
fi
while IFS= read -r item; do
if [ -e "$item" ]; then
if [ -d "$item" ] && [ -z "$(ls -A "$item" 2>/dev/null)" ]; then
echo "Directory $item is already empty"
else
if sudo rm -rf "$item" 2>/dev/null; then
echo "Successfully removed $item"
else
echo "Could not remove $item (likely protected by SIP)"
error_occurred=true
fi
fi
else
echo "Item $item does not exist"
fi
done <<< "$find_output"
done
if $error_occurred; then
echo "Some items could not be removed or were not found"
return 1
else
echo "All items processed successfully"
return 0
fi
}
# Modified clear_cache_logs function
clear_cache_logs() {
echo "Clearing logs and caches..."
if safe_remove ~/Library/Logs/* ~/Library/Caches/* /Library/Logs/* /Library/Caches/*; then
echo "Logs and caches cleared successfully"
else
echo "Some logs and caches could not be cleared or were not found"
fi
}
# Modified clear_browser_history function
clear_browser_history() {
echo "Clearing browser histories..."
if safe_remove ~/Library/Safari/History.db ~/Library/Safari/WebpageIcons.db \
~/Library/Application\ Support/Firefox/Profiles/*/places.sqlite \
~/Library/"Application Support"/BraveSoftware/Brave-Browser/Default/History \
~/Library/"Application Support"/Google/Chrome/Default/History \
~/Library/"Application Support"/Microsoft\ Edge/Default/History \
~/Library/"Application Support"/com.operasoftware.Opera/History; then
echo "Browser histories cleared successfully"
else
echo "Some browser histories could not be cleared or were not found"
fi
}
# Define clear_all as an alias
alias a_clear_all='echo "Starting the process to clear all data..."; clear_cache_logs && clear_browser_history && echo "Process completed. All possible data cleared."'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment