Thursday, January 17, 2008

HowTo: Create and Overwrite Existing Alias

An existing linux command exists from linux binary folder, and this command when executed display a result which is not complete for your regular daily task. Simply to say, you need to add or delete its default command parameter when executed. Worry not, here's an entry to create and/or overwrite an existing linux command aliases.

Create Linux Command Alias
Assuming that you have a current assignment for the day, and you noticed that during the accomplishment of this task, you keep on executing and listing out all files under /var/www/html/zoneA/project/2007/public_html/web1 directory folder. Creating shortcut commands of those repetitive commands can be done using linux command aliases.

Basic example of creating linux command alias.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ls -la /var/www/html/zoneA/project/2007/public_html/web1
# alias wow='ls -la /var/www/html/zoneA/project/2007/public_html/web1'
# wow
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Issuing the two linux commands below will display the same results
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# wow
# ls -la /var/www/html/zoneA/project/2007/public_html/web1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, here's how to verify and list out current linux command aliases

# alias
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -ll --color=tty'
alias wow='ls -la /var/www/html/zoneA/project/2007/public_html/web1'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To modify an existing linux command alias, simply issue as root

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# alias cp='cp -i -arpv'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Verify that the particular linux alias has been modified by listing out all aliases

# alias
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias cp='cp -i -arpv'
alias l.='ls -d .* --color=tty'
alias ll='ls -ll --color=tty'
alias wow='ls -la /var/www/html/zoneA/project/2007/public_html/web1'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Be careful with linux aliases though it could also confuse system users like so

# alias ls='echo command not found'
# ls
Result:
~~~~~~~~~~~~~~~~~~
command not found
~~~~~~~~~~~~~~~~~~

Linux aliasing command can also be used to execute external scripts. That means, linux aliases can also be used to restrict and/or prevent user from executing particular scripts or commands from the system.

How to use linux aliasing to call and execute an external shell script?

# alias wow='$USER/myown.sh'
# wow

The above creates a wow alias that calls for myown.sh script under the user's home folder. myown.sh could contain some useful script, reminder, note or another linux command, sure you get the idea here.

Enjoy.

Related Readings:
Linux Command Line Shell Variables
Linux Command Aliases Explained
Find Command Translated to Perl Codes

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy