Bash Aliases
Bash aliases are a really great way to customize your shell and command line. They let you set short commands for commands you use often, set aliases for speling mistakes, or even small bash scripting functions to do useful things. I've collected a bunch of useful ones here.
Contents |
.bashrc
All the customizations can go in a file at ~/.bashrc, however, I would suggest putting them in ~/.bash_aliases and "sourcing" them with "." in ~/.bashrc, like so:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases # --> Read ~/.bash_aliases , if present.
fi
aliases
Here are a bunch of useful aliases and what they do:
Color ls and grep by default:
alias ls='ls --color=auto' alias grep='grep --color=auto'
Useful cd aliases:
alias back='cd $OLDPWD' alias cd..='cd ..' alias ..='cd ..'
Random useful aliases:
alias ping='ping -c 5' alias more='less' alias recent="ls -lAt | head" alias Xdefaults="xrdb -load ~/.Xdefaults" alias daemons='ls /var/run/daemons'
Check is program/process is running (sproc processname):
alias sproc='ps aux | grep'
If you run archlinux and use yaourt: (:P)
alias pacman='sudo pacman' alias yoaurt='yaourt' alias yuaort='yaourt' alias youart='yaourt' alias yauort='yaourt' alias yuoart='yaourt'
To make a temporary shared fileserver, it shares everything under the current directory, so be careful with it, also requires ext. ip alias below:
alias share='hostip && echo "To end sharing, press Ctrl + C - server on port 56840" && python -m SimpleHTTPServer 56840'
This one basically gives you a diff between your mp3 player and your music directory to see if you missed any music, very useful imho (it's customized for my cowonD2, you have to change mount point and music directory and alias name if you want):
alias syncd2='diff -q ~/music/ /media/D2/MUSIC/ | sort | most'
Check external IP:
alias hostip="wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1"
functions and other tips
This function is extremely useful. It unzips almost any archive for you automatically depending on what type of archive it is:
# Easy extract
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
This is a function to make a dir and then cd into it, very helpful:
# Makes directory then moves into it
function mkcdr {
mkdir -p -v $1
cd $1
}
Have google define a word for you (requires lynx):
# go to google for a definition
define() {
local LNG=$(echo $LANG | cut -d '_' -f 1)
local CHARSET=$(echo $LANG | cut -d '.' -f 2)
lynx -accept_all_cookies -dump -hiddenlinks=ignore -nonumbers -assume_charset="$CHARSET" -display_charset="$CHARSET" "http://www.google.com/search?hl=${LNG}&q=define%3A+${1}&btnG=Google+Search" | grep -m 5 -C 2 -A 5 -w "*" > /tmp/deleteme
if [ ! -s /tmp/deleteme ]; then
echo "Sorry, google doesn't know this one..."
else
cat /tmp/deleteme | grep -v Search
echo ""
fi
rm -f /tmp/deleteme
}
Safely edit a file:
# make a backup before editing a file
safeedit() {
cp $1 ${1}.backup && vim $1
}
Send and receive files via nc (netcat)
ncput() {
if [ $# -lt 2 ]; then
echo "usage: ncput files ip"
return 0
fi
tar -cv $1 | nc $2 1337;
}
ncget() {
if [ $# -lt 1 ]; then
echo "usage: ncget destination"
return 0
fi
nc -l -p 1337 | tar -C $@ -xv;
}
Let google translate stuff for you and read it (if possible)
googlespeak() {
if [ "$2" = "" ]; then
2="en"
fi
wget --user-agent="" -O - "http://translate.google.com/translate_tts?tl=$2&q=$1" > /tmp/google_translate.mp3
mplayer /tmp/google_translate.mp3
rm /tmp/google_translate.mp3
#exit 0
}
This isn't a function or alias, but a very helpful setting for debugging C/C++ programs and possibly others (enables core dumps):
ulimit -c 10000
final word
This is all the aliases I have at the moment, I will be adding more as I find them, and please feel free to add more yourself. Please try to keep the article clean. Add a description if the alias is somewhat cryptic.
Also, many of these come from http://bbs.archlinux.org/viewtopic.php?id=64607 You can check there for many more.