The Linux system has methods for configuring aliases for commands

  • 2020-09-28 09:19:20
  • OfStack

What is an alias

In the management and maintenance of Linux systems, a large number of commands will be used, some very long commands or usage will be frequently used, repeated and frequent input of a very long command or usage is not desirable. You can use the alias feature to simplify the process.

Some commands under Linux are as follows rm cp mv Be careful when using these commands such as delete moves or use aliases as a reminder

In simple terms

1. Warn cp rm mv before using dangerous commands

2. Complex commands are more convenient to use

Problem: Configuring the alias do not use rm means that do not use rm is prompted when the rm command is run.

1. The single command to output do not use rm to the screen interface is echo


[root@liuhao ~]# echo "do not use rm"
do not use rm
[root@liuhao ~]# 

2. View existing aliases


[root@liuhao ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

3.1 Configure rm with the alias do not use rm


[root@liuhao ~]# alias rm='echo do not use rm'

Note: = There is no space on either side of the direct input character

3.2 check


[root@liuhao ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='echo do not use rm'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@liuhao ~]# rm /data/oldboy.txt 
do not use rm /data/oldboy.txt
[root@liuhao ~]# 

Note the alias has been configured successfully but only this time the connection xshell has been disconnected or the system has been restarted

4.1 Make the alias of the configuration permanent


[root@liuhao ~]# vim /etc/profile

G under the vim editor means straight to the bottom o (lowercase o) means one more line below the current one

After entering vim, press upper case G and lower case o

Enter the following command at the lowest end


##
alias rm='echo do not use rm'

4.2 Check whether the last two lines of the file were input successfully and output with tail


[root@liuhao ~]# tail -2 /etc/profile
##
alias rm='echo do not use rm'
[root@liuhao ~]# 

5. Make the configuration source effective


[root@liuhao ~]# source /etc/profile
[root@liuhao ~]# rm /data/oldboy.txt 
do not use rm /data/oldboy.txt
[root@liuhao ~]# 

6. Modify/root /. bashrc


[root@liuhao ~]# vim /root/.bashrc
#alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

will alias rm='rm -i'  Add # to it as shown above and exit vim and save

7. How do I delete something when rm is aliasing

7.1 The status of rm is now directly used


[root@liuhao ~]# rm /data/oldboy.txt 
do not use rm /data/oldboy.txt
[root@liuhao ~]# rm -f /data/oldboy.txt 
do not use rm -f /data/oldboy.txt
[root@liuhao ~]# rm -r /data/oldboy.txt 
do not use rm -r /data/oldboy.txt
[root@liuhao ~]# 

Files cannot be deleted using rm directly

7.2 How to delete a file

Method 1


[root@liuhao ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
0

Method 2


[root@liuhao ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
1

which rm can see the absolute path of the rm command

conclusion


Related articles: