美文网首页
Linux 命令历史

Linux 命令历史

作者: BeeBee生信 | 来源:发表于2020-06-27 20:13 被阅读0次

bash shell 的 history 查看命令历史,属于内置函数所以 which history 是找不到的。

$type history
history is a shell builtin

输入 history 查看命令历史每行一个,同时每个记录有个数字配合 ! 可以快速重新运行该命令。

$history 10
 1371  ls
 1372  cp bismark_methylation_extractor1.sh bismark_methylation_extractor2.sh
 1373  vim bismark_methylation_extractor2.sh
 1374  less bismark_methylation_extractor2.sh
 1375  qsub bismark_methylation_extractor2.sh
 1376  vim bismark_methylation_extractor1.sh
 1377  qstat
 1378  history
 1379  type history
 1380  history

这里数字 10 表示只显示 10 条记录。第 1379 命令是 type history 因此输入 !1379 就可以直接重新运行这个命令。

$!1379
type history
history is a shell builtin

为了防止意外运行了错误的历史命令,可以先做个检查。

$!1379:p
type history

也可以用负数,表示倒数第几个历史命令。本来 1379 是倒数第二个,但是刚刚又运行了一次,因此他成了倒数第一个和第三个,所以 !-3 就能把他再运行一次。如果要运行倒数第一个直接 !! 就行。

$!-3
type history
history is a shell builtin

想重新运行前几个命令时还是使用上翻键比较爽快,要找前十几个几十个时还是 history 命令适合。
配合 grep 命令可以帮助搜索到需要的历史,像我这种 1000 多条历史记录的,有时候很需要搜索。

$history | grep qstat
  385  qstat
  630  qstat
 1185  qstat
 1187  qstat
 1197  qstat
 1211  qstat
 1223  qstat
 1231  qstat
 1244  qstat
 1254  qstat
 1265  qstat
 1268  qstat
 1270  qstat
 1274  qstat
 1276  qstat
 1279  qstat
 1284  qstat
 1298  qstat
 1338  qstat
 1359  qstat
 1367  qstat
 1370  qstat
 1377  qstat
 1382  history | grep qstat

也可以 ! 紧跟搜索的字符串,缺点就是他搜索到命令后会直接运行,所以为了保险还是配合 grep 去做搜索比较好。或者是之前说的用 :p 打印命令出来,而不是立即执行,这样可以先检查。

$!type
type history
history is a shell builtin

# 用 :p 先进行打印
$!type:p
type history

-d 参数指定可以删除某条记录。

$history -d 1382

这样就把第 1382 条命令历史记录删除了。
^[str1]^[str2]^ 可以对命令先进行字符串替换然后执行。比方说我先 history | grep qstat 然后发现其实我是想 grep qsub 的,此时把之前命令 qstat 替换为 qsub 就行。

$^qstat^qsub^
history | grep qsub

不过测试的时候发现这个替换可能只能对上一条命令替换执行,更早的命令没成功过。
使用 :n 可以获取命令的第 n 个参数,第一个和最后一个可以分别用 ^$ 符号。比如说我这里第 1394 条记录是 type history ,那么第一个参数是 type 最后一个参数是 history。此时用 !1394:$ 表示取 1394 命令最后一个参数也即 history,那么 !1394:$ 10 就等同于 history 10 命令。

$history 10
 1389  history
 1390  history -d 1382
 1391  history
 1392  history 10
 1393  $type history
 1394  type history
 1395  history --help
 1396  history | grep qstat
 1397  history | grep qsub
 1398  history 10
 
# 取命令最后一个参数
 
 $!1394:$ 10
history 10
 1389  history
 1390  history -d 1382
 1391  history
 1392  history 10
 1393  $type history
 1394  type history
 1395  history --help
 1396  history | grep qstat
 1397  history | grep qsub
 1398  history 10

其实最常用往往是使用上一命令的最后一个参数,比如说拷贝了以前旧脚本打算改一改使用,用 cp 命令后可以直接 vim !$ 或者 vim !!:$ 编辑拷贝的文件。
取全部参数使用 * 按照范围取可以用 n-m 这种风格。
大多时候 Linux 命令都有一些参数是文件/目录路径,使用 :h:t:r 能简单处理路径。使用 :h 能够移除文件名,保留目录路径; :t 相反取文件名;而 :r 移除文件后缀。

(DNA) [/datapool/pengguoyu/20200421BLCA_Omics/WGBS/Shell@vcompute-2-1]
$ll -h /datapool/pengguoyu/20200421BLCA_Omics/WGBS/Shell/Bismark_8.sh
-rw-rw-r--. 1 pengguoyu pengguoyu 500 Jun 15 09:02 /datapool/pengguoyu/20200421BLCA_Omics/WGBS/Shell/Bismark_8.sh
(DNA) [/datapool/pengguoyu/20200421BLCA_Omics/WGBS/Shell@vcompute-2-1]
$ls !$:h
ls /datapool/pengguoyu/20200421BLCA_Omics/WGBS/Shell
Bismark_1.sh  Bismark_5.sh  Bismark_7.sh  Bismark_8.sh  bismark_methylation_extractor1.sh  bismark_methylation_extractor2.sh  deduplicate_bismark.sh  M-bias1.sh  MergeCleanData.sh

Linux 有非常多提升效率的小技巧,硬记是不可能的,尝试多去使用慢慢就熟悉了。

[参考]
history command in Linux with Examples - GeeksforGeeks
How to use the history command in Linux | Opensource.com
技术|如何使用 Bash history 命令
15 Linux Bash History Expansion Examples You Should Know

相关文章

  • HISTSIZE和HISTFILESIZE的区别

    HISTFILESIZE与HISTSIZE的区别 在linux系统中,history命令可以输出历史命令,历史命令...

  • linux命令行常用光标移动快捷键

    Linux 命令行快捷键 涉及在linux命令行下进行快速移动光标、命令编辑、编辑后执行历史命令、Bang(!)命...

  • Linux 笔记

    Linux编辑器:sublime text 3命令:--help 选项man 命令history 历史命令 通配符...

  • Linux 命令历史

    bash shell 的 history 查看命令历史,属于内置函数所以 which history 是找不到的。...

  • Linux基础

    知识点 linux是什么 linux历史 linux目录配置 linux用户和组的概念 linux基础命令:gro...

  • Linux用户登录后精确命令History记录

    Linux用户登录后精确命令记录(history 按时间、用户显示命令记录) 设置保存历史命令的文件大小 保存历史...

  • Linux快捷命令

    linux下命令行操作快捷键及技巧 历史相关命令 !!:执行上一条命令!num:执行历史命令中第num条命令!-n...

  • Bash的基本功能-历史命令与命令补全

    (Bash是Linux中的标准shell) 1、历史命令 Linux非常的智能啊,会把我们之前敲过的命令保存起来,...

  • Linux 命令行快捷键

    涉及在linux命令行下进行快速移动光标、命令编辑、编辑后执行历史命令、Bang(!)命令、控制命令等。让bash...

  • CentOS系统账户和登录安全

    一、合理使用Shell历史命令记录功能 在Linux下可通过history命令查看用户所有的历史操作记录,同时sh...

网友评论

      本文标题:Linux 命令历史

      本文链接:https://www.haomeiwen.com/subject/lwivfktx.html