How to use the command `history` (with examples)

How to use the command `history` (with examples)

The history command in Unix-like operating systems serves a crucial purpose for users who frequently interact with the command line. It is mainly used to display the list of previously executed commands, offering a quick reference and aiding in the management of command-line history. This can be particularly useful for debugging, auditing, or simply recalling complex commands without needing to retype them. history is a built-in shell feature available in most shells like Bash and Zsh, though some functionalities can vary slightly between different shells.

Use case 1: Display the commands history list with line numbers

Code:

history

Motivation:

Using the history command without any arguments offers a straightforward way to access the list of commands previously executed in the shell. This is particularly helpful when you need to recall a complex command or check the sequence of actions taken during your terminal session.

Explanation:

  • history: This invocation lists all previously executed commands, each accompanied by a line number. The line number serves as a reference point, allowing users to easily refer to or re-execute a specific command from the list by using its number with !number.

Example output:

1  ls
2  cd projects
3  git status
4  nano README.md
5  git add .
6  git commit -m "Initial commit"
7  git push origin main

Use case 2: Display the last 20 commands

Code:

history 20

Motivation:

Accessing only the last 20 commands can be notably beneficial when you are interested in reviewing your most recent actions without scrolling through potentially thousands of entries. This is helpful in scenarios where your terminal has been extensively used over time.

Explanation:

  • 20: The number provided as an argument specifies how many commands from the end of the list should be displayed. In Bash, it shows the last 20 executed commands. However, in Zsh, it behaves differently by showing commands starting from the 20th one, if such a command is in the history.

Example output:

480  pwd
481  mkdir new_folder
482  cd new_folder
483  touch index.html
484  ls
...
499  echo "Hello, World!"

Use case 3: Display history with timestamps in different formats (only available in Zsh)

Code:

For different timestamp formats in Zsh, you can use:

history -d
history -f
history -i
history -E

Motivation:

Having commands associated with timestamps provides a temporal context for when each command was executed. This is particularly useful for users who manage tasks over time and require precise time logging for debugging or collaboration purposes.

Explanation:

  • -d|f|i|E: These options allow you to choose the format in which the timestamp appears:

    • -d: Dates and times are displayed using the default format.
    • -f: Specifies a fixed timestamp format for all entries.
    • -i: Outputs timestamps in ISO 8601 format.
    • -E: Extended timestamp format, which includes seconds.

Example output (using -d):

2023-10-12 10:00:01 cd projects
2023-10-12 10:01:45 git push

Use case 4: Clear the commands history list (only for current Bash shell)

Code:

history -c

Motivation:

Clearing the command history is valuable when you have finished working on sensitive projects and wish to ensure that no command history remains in the current session. This adds an extra layer of privacy and security.

Explanation:

  • -c: This option instructs Bash to clear the in-memory history list of the current shell session but does not affect the history file saved on disk.

Example output:

There is no typical output, but running history after this will result in an empty list.

Use case 5: Overwrite history file with history of current Bash shell

Code:

history -w

Motivation:

After making modifications to your session’s history or clearing it, you may want to update the history file stored on disk to reflect these changes. This ensures that your desired history state persists across future sessions, adhering to personal preferences or compliance requirements.

Explanation:

  • -w: This option is used to write the current in-memory history to the history file, overwriting the existing content.

Example output:

No direct output, but the command writes the current in-memory history to the history file.

Use case 6: Delete the history entry at the specified offset

Code:

history -d offset

Motivation:

Deleting a specific command from your history can be useful if the command contains sensitive information or if you simply want to tidy up your history record. By specifying the offset, you pinpoint exactly which command to remove.

Explanation:

  • -d offset: The -d option signifies a desire to delete a history entry, with offset being the line number of the entry you wish to remove. You can determine the correct offset using the basic history command.

Example output:

No direct output, but upon running history, the specified entry will no longer be present.

Conclusion:

The history command offers a wealth of utilities that enhance productivity, security, and convenience on the command line. Understanding each of these functionalities enables users to efficiently manage their command-line history, secure sensitive data, and quickly access past commands, making history a powerful tool in any user’s shell toolkit.

Related Posts

How to use the command 'gh gist' (with examples)

How to use the command 'gh gist' (with examples)

The gh gist command is part of the GitHub CLI (Command-Line Interface) tool, allowing developers to manage their GitHub Gists directly from the terminal.

Read More
How to use the command 'Get-WUApiVersion' (with examples)

How to use the command 'Get-WUApiVersion' (with examples)

The Get-WUApiVersion command is utilized primarily within the PowerShell environment to retrieve the version of the Windows Update Agent (WUA) installed on a system.

Read More
How to Use the Command 'telinit' (with Examples)

How to Use the Command 'telinit' (with Examples)

telinit is a command used in Unix-like systems to change the runlevel of the system.

Read More