This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
alias is a shell builtin that gives a short name to a longer command. It is most useful for commands you always run with the same options.
Create an alias
alias name='command with options'
There must be no spaces around the = sign, and the command goes in quotes when it contains spaces.
For example, I almost always want du and df to print sizes in human-readable units:
alias du='du -h'
alias df='df -h'
From now on, typing df runs df -h. Anything you add after the alias is passed along, so df /home runs df -h /home.
List your aliases
alias with no arguments prints every alias defined in the current shell:
alias
To see a single one:
alias df
To find out whether a name is an alias, a function, a builtin or a program on disk:
type df
df is aliased to `df -h'
Remove an alias
unalias du
To remove all of them from the current shell:
unalias -a
Make aliases permanent
An alias typed at the prompt lasts only until you close that shell. To keep it, add the alias line to ~/.bashrc:
echo "alias df='df -h'" >> ~/.bashrc
Then reload the file, or open a new terminal:
source ~/.bashrc
On Debian and Ubuntu, the default ~/.bashrc also loads ~/.bash_aliases if it exists, which keeps your aliases in a file of their own. If you use Zsh, the file is ~/.zshrc.
Bypass an alias
Sometimes you want the original command, not the alias. Any of these runs the real ls once:
\ls
command ls
/usr/bin/ls
This matters with protective aliases such as alias rm='rm -i': use \rm when you really do not want to be asked for every file.
Aliases and sudo
By default this does not work:
alias ll='ls -lah'
sudo ll
sudo: ll: command not found
The shell only expands an alias in the first word of a command, and here the first word is sudo. The fix is an alias for sudo itself that ends in a space:
alias sudo='sudo '
When an alias value ends in a space, Bash also checks the next word for alias expansion, so sudo ll becomes sudo ls -lah.
Aliases in scripts
Aliases are meant for interactive use. Bash does not expand them in scripts unless you enable it with shopt -s expand_aliases, and a script does not read your ~/.bashrc anyway. In a script, write the full command or use a function.
When you need a function instead
An alias only puts text in front of your arguments. It cannot place an argument in the middle of a command or use it twice. For that, define a function in the same ~/.bashrc:
mkcd() {
mkdir -p "$1" && cd "$1"
}
mkcd projects/new creates the directory and enters it, which no alias can do.
Some useful aliases
alias ll='ls -lah'
alias la='ls -A'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias free='free -h'
alias ports='ss -tulpn'
alias myip='curl -s https://ifconfig.me'
alias update='sudo apt update && sudo apt upgrade'
alias gs='git status'
Be careful not to hide a real command behind an alias with a different meaning; type name tells you whether the name is already taken.
See also
help alias — the builtin reference (alias is part of the shell, so it has no man page of its own).