Aliases are custom commands which can replace a longer command or a group of commands, thus making it faster and easier to execute particular tasks by only typing a few characters. For example, one can only type upgrade instead of typing sudo apt-get upgrade && sudo apt-get upgrade and upgrade his Debian-based system. Aliases are very useful time-savers.

Note: If you are new to the Linux shell, I recommend reading the introduction to Linux command-line first. It explains a bit about what is, how to start and how to use a shell.

On most systems, the shell profile file includes certain aliases to be available by default. These aliases are usually specified in the $HOME/.bashrc file, where $HOME is your home directory, e.g. /home/your_username, and .bashrc is a hidden configuration file which is read and executed by Bash every session. To see what aliases you have defined on your system, open a terminal and use the alias command. Here's a possible output:
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -lF'
alias ls='ls --color=auto'
In the above example, l, la, ll and ls are all aliases. When you type into the terminal la, the shell will actually execute the ls -A command, which is aliased to la, thus listing all the files in the current directory, omitting the virtual files . and ...

So how do you create aliases? It's pretty simple, you add them in a text file which is read by Bash. In this tutorial, we will add aliases to the ~/.bashrc file, which should already exist and is read by default by the shell. Notice that ~ means your home directory (e.g. /home/username) and also that .bashrc is a hidden directory, so you will have to enable viewing of hidden files in your file manager if you decide to edit it with a graphical application. We will add aliases at the end of this file, and since it's a plain text file, a simple text editor should suffice (like Gedit or Nano). For this example I will use Nano:
nano ~/.bashrc
Go to the end of the file, and, under the last line, add aliases in the form of alias_name='commands'. Notice that there should be no spaces between the alias name and the command(s), which in turn is inside the single quote characters.

Take a look at the screenshot below to see several examples of aliases:


Now let's explain them.

alias update='sudo apt-get update && sudo apt-get upgrade'
This lets you upgrade your system (if it's a Debian-based one and uses apt) just by typing update. The && operator will only execute the commands that follow it if the first part was executed successfully (if sudo apt-get update fails, the last part will not be executed anymore).
alias killfx='kill -9 $(pidof firefox)'
This allows you to kill Firefox by typing killfx in the terminal.
alias stripmp3='id3v2 -d *.mp3; id3v2 -s *.mp3'
This uses the command-line tool id3v2 to remove all tags from all the MP3 files in a directory. Notice the * wildcard, which expands into a list of all the files that end in .mp3 and the semicolon, which separates commands.
alias dt='sudo modprobe -r psmouse' # disable touchpad
You can use this one to disable the laptop touchpad by unloading the respective kernel module. Note that what follows after the # sign is a comment. You could also use an alias to enable the touchpad, say alias et='sudo modprobe psmouse'. This way, when you type dt you will unload the kernel module (so you can just use a normal mouse and no touchpad), and you can enable it back by typing et.
alias cda='cd /floyda'
alias cdb='cd /floydb'
alias back='cd $OLDPWD'
These aliases are shortcuts when changing the current working directory. You can make up anything here, e.g. alias cdnet='cd /etc/network/' or automatically edit a file like alias netedit='sudo nano /etc/network/interfaces'. The last one changes the current directory to the last known working directory.
alias chx='chmod 755'
alias dfh='df -h'
alias duh='du -h'
alias src='source $DEBCONF_DIR/bash_profile'
The next alias sets the permissions of a file to 755 (rwxr-xr-x), making it executable for everybody, while the dfh and duh will display the disk usage, and, respectively, the current directory usage in human-readable format.

The last one tells the shell to read (for that it uses source) the file $DEBCONF_DIR/bash_profile. This is just an example which I use on my computer, you may want to use here alias src='source ~/.bashrc'. This is useful to source again (read below) your ~/.bashrc file whenever you modify it.

Keep in mind that these are just examples, you can create your own aliases to virtually any commands. After you're done, save the file (Ctrl+O in Nano followed by Ctrl+X to quit it) and proceed to the next step.
OK, I Added the Aliases, Now What?
Now we have to make Bash aware of the newly added aliases. For this, we need to source the .bashrc file:
source ~/.bashrc
Now everything should be in place. Type alias to see a complete list of aliases, then test them and create some of your own.
By Craciun Dan on March 01, 2013 | Updated: October 19, 2013 | v0.2.1 r2
TuxArena Projects
Online Readers