Print and Query Command History to Find Specific Past Commands

Feb 6, 2011 - 7 Comments

Terminal in macOS

If you’re trying to remember an exact command you executed via the Terminal but can’t quite come up with it, you can query your command line history to discover old commands that have been run or executed in the past.

This trick to find and retrieve prior commands executed in the command line works on Mac OS, Mac OS X, as well as linux and other unix operating systems too. Anything with a standard history command can use this trick to retrieve prior commands, making it an invaluable tool for systems administrators and command line users.

How to Find Specific Commands from Command History in Mac OS

To track down the command history of a particular command, you need to open the Terminal app and then use the following syntax:

history |grep "search string"

This will look for “search string” in your command history and only print back instances that include the search text.

If you’re unfamiliar with the Terminal and you’re wondering why this might be useful, let’s take an example.

Example: Searching Past “defaults” Commands
Here’s a practical example: I was trying to recall the exact syntax of a defaults write command that I recently used. The defaults commands are often long strings of text that modify behavior of Mac OS X or certain applications, because of their length and obscurity, trying to remember one of these off the top of your head is challenging to say the least.

Instead of hitting the up arrow to scroll through past executions for an eternity, I used the following to narrow my command history to only things with “defaults write” as so:

history | grep "defaults write"

This passes the results of the extensive ‘history’ command through grep to find only instances that include “defaults write” in the command string, you’ll see a results list that resembles something like this:

$ history |grep "defaults write"
44 defaults write full-window -1
51 defaults write invertStoreLinks -bool YES
421 defaults write AutoAcceptInvitesFrom -array-add
426 defaults write com.twitter.twitter-mac ESCClosesComposeWindow -bool true
427 defaults write com.twitter.twitter-mac ESCClosesComposeWindow -bool false
428 defaults write ShowDebugMenu -bool true

Now instead of searching through your entire history list, you have narrowed the results.

Refining the Command History Search for Specifics

You can make the history search as specific or unspecific as you want. For example, if I knew the defaults command I was looking for pertained to I could use the following command to further refine my search:

history |grep "defaults write"

Which would return something like:

44 defaults write full-window -1
51 defaults write invertStoreLinks -bool YES

Try it out yourself. You can do this with any command you’ve entered through the Terminal since all recently executed commands are stored in your history. The defaults command is Mac OS X specific, but history and grep are tools generic to the world of unix, so if you’re ever on a linux machine or otherwise you can use the same techniques.

If you like learning about the underpinnings of Mac OS X, check out our command line tips.


Related articles:

Posted by: David Mendez in Command Line, Mac OS


» Comments RSS Feed

  1. […] discussed other ways of printing and searching through past command history before but this is perhaps the best option […]

  2. […] commands from the terminal, you already know how hard it can be to keep track of them. Sure you can query command history for specific command syntax, and you can always use grep to find executed defaults commands, but […]

  3. […] to be quite useful before, whether it’s for discovering your most frequently used commands, dumping history and searching it to find specific past commands, listing all defaults commands used, or whatever else. That said, […]

  4. […] history command is a useful way to find specific commands that have been used in the past, and it can also be used to discover what your personal most used […]

  5. […] touched on this concept before in a past post about the history command, but focusing on defaults commands is useful enough to deserve individual recognition. […]

  6. […] If you’ve forgotten what defaults write commands you’ve used recently, check out this tip to query your command history. […]

  7. piet says:

    If using bash, Ctrl-r will let you do a reverse search of your history narrowing it down as you type. Once you find it you can press “Enter” to run it or Ctrl-j to retrieve it and allow you to edit it. Fun times.

Leave a Reply


Shop on and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks


iPhone / iPad



Shop on Amazon to help support this site