Skip to content

history MacOS Command Guide

The MacOS history command allows users to view a list of previously executed terminal commands. By accessing this command history, users can easily recall and re-run specific commands without having to manually type them again. This feature can improve efficiency and productivity for users who frequently use the terminal for various tasks. The history command displays a numbered list of commands, making it easy to reference and execute specific commands by their corresponding numbers. Additionally, users can customize the behavior of the history command by specifying options such as the number of commands to display or ignoring duplicate commands. By familiarizing themselves with the history command, MacOS users can leverage this tool to streamline their workflow and make the most of their terminal sessions.

history Syntax:

Terminal window
history [options] [number]

Options:

OptionDescription
-cClear the history list
-dDelete a specific entry
-aAppend new history lines to history file
-nRead all history lines without checking lines for duplicates
-rRead the history from the history file and append it to the current history list
-wWrite the current history to the history file
-pReplace the current history with the history lines in the file

Parameters:

ParameterDescription
numberRecall the command at the given number in the history list

history Command Samples:

Show the Command History

Terminal window
history

Displays the list of previously executed commands.

Run a Previous Command by Number

Terminal window
!5

Executes the command at position 5 in the command history.

Search the Command History

Terminal window
history | grep "keyword"

Filters the command history for commands containing the specified keyword.

Clear Command History

Terminal window
history -c

Clears the entire command history.

Limit the Number of Commands Displayed

Terminal window
history 5

Displays only the last 5 commands from the history.

Redirect Command History to a File

Terminal window
history > command_history.txt

Saves the command history to a text file named “command_history.txt”.

Repeat the Last Command Entered

Terminal window
!!

Executes the most recently entered command again.

history FAQ:

How do I view the list of previous commands in MacOS?

To view the list of previous commands in MacOS, simply use the following command:

Terminal window
history

How do I search for a specific command in the history in MacOS?

To search for a specific command in the history in MacOS, you can use the following command:

Terminal window
history | grep "keyword"

How do I limit the number of commands displayed in the history in MacOS?

To limit the number of commands displayed in the history in MacOS, you can use the following command with the desired number of lines:

Terminal window
history <number_of_lines>

How do I clear the history of commands in MacOS?

To clear the history of commands in MacOS, you can use the following command:

Terminal window
history -c

How do I save the history of commands to a file in MacOS?

To save the history of commands to a file in MacOS, you can use the following command with the filename:

Terminal window
history > filename.txt

How do I exclude specific commands from being saved in the history in MacOS?

To exclude specific commands from being saved in the history in MacOS, you can use the following command including the command to exclude in the HISTIGNORE variable:

Terminal window
export HISTIGNORE="command_to_ignore"

Applications of the history command

  • Tracking previously executed commands
  • Repeating or recalling previous commands
  • Analyzing the user’s command-line activity
  • Troubleshooting by reviewing past commands executed