Tip: Using Alias in Terminal

Discussion in 'Linux' started by rbil, Sep 24, 2008.

  1. rbil

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    You can create your own commands to use in the terminal. This is done by defining an "alias" in the hidden file .bashrc in your home directory.

    So let's create a couple of aliases:

    You know that the command ls -l will give a long listing of your files and ls -la will give a long listing with hidden (dot) files also showing.

    mousepad .bashrc

    See the existing aliases and just add a couple more:

    alias ll='ls -l'
    alias lla='ls -la'

    Save and close the file.

    Now open a terminal and you have access to the 2 new commands.

    ll ... will give you a long listing
    lla ... will give you a long listing with hidden files.

    Of course you can create commands that are much longer than that as aliases, saving much repetitive typing.

    Cheers.
     
    rbil, Sep 24, 2008
    #1
  2. rbil

    melhiore

    Joined:
    Sep 3, 2008
    Messages:
    1,503
    Likes Received:
    0
    Location:
    Bolton, Lancashire, UK
    And after you restart machine have you them still or they disappear?? ;)
     
    melhiore, Sep 24, 2008
    #2
  3. rbil

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    They remain until you re-edit the .bashrc file.

    Cheers.
     
    rbil, Sep 24, 2008
    #3
  4. rbil

    scottro

    Joined:
    Aug 31, 2008
    Messages:
    347
    Likes Received:
    0
    You can create an alias on the fly as well

    Just type alias
    ls='ls -l'

    for example. (Useful when it's a one time thing.
    However, when you open a different terminal, it won't have that alias.

    To unset it on the run

    /ls

    (This is useful for me--on RH based systems ls is aliases to ls color--this is often useful, but on certain backgrounds, with my eyes, it can make some of their color choices difficult to read.) So, while I don't want to completely unset the alias, at times I don't want the color, and then I can just type
    /ls

    The other way way would be
    unalias ls

    Again all changes like that (done on the fly in a terminal) will only hold for that terminal. Open a second terminal, and the only aliases that persist are those which you set in .bashrc as per rbil's helpful tutorial.
     
    scottro, Sep 24, 2008
    #4
  5. rbil

    scottro

    Joined:
    Aug 31, 2008
    Messages:
    347
    Likes Received:
    0
    Actually, there's a slight difference between the two unsetting commands I gave above.

    Doing /ls only unsets it for that one time.
    Doing unalias ls unsets it for that entire session in that terminal. (So for example, if you have the alias in .bashrc but don't want it for a little while, do unalias ls. Now, any ls commands you type in that terminal (but only that terminal) will not use the alias.
     
    scottro, Sep 24, 2008
    #5
  6. rbil

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    Here's another useful alias to add to .bashrc

    alias back="cd \"\$OLDPWD\""

    This allows you to easily return to the directory you were in before the one you're in now. Example:

    cd /etc
    back ... back to previous directory

    Cheers.
     
    rbil, Sep 24, 2008
    #6
  7. rbil

    Ace_Rimmer

    Joined:
    Aug 16, 2008
    Messages:
    103
    Likes Received:
    0
    Doesn't "cd -" do the same thing? (And it's already built into bash.)

    But anyway, thanks for posting the great tip. I use mutt for my imap E-mail, and have set up a couple of short aliases so I don't have to type things such as "mutt -f /home/user/mbox" and "mutt -f /home/user/sent" to retrieve messages that are archived locally.

    toodle-pip!
     
    Ace_Rimmer, Sep 24, 2008
    #7
  8. rbil

    scottro

    Joined:
    Aug 31, 2008
    Messages:
    347
    Likes Received:
    0
    In Unix there is always more than one way of doing the same thing. The corollary is that someone will think your way is stupid. :)

    (Not that there's been any insults here.)
    I think it has to do with people's preferences and what they use most. The standard cd just takes you back to your $HOME directory, which is what I most often do. cd - will, as was said, put you back in the last directory you were in--some folks might use that more often than I do.
    For example, if *I* used that cd alias, then when I want to go $HOME, I have to either do cd ~ or /cd. Both of those, for no particular reason, seem to take more effort for me to type than cd -
    That's why I say it's a very individual thing.

    Aliases, however, are an extremely handy thing. Sigh, what was the old joke--you could tell how new someone was if they had the alias
    kitty='cat'
     
    scottro, Sep 24, 2008
    #8
  9. rbil

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    re: cd -

    Never knew that. Live and learn. Thank you!

    Cheers.
     
    rbil, Sep 24, 2008
    #9
  10. rbil

    Ace_Rimmer

    Joined:
    Aug 16, 2008
    Messages:
    103
    Likes Received:
    0
    Geepers! I guess sometimes the student teaches the master!

    :D :shock: :eek: :lol:
     
    Ace_Rimmer, Sep 25, 2008
    #10
  11. rbil

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    That's what makes Linux so interesting. No matter how long one uses it, there's always more to learn. :)

    Cheers.
     
    rbil, Sep 25, 2008
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.