DEV Community

Cover image for Mastering the `history` Command in Red Hat Linux: Tracking and Managing Commands
Alexand
Alexand

Posted on

Mastering the `history` Command in Red Hat Linux: Tracking and Managing Commands

The history command in Red Hat Linux records previously executed commands, helping users recall past actions, troubleshoot issues, and monitor system activity.


Using the history Command

To view saved commands:

history
Enter fullscreen mode Exit fullscreen mode

Each command appears with a number for easy reference.

To rerun a specific command:

!n
Enter fullscreen mode Exit fullscreen mode

Replace n with the command number.


Why It Matters

  • Saves Time – Quickly reuse past commands.
  • Fix Mistakes – Modify and rerun incorrect commands.
  • Tracks Activity – Helps administrators monitor user actions.

To repeat the last command:

!!
Enter fullscreen mode Exit fullscreen mode

To rerun a command that starts with a specific word:

!keyword
Enter fullscreen mode Exit fullscreen mode

Managing History

To remove all stored commands:

history -c
Enter fullscreen mode Exit fullscreen mode

To delete a specific entry:

history -d n
Enter fullscreen mode Exit fullscreen mode

Replace n with the command number.

For permanent deletion:

cat /dev/null > ~/.bash_history
Enter fullscreen mode Exit fullscreen mode

Storing Command History

To ensure command logs are saved:

echo 'export HISTFILE=/var/log/bash_history' >> ~/.bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

This stores command history in /var/log/bash_history for tracking.


Conclusion

The history command is a valuable tool for efficiency, troubleshooting, and security. Learning to use it effectively simplifies Linux management and keeps systems running smoothly.

Top comments (0)