Linux-别名alias和历史命令history

7.Shell

本章同步视频:https://edu.51cto.com/sd/e4874

7.3 命令别名与历史命令

7.3.1 命令别名 alias, unalias

1.查看别名

[root@localhost ~]# alias

alias cp='cp -i'

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 mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

2.定义别名(临时,重启后无效)

[root@localhost ~]# alias ctmp="cd /tmp"

[root@localhost ~]# ctmp

[root@localhost tmp]# alias

alias cp='cp -i'

alias ctmp='cd /tmp'

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 mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

3.取消别名

[root@localhost tmp]# unalias ctmp

[root@localhost tmp]# alias

alias cp='cp -i'

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 mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

4.永久生效的别名定义(当前用户)

[root@localhost ~]# vim ~/.bashrc

clipboard.png

[root@localhost ~]# ctmp      

bash: ctmp: command not found...          #未生效

[root@localhost ~]# source .bashrc    #执行配置文件

[root@localhost ~]# ctmp                     #生效

[root@localhost tmp]#

[root@localhost tmp]# su - calf                #测试其他用户

Last login: Tue Apr 14 05:04:31 EDT 2020 on pts/0

[calf@localhost ~]$ ctmp

bash: ctmp: command not found...          #无效

5.让所有用户永久生效的别名(RHCE考点)

[root@localhost tmp]# vim /etc/bashrc       #在最后加上一行

clipboard.png

[root@localhost tmp]# source /etc/bashrc   #执行配置文件

[root@localhost tmp]# su - calf        #切换用户

Last login: Tue Apr 14 05:04:48 EDT 2020 on pts/0

[calf@localhost ~]$ ctmp             #生效

[calf@localhost tmp]$

7.3.2 历史命令history

1.语法

[dmtsai@study ~]$ history [n]

[dmtsai@study ~]$ history [-c]

[dmtsai@study ~]$ history [-raw] histfiles

选项与参数:

n   :数字,意思是『要列出最近的 n 笔命令行表』的意思!

-c  :将目前的 shell 中的所有 history 内容全部消除

-a  :将目前新增的 history 指令新增入histfiles中,若没有加histfiles,

则预设写入 ~/.bash_history

-r  :将histfiles的内容读到目前这个 shell 的 history 记忆中;

-w  :将目前的 history 记忆内容写入histfiles中!

2.用法

[root@localhost tmp]# history

[root@localhost tmp]# history 5

  470  source /etc/bashrc

  471  su - calf

  472  history

  473  history -5

  474  history 5

3.特殊用法

[dmtsai@study ~]$ !number

[dmtsai@study ~]$ !command

[dmtsai@study ~]$ !!

选项与参数:

number  :执行第几笔指令的意思;

command :由最近的指令向前搜寻『指令串开头为 command』的那个指令,并执行;

!!      :就是执行上一个指令(相当于按↑按键后,按 Enter)

[root@localhost ~]# history 5

  475  cd

  476  history -5

  477  history 5

  478  echo aaaa

  479  history 5

[root@localhost ~]# !478       #执行第478条指令

echo aaaa

aaaa

[root@localhost ~]# !echo    #执行最近的以echo开头的指令

echo aaaa

aaaa

[root@localhost ~]# !!       #执行上一条指令

echo aaaa

aaaa

4.其他说明

(1)保存指令的上限

[root@localhost ~]# echo $HISTSIZE

500

#如需临时修改上限,直接修改环境变量的值即可,如下:

[root@localhost ~]# export HISTSIZE=800

[root@localhost ~]# echo $HISTSIZE

800

#永久修改如下:

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

clipboard.png

修改第46行后面的数值即可,然后保存退出,执行下面的命令。

[root@localhost ~]# source /etc/profile

(2)查看历史命令存放的位置

[root@localhost ~]# cat ~/.bash_history

#注:此时可能看不到最近使用的命令,因为history是在用户退出时写入数据,如果需要提前写入数据,可以执行以下代码:

[root@localhost ~]# history -w

(3)让命令带时间和下达者

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

#在文件里添加如下一行(不限位置)

export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  `whoami` "

#保存退出,注意反引号。

[root@localhost ~]# source /etc/profile

[root@localhost ~]# history

    7  2020-04-09 09:43:57  root env

    8  2020-04-09 09:44:01  root set

    9  2020-04-09 09:57:33  root read name

本章同步视频:https://edu.51cto.com/sd/e4874


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章