Wednesday, October 21, 2009

Play with alias command in linux os

In the bash-shell:
alias new_name='command'
alias ll='ls -l' (Defines a new command ll)
alias ls='ls -F --color=auto' (Redefines an existing command. Use \ls (backslash) or full path (/bin/ls) to use the unaliased version)
Add the commands to .bashrc or .bash_profile to load them automatically at log-in.
The unalias command can be used to turn it off.

Other shells -- zsh, tcsh, pdksh -- use other commands/notations to set alias.

You can also make symbolic-links to make aliases:
ln -s /usr/bin/w /usr/bin/whodo (Makes whodo an alternative name for w)

Definition: alias is a built-in shell command in Linux / Unix operating systems. It can save you a lot of typing by assigning a name to long commands. For example, if you need to repeatedly copy files from one directory to another using the command

cp /home/jones/data1/* /usr/local/share/latest/.

you can replace this long command line with a short easy to remember name like "moveit" using the alias command as follows:

alias moveit="cp /home/jones/data1/* /usr/local/share/latest/."

Then you can just type "moveit" at the command prompt and it will do the same copy operation.

If you change your mind and want to assign a different command to the alias, you do it same way-the previous definition will be overwritten.

An alias definition is valid only in the current shell execution environment and the execution environments of its subshells.

Making Aliases Permanent

The main disadvantage with the alias command is that any alias set up with it remains in effect only during the current login session (i.e., until the user logs out or the computer is shut down). Although this might not be much of a problem for systems which are rebooted (i.e., restarted) only infrequently (such as corporate database servers), it can be a nuisance for systems that are frequently rebooted (e.g., home computers).

Fortunately, however, any alias can be made more enduring (i.e., until it is explicitly removed) by writing it to the appropriate configuration file with a text editor. The name and location of such file can vary according to the system. In the case of Red Hat Linux, an alias for any user can be added to the .bashrc file in that user's home directory. Because this file is read at login, the change will not take effect until the user has logged in again.

Aliases for the root user can be made permanent by entering them in the .bashrc file in the root's home directory, i.e., in /root/.bashrc. System-wide aliases can be put in the /etc/bashrc file. The system needs to be restarted before system-wide aliases can take effect.

Removing Aliases

The command unalias, which is likewise built into bash and some other shells, is used to remove entries from the current user's list of aliases. Its syntax is

unalias [-a] name(s)

For example, the following would remove the alias rm which was created in an earlier example:

unalias rm

unalias removes not only aliases created during the current session but also permanent-a option tells unalias to remove all aliases for the current user for the current shell. aliases that are listed in system configuration files. The

A second way to remove an alias is by using the alias command to create a new alias with the same name. This overwrites the existing alias with that name.

A third way is to delete the alias from the appropriate configuration file using a text editor. For example, in the case of Red Hat, deleting an alias in the bash shell for a user named joe would involve removing the appropriate line in the file /home/joe/.bashrc. Likewise, an alias can be modified by editing the appropriate line in the configuration file.

Post a Comment

You might also like :

Related Posts with Thumbnails