Linux Command - alias

alias - Creates aliases. Alias are simple names for commands.

alias created with the current shell session are not permenent. Add alias to ~/.bashrc to make it permanent.

To Remove an alias, use unalias alias_name command.

Syntax

1
alias alias_name="command_to_run"

Examples

create alias for git add and git commit

1
alias gac="git add . && git commit -m"

create an alias for searching processes

1
alias psg="ps aux | grep -v grep | grep -i -e VSZ -e"

Sample output for psg firefox

1
2
3
$ ps aux | grep -v grep | grep -i -e VSZ -e firefox
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
xing 8321 0.6 3.2 1856788 261572 tty2 Sl+ 16:17 0:11 /usr/lib/firefox/firefox

Note: grep command greps ‘VSZ’ so that the column names are displayed.

Bash Function

alias are limited in their scope. You can’t access arguments in an alias. The alternative is to create Bash functions in .bashrc file.

Sample bash function in .bashrc file. It is used to create directory and move to that directory immediately

1
2
3
4
mcd () {
mkdir -p $1
cd $1
}

Reference