Linux命令之history

目录

什么是 history 命令

如何使用history命令

参数详解

history的使用🌰

分页查看某用户的历史命令

显示最近的10条历史命令

快捷执行某条历史命令

使用 -c 选项清除所有的命令历史   谨慎使用!!!

清除部分的历史命令

删除指定行的历史命令


什么是 history 命令

        history 命令被用于列出以前输入的命令和 Bash 日志。Bash 将关闭终端会话时所运行的所有命令并写入历史记录文件(~/.bash_history)。

如何使用history命令

# help history 
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
    
    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.
    
    Options:
      -c        clear the history list by deleting all of the entries
      -d offset delete the history entry at position OFFSET.
    
      -a        append history lines from this session to the history file
      -n        read all history lines not already read from the history file
                and append them to the history list
      -r        read the history file and append the contents to the history
                list
      -w        write the current history to the history file
    
      -p        perform history expansion on each ARG and display the result
                without storing it in the history list
      -s        append the ARGs to the history list as a single entry
    
    If FILENAME is given, it is used as the history file.  Otherwise,
    if HISTFILE has a value, that is used, else ~/.bash_history.
    
    If the HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
    
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.

参数详解

-c                   清空历史列表。
-d offset        根据offset删除记录。如果是正数则表示offset位置的记录,如果为负数则表示从结尾向前offset位置的记录。
-a                    将当前终端的历史记录行添加到历史记录文件。
-n                    将尚未从历史文件中读取的历史行追加到当前历史列表中。
-r                    读取历史文件,并将其内容附加到历史列表中。
-w                   将当前历史记录列表附加到历史记录文件中并且附加它们到历史列表中。
-p                    在每个arg上执行历史记录扩展并在标准输出上显示结果,而不将结果存储在历史记录列表中。
-s                    将每个arg作为单个条目附加到历史记录列表。

注:参数是可选项,可以直接使用 history 查看所有历史命令

history的使用🌰

举🌰:

分页查看某用户的历史命令

# more /home/$UserName/.bash_history

同理 可搭配 tail 、grep 、cat 查看筛选历史命令

显示最近的10条历史命令

# history 10

   90  cd /etc
   91  ls
   92  find / -name known_hosts
   93  cd ..
   94  history
   95  cd .ssh/
   96  ls
   97  cat known_hosts
   98  pwd
   99  history 10

快捷执行某条历史命令

# 执行第 N 条历史命令--需要先知道该历史命令行号
# !n

# 执行最后一条 xxx 开头的历史命令
# !xxx

# 执行最后一条历史命令
# !!
or
# !-1
or
# 按 Ctrl+P 或者 使用上方向键

使用 -c 选项清除所有的命令历史   谨慎使用!!!

# history -c

清除部分的历史命令

# vim ~/.bash_history
### 进入~/.bash_history文件中删除某些不想被人看到的历史命令。
# history -r

删除指定行的历史命令

# history -d 指定行号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值