Command aliasing function of Linux system

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 not advisable to repeatedly and frequently enter a long command or usage. In this case, the command alias feature can be used to simplify the process.

1. Aliases defined by the system
Usually, some command aliases have been defined in the system. To view the defined command aliases, you can use the alias command:
#alias command will output all defined command aliases
# 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 When we use the command cp (copy file command), the system will use cp -i instead of cp in the command. In addition, the ls command and the colors it uses, the moving file command mv, the delete command rm, etc. are also defined.
The relevant files for setting system aliases are saved in the /etc/profile.d/ directory (system alias directory), which can be viewed using the following methods:

#Enter the directory /etc/profile.d/
# cd /etc/profile.d/ #View
the files in the directory
# 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
#View the content of the file less.csh
# cat less.csh
# The following is the content of less.csh, which defines aliases such as colors used by the ls command
# less initialization script (csh)
if ( -x /usr/bin/lesspipe.sh ) 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, make the command to view the content of the current file compatible with the view text command type in DOS: #Define
an alias type for the cat command
# alias type='cat'
#Use the type command to view the content of the file alias.txt
# 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, an alias named type is first defined for the cat command. When the user uses the command type, the system will automatically use the cat command to replace it. www.2cto.com
3. Undefined Aliases
To cancel an already defined command alias, use the unalias command on the alias:
# unalias type
# type alias.txt
-bash:type: command not found

4. Save alias settings

Aliases defined with the alias command will be lost when the system is restarted or the user logs back in. You can add alias configuration files in the system alias directory, but aliases defined in this way are effective for all users, and this method is generally not recommended.
If you want to define a global alias, it is generally recommended to add the command to the global configuration file /etc/profile. For example to 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 operating system configuration files such as /etc/profile, otherwise the system may be damaged. So ">>" is used in the above command instead of ">", and ">>" means append the content to the end of the file.
If a user wants to define his own command alias, he can add the command to the file .bash_profile in the user's home directory. For example, to define the user's own alias:
# echo "alias vi='vim'">>~/.bash_profile

source ~/.bash_profile Finally, log out and back in for the definitions to take effect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604064&siteId=291194637