Last week, I had a very nice reader email me to ask a question about how to look through the commands he’d previously typed into Terminal. So if, for example, he’d used a “defaults write” command to alter how OS X looks or acts, he could find exactly what he did in his history to know how to reverse the changes.
There are quite a few ways you could go about this; I mentioned one method a couple of years ago that’ll help you search through the history, which is useful if you at least know a keyword in the command you’re trying to find. However, you could also just view your history in either the Terminal window or as a text file. To do the former, simply type “history” in at the prompt, and you’ll get what you’re looking for.
If you’re familiar with using the “greater than” (“>”) symbol in Terminal, that’s an easy way to make “history” easier to read—it’ll take the command and create a file you designate with the output. So this example…
history > /Users/melissa/Desktop/history.txt
…means “run the ‘history’ command, and then create a new file on Melissa’s desktop called ‘history.txt’ with the results.” That little “greater than” symbol is handy for all sorts of stuff within Terminal, but be aware of its one big caveat: If there is already an existing file in your requested location with the same name, using the command above will replace it. If you want to add to existing text instead of replacing it, type “greater than” twice (“>>”).
Finally, there is one more method I’m going to suggest, and I think it’s the simplest of all. You could just open the file containing your history and view it as text! Unsurprisingly, though, that file is hidden by default, so to access it, open a new Terminal window, type this command in, and press Return:
That should open your list of previously entered commands within your default text editor, and you can search through it at your leisure.
(If the command above didn’t work, you may have navigated away from your user folder within Terminal, and if so, you can use the “cd ~” command [without the quotes] before running “open .bash_history” again.)
And finally, it wouldn’t be a command-line tip if I didn’t offer the usual disclaimers—be very careful of what you type, always copy and paste commands if you aren’t confident in your skills, blah de blah blah. Terminal’s awesome, but boy, could you cause issues if you really tried. Please don’t try, OK?