Discover the last used command beginning with anything without executing it

If you want to discover the last time a specific command was used without actually executing it, follow this format at the command line:

!sudo:p

The above example will print back the last usage of the ’sudo’ command without actually executing it, which is very useful in some situations (like the situation below, where the last time the sudo command was used was deleting everything recursively!). You will see the last used command printed directly below:

$ !sudo:p
sudo rm -rf /var/logs/*

This works with anything, even incomplete commands. Can’t remember that obscure command you used last week, but you know it started with a t? No problem!

!t:p
might print something like this:
time grep -c and rewin.sh

It’s important to note the :p modifier at the end of the command is what is responsible for printing out the command rather than executing it, which is the default behavior for the bash history command ! so if you had just typed !p it would execute the last time a command beginning with ‘p’ was executed, but !p:p will print out the command minus the execution.

Related Posts with Thumbnails

One Comment

» Comments RSS Feed

  1. [...] I just found another great terminal command here: Discover the last used command beginning with anything without executing it… [...]

Leave a Reply

 

Save big on Apple & Mac Deals from Amazon.com

Recent Comments

Shared on Facebook

Support Us

Mac Picks

iPhone & iPad Picks

Mac from PC Picks