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


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