Linux Classes
Share This With a Friend  

What Are Linux File Aliases Used For?

Aliases: Create Meaningful Synonyms for Commands

Defining an alias is another way to minimize your work at the keyboard, and you can also eliminate the need to remember long, awkward commands by creating synonyms that are more meaningful to you. Here are some examples: alias dir='ls -l'
alias dogs='find . -name "*dog*" -print'

In this example, the first alias tells bash that when you enter dir on the command line, it should actually execute the ls -l command instead. If you're a hard-core DOS user, you could use alias to create DOS-like synonyms for many Linux commands.

The second alias lets you enter dogs instead of that long, ugly find command shown above. Don't worry about the ls and find commands right now. Just keep in mind that the alias command can save you some keystrokes and make it easier to remember a command.

Undoubtedly you will find other clever things to do with the alias command and add them to your .profile file so they will be available each time you log in. Your .profile file contains a series of comm ands that bash executes automatically when you log in, similar to autoexec.bat the DOS world.

Previous Lesson: Command History
Next Lesson: Redirection



Comments - most recent first
(Please feel free to answer questions posted by others!)

New Guy     (31 Oct 2012, 18:40)
How long does an alias command last? For example: alias home=cd. How long would I be able to type home and return home with erroring out? Just my current session or does it last forever?

Namchy     (14 Aug 2012, 09:19)
Grat work, Sir!
Thank you.
theophilu kakero     (10 Apr 2012, 20:50)
help mi to write a linux script dat prompt users to login and it check if password and username are correct
ahmad     (24 Feb 2012, 15:14)
I would like to thank you for such a good stuff you've written
Tauseef     (10 Feb 2012, 13:33)
how to create an aliase where using that aliase we want to create a script ?
Bhatt Bipin     (13 Jan 2012, 04:35)
It is very helpful to make DOS like command.
Thank you.
Jam     (25 Oct 2011, 22:10)
I want to create an alias that will only be valid for my current login session such that when I type the letters It at the command line it will run the command Is -lat How can I do this
Kabilan Kannan     (20 Oct 2011, 21:23)
The site is really awesome. Thanks Dr Bob :)
Akshay     (14 Oct 2011, 09:35)
if you want to set the alias's permanently edit your ~/.bashrc add them..
or use
echo alias cls='clear' >> ~/.bashrc
and remember ">>" is very important if > is used you'l be totally f***ed up
Bob Rankin     (30 May 2011, 11:19)
The bash manpage says this:

When bash is invoked as an interactive login shell, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

For an interactive shell that is NOT a login shell, bash reads and executes commands from ~/.bashrc, if that file exists.

Clear as mud? :-)
CENG.Zeinab     (14 May 2011, 05:51)
Please, I need to build a bash command cgrep that search the indicated files using color, ignoring cases and showing the line number.I am requested to add a manual for this command.Help me with that,thanks
Dan     (24 Apr 2011, 03:19)
Hi Bob,
Can u tell me whr i can find .profile (i mean wht is the path for this file).
gunjankapoor     (13 Apr 2011, 05:51)
I want to save the alias commands so that when i again open a terminal, all the alias commands should be there. So please let me know how to do that.
!codemonkey     (06 Mar 2011, 14:24)
.bashrc and .profile have similar functions. which one you have depends on which shell you are using.
Tod     (14 Jan 2011, 18:58)
Sorry, answered my own question. .profile auto-executes the command...
Tod     (14 Jan 2011, 18:56)
Good stuff. What is the difference between .profile and bashrc? I created a bashrc file for my aliases which works fine but wondering if .profile is a better way for some reason.
Bulfennuh     (30 Nov 2010, 01:57)
Very nicce!
dave the guru     (27 Jul 2010, 00:04)
bob great tip but if you have 2000+ linux servers how to achieve this automatically - build an automatic login script?
aluku     (25 May 2010, 01:46)
good for starters!

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.