Discover the last used command beginning with anything without executing it

May 10, 2010 - 1 Comment

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: David Mendez in Command Line

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

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates