Set command alias in Linux

Source: Internet
Author: User
Recently, a Django site needs to frequently access the varwwwsitemycitsm Directory. every time you have to repeat the input string, you have recently created a Django site, the/var/www/site/mycitsm/directory needs to be accessed frequently. it is troublesome and time-consuming to enter this long string of paths repeatedly each time, is there a good way to get an alias for a command such as "cd/var/www/site/mycitsm". every time you just enter this alias, you can enter this directory?

Fortunately, the Linux system provides a useful tool called alias, which allows us to set an alias for frequently used but overly lengthy commands, in the future, you only need to enter a brief alias to achieve the same effect.

Usage: alias [-p] [name [= value]...] note that '=' and the string cannot contain spaces.

Display the alias currently set:
Shell> alias-p
Alias l. = 'ls-d. * -- color = tty'
Alias ll = 'ls-l -- color = tty'
Alias ls = 'ls -- color = tty'
Alias vi = 'vim'
Alias which = 'Alias |/usr/bin/which -- tty-only -- read-alias -- show-dot -- show-tilde'

Or enter:
Shell> alias-p
Alias l. = 'ls-d. * -- color = tty'
Alias ll = 'ls-l -- color = tty'
Alias ls = 'ls -- color = tty'
Alias vi = 'vim'
Alias which = 'Alias |/usr/bin/which -- tty-only -- read-alias -- show-dot -- show-tilde'

If you only want to display the meaning of an alias, enter alias name, for example:
Shell> alias ll
Alias ll = 'ls-l -- color = tty'

To set an alias for a command, enter the new alias command = 'original command options/parameter', for example:
Shell> alias site = 'CD/var/www/site/mycitsm /'

To cancel an alias, enter the unalias name, for example
Shell> unalias site

However, there is a problem with the preceding setup method, that is, the configured command alias is only valid for the current session. the alias set before the connection is disconnected and reconnected is not valid.

You can write the alias setting command into the startup file to make the alias take effect permanently. Most Linux distributions use one of the following three startup files:
$ HOME/. bash_profile
$ HOME/. bash_login
$ HOME/. profile
You can write the command for setting aliases into the startup file, so that the alias will take effect every time you connect to the system. If you want to take effect immediately after the command is written to the startup file, execute the source command, for example:
Source $ HOME/. bash_profile

The preceding command alias settings solve the problem that the command alias only takes effect for calls. However, the command alias written into the startup file of a specific user's main directory is only valid for this user. It has no effect on other users, which is usually expected under normal circumstances. However, if the configured alias is valid for any user, you can write the alias setting command into the global startup file, such as/etc/profile.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.