How to create alias commands in Linux

The terminal is a powerful tool, which can be customized to your needs with the alias command. The most used example is to override the remove command rm. The remove command deletes files without a prompt. You can create an alias for the remove command and add the -i option to add a prompt before you delete any file or directory.

The most common way to add aliases is to create a .bash_aliases file in your home directory and pointing to this file from the .bashrc file. To accomplish this you can add the follwing snippet to the end of the .bashrc file.

if [ -e ~/.bash_aliases ]
. ~/.bash_aliases

Open the .bash_aliases file and define an alias which follows the following pattern.

alias name='command'

You can tpye alias into your terminal to see the existing alias commands. Here you can see the commands from my Fedora distribution:

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias rm='rm -i'
alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'

Now it’s up to you to fill the .bash_aliases file with aliases which meets your desires. Write in the comments what are your favourite aliases.


Leave a comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.