[Linux]history命令常用用法以及显示时间和执行者

2021-03-22 1610点热度 0人点赞 1条评论

history命令是显示之前执行过的命令的列表,下面介绍下history最长用到的用法。

常用用法举例

# 直接显示运行命令历史记录
history

# 示例
Terry@localhost ~  history
1 cd Applications
2 brew info java
...

# 如果你的历史记录很多,可以使用history | more 或者history | less 来分页查看
history | more
history | less

root@localhost:.ssh# history | more
48 2021-01-21 16:51:42 root chown -R www-data:www-data /var/www/upload
49 2021-01-21 16:52:08 root chmod -R g+rw /var/www/upload
50 2021-01-21 16:52:09 root ll
--More--

#这个时候输入n可以继续往下翻,或者输入q退出

#查看最初的10行
history | head -10

#查看最后的10行,就是最近的10个命令
history | tail -10
#查看最近10个命令也可以更简单,直接history后面跟数字
history 10

#翻动查看最近的命令
#直接使用键盘的向上、向下就可以了

#重复执行上一条命令,输入!!
#一般不建议用,不保证你一定记得最后执行了什么,同样的考虑,后面我也不介绍根据命令的序号(一般会根据命令的执行而变动)代码来执行

root@localhost:.ssh# date
Mon Mar 22 09:23:54 CST 2021
root@localhost:.ssh# !!
date
Mon Mar 22 09:23:55 CST 2021

#搜素举例(利用grep):
history | grep keywords

查看其他用户的history

history命令存储在~/.bash_history,所以其他用户,只要你有权限,可以直接访问对应的文件即可:

more /home/$USER/.bash_history

history命令显示时间和执行者

默认执行history显示的是命令序号和实际命令,而有时候,我们往往希望能看到执行的命令的时间、用户是谁,实现如下:

export HISTTIMEFORMAT="%F %T `whoami` "

root@localhost:.ssh# history 10
1013 2021-03-18 16:26:30 root vim /etc/nginx/sites-enabled/default
1014 2021-03-19 09:43:45 root vim /etc/nginx/nginx.conf
1015 2021-03-22 09:17:27 root history | more
1016 2021-03-22 09:18:44 root history | head -20
1017 2021-03-22 09:20:27 root history | tail -10
1018 2021-03-22 09:20:30 root history 10
1019 2021-03-22 09:23:53 root dae
1020 2021-03-22 09:23:54 root date

如果想要每次进入shell,都默认以这种方式显示history格式,可以把export放到~/.bashrc

vim ~/.bashrc

最后添加如下:

export HISTTIMEFORMAT="%F %T `whoami` "

admin

这个人很懒,什么都没留下

文章评论

  • uncekly

    Hvcxjs Cialis Priligy Sustancia Activa Yywcmp Toothache Abscess Keflex cialis online3 Cialis Qkrhho Generique Amoxicillin Pharmacie Gratuit Tablette Pour

    2022-06-21
  • 您需要 登录 之后才可以评论