Blah, Cloud.

Adventures in architectures

  • Twitter
  • GitHub
  • Home
  • Blog
  • Kubernetes on vSphere
  • Multi-tenant IaaS Networking
  • Me
    • About
    • CV
    • Contact
Home » Blog » Software » Replaying Linux CLI commands with history

Replaying Linux CLI commands with history

20/06/2015 by Myles Gray Leave a Comment

Very handy little snippet I discovered today, mostly here for my own reference in future.

It’s handy to be able to re-play/re-submit commands that you’ve typed into the CLI before on your Linux box, to do this you can use the history command.

Let’s take a look at my webserver hosting the site, just as an example:

[email protected]:~# history
  781  nano /etc/cron.hourly/.placeholder sess_
  782  crontab
  783  crontab -e
  784  nano /etc/cron.d/php5
  785  sudo bash
  786  exit
  787  sudo bash
  788  service php5-fpm restart
  789  service nginx restart
  790  service varnish restart

If I want to look for particular entries I can just grep the output:

[email protected]:~# history | grep "cat"
 1005  cat /etc/nginx/sites-enabled/default
 1006  cat /etc/nginx/sites-enabled/www.mylesgray.com
 1027  cat mini/README.txt
 1137  cat var/report/1084963200249

Then if you pay attention to the left hand column, there is a history number, to re-enter a command we simple put a bang (!) in front of the number in the CLI:

[email protected]:~# !1006
cat /etc/nginx/sites-enabled/www.mylesgray.com
## https://blah.cloud -> https://blah.cloud
server {
.
.
.
etc

If you want to execute the very last command you typed:

[email protected]:~# !!

If you want to execute a command issued n commands previous:

[email protected]:~# !-n

If you want to execute the last command starting with word:

[email protected]:~# !word

If you want to execute the last command containing word:

[email protected]:~# !?word

If you want to view the parameters passed to the last command:

[email protected]:~# !*

If you want to view the parameters passed to command n commands previous:

[email protected]:~# !-n*

As you can see, plenty of options, these are by no means exhausted, you can combine them to create any kind of filter in-between.

Why not follow @mylesagray on Twitter for more like this!

Show some love:

  • Reddit
  • Twitter
  • Pocket
  • LinkedIn
  • Email
  • Telegram

Similar things I've written

Filed Under: Software Tagged With: cli, linux

About Myles Gray

Hi! I'm Myles, and I'm a Dev Advocate at VMware. Focused primarily on content generation, product enablement and feedback from customers and field to engineering.

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Myles Gray

Hi! I'm Myles, and I'm a Dev Advocate at VMware. Focused primarily on content generation, product enablement and feedback from customers and field to engineering. Read More…

Categories

Tags

active directory authentication CBT cisco datastore dell design esxi fortigate iscsi jumbo frame kubernetes lab linux load-balancing lun md3000i mtu networking NginX nic nsx openSUSE osx pxe readynas san sdelete serial teaming ubuntu vcenter vcloud director vcsa vexpert video VIRL vmdk vmfs vmware vsan vsphere vsphere 6 vsphere beta windows

Subscribe to Blog via Email

Copyright © 2021 · News Pro Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.