When operating Linux, you may encounter some very long commands. This command is usually used frequently, and it will feel very troublesome to input and execute it every time. It is convenient if you can set a long command into a short alias. Linux alias
commands can help us set aliases for long commands.
1. List of alias commands on the server
Execute alias
the command on the Linux server, and you can see several familiar commands. As follows.
[test@271ba307f4954c74955b28c8389bc648 ~]$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
For example: ll
the command is not a Linux command, ll
but it is executed by setting an alias ls -l --color=auto
.
Two, alias Add an alias
2.1 alias Add alias syntax
alias The syntax for adding an alias is: alias [别名]='真实命令'
.
2.2 alias Add alias steps
As shown below, add the command alias svccdl for the current Linux user test to enter the log file path of the current day, and the operation steps are as follows.
- Enter the home path of the current user, execute it
vi .bashrc
, and add the following line of text:
alias svccdl='cd /home/test/logs/`date +%Y-%m-%d`'
The contents of the edited .bashrc file are as follows.
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias svccdl='cd /home/test/logs/`date +%Y-%m-%d`'
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
- Execute
source .bashrc
to make the alias permanent.
[test@271ba307f4954c74955b28c8389bc648 ~]$ source .bashrc
- use test
[test@271ba307f4954c74955b28c8389bc648 ~]$ svccdl
[test@271ba307f4954c74955b28c8389bc648 2022-08-30]$ pwd
/home/test/logs/2022-08-30