前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux中重新运行上次执行的命令的2种方法

linux中重新运行上次执行的命令的2种方法

作者头像
入门笔记
发布2022-06-02 16:12:59
1.2K0
发布2022-06-02 16:12:59
举报
文章被收录于专栏:入门小站入门小站

一般我们在Linux中执行过的命令会被记录到/home/$USER/.bash_history文件。

要获取最近运行的命令,可以使用Up键盘键来检索以前的命令。使用Down键盘键向相反方向找执行过的命令。

历史文件可能包含很多条目,要从命令历史记录中重新执行特定命令,可以运行历史命令。

代码语言:javascript
复制
> history
1005  ls
1006  systemctl restart nginx
1007  systemctl status nginx

然后得到 number(s)command(s) 你想重新执行(例如,如果你想重新启动 nginx 并查看其状态)

1.使用数字编号重复执行历史命令

> !数字编号

代码语言:javascript
复制
> !1006
systemctl restart nginx
> !1007
systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-10-27 20:54:35 CST; 13s ago
  Process: 29074 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 29071 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 29069 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 29076 (nginx)
   CGroup: /system.slice/nginx.service
           ├─29076 nginx: master process /usr/sbin/nginx
           ├─29077 nginx: worker process
           └─29078 nginx: worker process

2.使用命令前缀匹配重复执行历史命令

> !命令前缀

你还可以重新执行以前使用过的命令 (systemctl status nginx)!后跟几个命令前缀字符(例如sys 或者 system)

代码语言:javascript
复制
> !sys
systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
...
//或者
> !system
systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
...
//或
> !systemctl
systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
...

注意:!和命令前缀之间不能有空白字符

相关文章

linux之history命令

linux之history使用技巧

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-10-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 入门小站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.使用数字编号重复执行历史命令
  • 2.使用命令前缀匹配重复执行历史命令
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档