No Account Yet?

You are not logged in.

Add to: JBookmarks Add to: Facebook Add to: Windows Live Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Blogmarks Add to: Technorati Add to: Newsvine Add to: Google Information
Introduction to the Alias Command E-mail
Linux HowTo's - Beginner Linux HowTo's
Written by Tim Inkpen   
Saturday, 14 June 2008 09:56

 

alias

What:

-Linux allows a complex (or commands with long names) to be given a shorter form using a simple character string

-This is called an alias

-You create aliases with the alias command

-This command is built  into the Bash shell, not a separate program



 

Where:

-usually set in home ~/.bashrc file (home/username/.bashrc)

-this allows alias to be used by interactive subshells.

-Aliases that are to be the defaults for all users are placed in the /etc/bashrc file

How (a) The Rules:

Syntax

alias name-of-alias=value

i.e.

alias r=rm

Rules:

1. No space is allowed before or after the = (equal sign)

2. If spaces/tabs are needed then quotation marks (usually single quotes) must be used

     i.e.

     alias cp='cp -i'

3. Aliases can be nested, so that you can have aliases that call other aliases.

     i.e.

     You create two aliases:

     alias lll='ls -la'

     alias tdr='cd /home/harry'

     You can now combine these two aliases to create another alias:

     alias lcd='tdr | lll'          

4. Aliases are disabled for noninteractive shells (i.e. shell scripts)

 

 

HOW (B) – PRATICAL USES

 

Adding Aliases

1) Temporary Alias

To add an alias that will only last as your current session:

     alias lll='ls -la'

2) Permanent Alias – Single User

a) vi /home/user/.bashrc
   i.e.

        vi /home/Harold/.bashrc

Note: The file is called .bashrc not bashrc. There is a period (.) at the beginning of the file

name.

b) Add aliases desired

    i.e.

        alias lll='ls -la'

c) Save file (ESC, then qw )

 

3) Permanent Alias – Root

a) As root:

         cd ~ 

b) Edit .bashrc file

         vi .bashrc 

c) Add desired aliases

    i.e.

        alias lll='ls -la'

d) Save file (ESC, then qw )

 

4) Permanent Universal Aliases

a) As root:

    vi /etc/bashrc

    Note: Unlike the other bashrc files, this has no dot at the front of the file name

b) Go to the end of the file, put in a comment and add the aliases desired:

     i.e.
# Custom Alias for this machine

alias rm='rm -i'

alias mv='mv -i'

alias cp='cp -i'

c) Save file

 

NOTE:  The alias command allows several commands to be strung together.

Example

As a system administrator, one of you main jobs will be working with Apache, the main web server for Linux. As part of this work you will frequently have to look for Apache (httpd) processes. The standard way to do this is:

ps -ef | grep httpd

Rather than having to type this over & over again you can create an alias that allows you to do this in a few key strokes:

alias psh='ps -ef | grep httpd'

 

 

List current aliases:

alias

i.e.

 

root@LocalHostSun Jun 01 > alias

alias cp='cp -i'

alias l.='ls -d .* --color=tty'

alias ll='ls -l --color=tty'

alias ls='ls --color=tty'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

 

Note: In this case, nothing follows the alias command. Also some aliases are preset by the OS.

 

List value of a specific alias

Type alias alias_name

i.e.

root@LocalHostTue Jun 10 > alias rm

alias rm='rm -i'

 

Preventing the Shell from Using an Alias

Lets say you have an alias that is the name of a command (only with options):

i.e.

alias rm='rm -i'

In this case whenever you type the rm command you will be prompted for confirmaition (-i).

If you want to run therm command with the –i option then use the full pathname for the command:

i.e.:

/bin/rm test2

You can also us  the \ with the command:

i.e.

\rm test4

 

Removing Aliases

1) Temporary Alias

Use the unalias command, unalias alias-name

i.e.

You have created the following alias for the current session:

alias lll='ls -la'

To remove it, do the following:

unalias lll

 

Comments
Search RSS
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Tuesday, 17 June 2008 11:29 )