命令别名与历史命令
命令别名配置: alias, unalias
- 查看目前有哪些的命令别名
[root@www ~]# alias alias cp='cp -i' alias l.='ls -d .* --color=tty' alias ll='ls -l --color=tty' alias lm='ls -l | more' alias ls='ls --color=tty' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --show-dot --show-tilde'
- 命令别名配置
[root@www ~]# alias lm='ls -al | more'
- 取消命名别名
[root@www ~]# unalias lm
历史命令: history, HISTSIZE
[root@www ~]# history [n]
[root@www ~]# history [-c]
[root@www ~]# history [-raw] histfiles
选项与参数:
n :数字,意思是『要列出最近的 n 笔命令行表』的意思!
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 命令新增入 histfiles 中,若没有加 histfiles ,
则默认写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中;
-w :将目前的 history 记忆内容写入 histfiles 中!
范例一:列出目前内存内的所有 history 记忆
[root@www ~]# history
# 前面省略
1017 man bash
1018 ll
1019 history
1020 history
# 列出的信息当中,共分两栏,第一栏为该命令在这个 shell 当中的代码,
# 另一个则是命令本身的内容喔!至于会秀出几笔命令记录,则与 HISTSIZE 有关!
范例二:列出目前最近的 3 笔数据
[root@www ~]# history 3
1019 history
1020 history
1021 history 3
范例三:立刻将目前的数据写入 histfile 当中
[root@www ~]# history -w
# 在默认的情况下,会将历史纪录写入 ~/.bash_history 当中!
[root@www ~]# echo $HISTSIZE
1000
当我们以 bash 登陆 Linux 主机之后,系统会主动的由家目录的 ~/.bash_history 读取以前曾经下过的命令,那么 ~/.bash_history 会记录几笔数据呢?这就与你 bash 的 HISTFILESIZE 这个变量配置值有关了!
假设我这次登陆主机后,共下达过 100 次命令,『等我注销时, 系统就会将 101~1100 这总共 1000 笔历史命令升级到 ~/.bash_history 当中。』 也就是说,历史命令在我注销时,会将最近的 HISTFILESIZE 笔记录到我的纪录文件当中啦!
当然,也可以用 history -w 强制立刻写入的!那为何用『升级』两个字呢? 因为 ~/.bash_history 记录的笔数永远都是 HISTFILESIZE 那么多,旧的信息会被主动的拿掉! 仅保留最新的!
[root@www ~]# !number
[root@www ~]# !command
[root@www ~]# !!
选项与参数:
number :运行第几笔命令的意思;
command :由最近的命令向前搜寻『命令串开头为 command』的那个命令,并运行;
!! :就是运行上一个命令(相当于按↑按键后,按 Enter)
[root@www ~]# history
66 man rm
67 alias
68 man history
69 history
[root@www ~]# !66 <==运行第 66 笔命令
[root@www ~]# !! <==运行上一个命令,本例中亦即 !66
[root@www ~]# !al <==运行最近以 al 为开头的命令(上头列出的第 67 个)