16

As all of you know, Mac OS X stores all commands that run from Terminal in Terminal itself, however, I want to delete (for example) the last 3 commands from the terminal. How can I do this?

12

Mac OS does not store anything anywhere.

bash, which is the shell that is run in the terminal, does store the command history.

The terminal command history is stored in a hidden file in your user directory called .bash_history

This means the file is: /Users/{username}/.bash_history

You have to enable "Show System Files" to see it (from the "View" menu in finder).

The file is just a textual list of everything you type in the shell. You can open it in TextEdit, or your editor of choice.


You should probably close Terminal.app to flush any recent changes into the file before making changes, or it may get overwritten from a version Terminal.app has in memory.

  • 2
    Commands are not stored on the fly but only when the shell exits or when prompted to do so (e.g., history -a or history -w). Usually the last commands are not yet in the file. – Matteo Dec 21 '11 at 6:20
  • Whoops, I meant to add you should probably close all instances of terminal.app before editing this file. Fixed. – Fake Name Dec 21 '11 at 8:18
19

It's not a Mac OS feature but a shell feature. Assuming that you are using bash (the default):

  • the command history is stored in memory and persisted in a file (usually ~/.bash_history)
  • you can list and edit the history with the history command

With man bash (in the SHELL BUILTIN COMMANDS section) you will find the description of the different options.

You can:

  • list the history entries

    $ history
    
  • delete the whole history

    $ history -c
    
  • delete a given entry

    $ history -d offset
    
  • both answers are correct, i chose the earliest one .. thank you bro .. – JAHelia Dec 21 '11 at 7:15
  • on doing history -c, it says fc: event not found: -c. Then I manually opened the file, deleted the contents. Closed and reopened the terminal and history does not exist now. – inquisitive Nov 16 '15 at 5:22
2

For Mac Terminal user

delete bash commands history with the following command $ echo '' > ~/.bash_history

For Mac zsh user

delete zsh commands history with the following command $ echo ''> ~/.zsh_hostory

  • Note that this will delete the entire history, and not just the last three lines. – John N Apr 12 '17 at 11:06
0

In addition to Text Edit, Also, if you are old school, use 'vi' to edit the file and remove the offensive commands. Similar command-line editors are emacs (which shares editing shortcuts with OS X) or nano (which has simplified navigation and visual help on how to save and quit the editor without needing a reference sheet).

  • 1
    That would be covered by "You can open it in TextEdit, or your editor of choice", which is in the accepted answer. – John N Apr 12 '17 at 11:05
  • We prefer answers to stand on their own. Right now it's more a comment than an answer, can you add at least the name of the file to edit? – nohillside Apr 12 '17 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .