Command Line Basics: Create Custom Commands with Alias

May 30, 2009 by
Filed under: bash, command line, HowTo, linux 

In today's Command Line Basics we'll create some customized commands with alias. You can think of alias as a sort of command line shortcut. Odds are, your system already has a few aliases defined by default. If you enter the command by it's self, without an argument, it will tell you what aliases already exist on your system. Open a terminal window and give it a try.

alias
alias l='ls -CF'
alias la='ls -A'
alias ls='ls --color=auto'

You may see some output that looks like the above. That output shows the proper syntax to create a new alias. One command I like to alias is 'ls -l'.

alias ll='ls -l'

This functionality works great, but defining an alias on the fly only makes it stick to the current shell. If you open another terminal window, this alias won't be defined.

If you want an alias to be available every time you open a terminal window, you can define it in the .bashrc file. Go to your terminal and enter the following:

Affiliate Link
cd
nano .bashrc

Now arrow down to an appropriate place in this file to define your alias. If you're not sure where to put it, the very bottom of the file should be fine. Now add a line to define an alias.

alias ll='ls -l'

Save the file and exit the editor. The .bashrc file is read each time you start a new bash shell. To test your new file you can close the terminal window and open a new one, or force the current window to re-read the .bashrc file with:

source .bashrc

Have an aliased command that you find particularly useful? Leave a comment.

Comments

2 Responses to “Command Line Basics: Create Custom Commands with Alias”

  1. Hayden says:

    Nice tutorial! I found myself opening webpages in chromium from the terminal quite frequently and typing 'chromium-browser http://www.webaddresshere.com' was a bit tedius, so I used an alias to reduce it to 'web <www.webaddresshere.com'.

  2. I like to keep the .bash_aliases (and one each for .bash_functions, etc..) as a separate file and then add ". .bash_aliases"
    to the .bashrc file (In fact, there is usually a if ... fi in all the distro's .bashrc)

    Also instead of doing "source .bashrc" you could also do ". .bashrc"

    check out my blog on the same topic!

    http://srhegde.blogspot.com/2009/05/bash-aliases.html

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>