Command alias functionality for Linux systems

Source: Internet
Author: User
Tags aliases

In the process of managing and maintaining a Linux system, a large number of commands will be used, and some long commands or usages are often used, and it is undesirable to repeatedly and frequently enter a very long command or usage. This process can be simplified by using the command alias feature.

1. System-defined aliases
In general, some command aliases are already defined in the system, and you can use the alias command to view the command aliases that you have defined:
#alias命令将输出所有已经定义的命令别名
# 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 '
As can be seen from the above results, when we use the command CP (copy File command), the system uses cp-i instead of the CP in the command. In addition, it defines the LS command and the colors it uses, the move File command mv, the delete command rm, and so on.
The relevant files for setting system aliases are saved in the/etc/profile.d/directory (System alias directory) and can be viewed in the following ways:

#进入目录/etc/profile.d/
# cd/etc/profile.d/
#查看目录中的文件
# ls
colorls.csh glib2.sh krb5-workstation.csh lang.sh vim.csh
colorls.sh gnome-ssh-askpass.csh krb5-workstation.sh less.csh vim.sh
Glib2.csh gnome-ssh-askpass.sh lang.csh less.sh which-2.sh
#查看文件less. CSH Contents
# cat Less.csh
# The following is the content of LESS.CSH, which defines the aliases used by the LS command, such as the alias
# Less initialization script (CSH)
if (-x/usr/bin/lesspipe.sh) and then
Setenv Lessopen "|/usr/bin/lesspipe.sh%s"
endif
# cat colorls.sh
# Color-ls initialization
alias ll= ' Ls-l ' 2> /dev/null
alias l.= ' ls-d. * ' 2>/dev/null
...

2. User-defined aliases
Many times administrators define command aliases according to their own usage habits. For example, the command to view the contents of the current file is compatible with the View text command type in DOS:
#为cat命令定义一个别名type
# alias Type= ' Cat '
#使用type命令查看文件alias. txt content
# type Alias.txt
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 '
In the above command, a nickname named type is defined for the cat command first. When the user uses the command type, the system automatically replaces it with the Cat command. Www.2cto.com
3. To cancel a defined alias
To cancel an already defined command alias, you can use the Unalias command with the alias:
# Unalias Type
# type Alias.txt
-bash:type:command not found

4. Save Alias Settings

Aliases defined with the alias command are lost when the system restarts or when the user logs back in. Alias profiles can be added to the System alias directory, but aliases defined in this way are valid for all users and are generally not recommended.
If you are defining a global alias, it is generally advisable to add the command to the global profile/etc/profile. For example, define a global alias:
# echo "Alias type= ' Cat '" >>/etc/profile
This command adds alias pg= ' cat ' to the file/etc/profile.
Note: Be careful when working with system profiles such as/etc/profile, or you may damage the system. So the above command uses ">>" instead of ">", ">>" to append the content to the end of the file.
If a user wants to define their own command aliases, they can add a command to a file in the user's home directory. Bash_profile. For example, to define a user's own alias:
# echo "Alias vi= ' Vim '" >>~/.bash_profile

SOURCE ~/.bash_profile Finally, exit and re-login for the definition to take effect.

Command alias functionality for Linux systems

Related Article

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.