Bash aliases are awesome. They can save you keystrokes and give you shortcuts to some of your most used commands. I’ll share some of my own down in the answers.
What’s in your .bashrc
file? What bash aliases have you found the most useful?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
alias myips="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'"
shows a listing of your IP addresses, works great if you have multiple /dev/eth* (OSX/nix)
My favorite
alias fuck='sudo !!'
Some functions inside my .zshrc
function clock() {
watch -t -n1 "echo "" && date +""$' '"%T""
}
function c() {
clear; cd $1; l;
}
(for mac users)
# Change directory to the current Finder directory
function cdf() {
target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
if [ "$target" != "" ]; then
cd "$target"; pwd
else
echo 'No Finder window found' >&2
fi
}
One I must have when I use Sublime Text
alias slt='subl'
When I do presentations on my Mac I like to hide the mess on my desktop.
alias hideDesktop='defaults write com.apple.finder CreateDesktop -bool false; killall Finder;'
alias showDesktop='defaults write com.apple.finder CreateDesktop -bool true; killall Finder;'
Start my screensaver
alias afk="open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app"
Send my Mac to sleep:
alias sleep="pmset sleep now"
List global installed packages and bottles.
alias npml='npm list -g --depth=0'
alias brewl='brew leaves'
Some of my favorites:
Merges current branch into master then git checkout back to develop
alias mergemaster='git checkout master; git pull; git merge develop; git push; git checkout develop'
Adds everything from current folder to git
alias add='git add .'
For Node developers:
alias s='npm start'
alias lint='npm run lint'
Docker:
alias dockerstop='docker-compose stop'
alias dockerup='docker-compose up -d'
alias dockerrm='docker-compose rm --all'
In last few months I started using ZSH + http://ohmyz.sh/ as my default shell and it’s AWESOME!
My favorite alias/function:
randompw()
{
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}
Keep all your bash history:
export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
"$(history 1)" >> ~/.bash_eternal_history'
extract files:
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
Complete hosts:
_complete_hosts () {
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
host_list=`{
for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config
do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c
done
for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts
do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
done
sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'`
COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
return 0
}
complete -F _complete_hosts ssh
complete -F _complete_hosts sshs
complete -F _complete_hosts host
complete -F _complete_hosts telnet
complete -F _complete_hosts ping
Complete lxc/lxd containers: (~/etc/containers
must be created first)
_complete_container () {
lxc list|grep PERSIS | awk '{print $2}'|sort|uniq >| /home/$USER/etc/containers
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
host_list=`{
for k in /home/jonathan/etc/containers
do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
done
sed -n -e 's/^[0-9][0-9\.]*//p' /home/jonathan/etc/containers; }|tr ' ' '\n'|grep -v '*'`
COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
return 0
}
complete -F _complete_container c
c() {
lxc exec $1 -- /bin/bash
}
proxy remote:
proxy_src_remote () {
ssh $1@$2 -R 3128:localhost:3128
}
A docker and git alias, mainly because I can’t type and my typos are actually quite comical in terms of their use/misuse.
alias dicker='docker'
alias goit='git'
A few docker shortcuts.
# Docker stuff
# Clean stopped containers
alias cco="docker ps -a | grep "Exited" | awk '{print $1}' | xargs docker rm"
# After that, clean unused images to reclaim disk space
alias cimg="docker images | grep none | awk '{print \$3}' | xargs docker rmi"
# Finally the regular stuff just to save typing.
alias dps="docker ps -a"
alias di="docker images"
alias drmi="docker rmi"
alias drm="docker rm"
A system notification to popup when a long shell task finishes:
# Display an "alert" after running commands. Use like so:
# sleep 10; alert "Done sleeping"
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
Ha ha I have this one:
alias PANIC="espeak 'OH NO, the sky is falling'"
Everyone in the office just rolls their eyes
Here are a few more of my favorite aliases.
Show/hide hidden files in the macOS Finder:
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
Limit ping
to five ECHO_REQUEST packets:
alias ping='ping -c 5'
Display drives and space in human readable format:
alias drives='df -h'
Run this one in ~/ to discover what folders are using the most space:
alias usage='du -h -d1'
Colorize grep
output
alias grep='grep --color=auto -n'
Clear the screen (can’t live without this one!)
alias c='clear'
That’s all, folks!
ew, nano
The publically shareable section of my bashrc file!
I only use one, apt to alias aptitude …
Love the
json
one. Can’t never remember the name of the library!although actually, the
d
alias looks handy