DEV Community

Cover image for Unlock the Secrets of Your Command Line with the History Command
Richard Chamberlain
Richard Chamberlain

Posted on • Edited on

Unlock the Secrets of Your Command Line with the History Command

I recently worked on a project that required accessing the Linux terminal through a web interface. Unfortunately, this setup didn’t support the use of cut-and-paste functionality, which meant I had to rely heavily on manual typing. While this was initially frustrating, it turned out to be an excellent opportunity to address some of the bad habits I had developed over time. To borrow a phrase, it reminded me that those who don’t learn from the history command are doomed to retype it.

Table of Contents

History Command

The history command is a powerful tool in Linux, designed to keep track of commands entered into the terminal. I’ve often used it to look up commands I vaguely remember but need to revisit to refresh my memory. During my recent project—where I couldn’t cut and paste—I discovered a new appreciation for the history command as I began using it to quickly rerun commands.

For example, here’s a snippet from my terminal history that illustrates how I utilized it:

history
  547  echo "Rebuilding sendmail.cf file"
  548  echo "=========================="
  549  vim /etc/mail/sendmail.mc
  550  m4 sendmail.mc >sendmail.cf
  551  systemctl restart sendmail
  552  echo "Subject: Test Email"|sendmail  -v info@sebostechnology.ca
  553  echo "Rebuilding after changing access file"
  554  echo "=========================="
  555  vim /etc/mail/access
  556  makemap hash /etc/mail/access < /etc/mail/access
  557  vim /etc/mail/sendmail.mc
  558  m4 sendmail.mc >sendmail.cf
  559  systemctl restart sendmail
  560  journalctl --unit=sendmail
  561  echo "Subject: Test Email"|sendmail  -v info@sebostechnology.ca
Enter fullscreen mode Exit fullscreen mode

With the history command, I could easily reference or rerun previous commands, saving time and effort when performing repetitive tasks like rebuilding configuration files or troubleshooting. It’s an invaluable resource that turns the terminal into a personal assistant, logging your every step.

Rerunning a command through history

The history command makes it incredibly simple to rerun a previous command by using the ! symbol followed by the command’s associated number. For instance, to rerun m4 sendmail.mc >sendmail.cf, I could type either !550 or !558, depending on which instance I want to execute. This functionality is especially helpful for repetitive tasks.

During my project, I found myself regularly running a sequence of the same commands. To streamline this process, I combined the history command with Bash's && operator, allowing me to execute multiple commands in one go. For example, here’s a set of commands I frequently used:

556  makemap hash /etc/mail/access < /etc/mail/access
558  m4 sendmail.mc >sendmail.cf
559  systemctl restart sendmail
561  echo "Subject: Test Email"|sendmail  -v info@sebostechnology.ca
Enter fullscreen mode Exit fullscreen mode

By leveraging history, I could run all four commands with a single line:

!556 && !558 && !559 && !561
Enter fullscreen mode Exit fullscreen mode

This command sequence translates to:

makemap hash /etc/mail/access < /etc/mail/access && m4 sendmail.mc >sendmail.cf && systemctl restart sendmail && echo "Subject: Test Email"|sendmail  -v info@sebostechnology.ca
Enter fullscreen mode Exit fullscreen mode

Using this method saved me a significant amount of time and reduced the risk of errors from manual typing. It also highlights how history can be more than just a log—it can serve as a productivity booster.

But how else can the history command be helpful? For starters, you can search for specific commands using history | grep, create reusable scripts from frequently used sequences, or even prevent retyping errors by referencing previous entries. The possibilities make it an essential tool for anyone working in the terminal.

How the history command can be helpful

The history command offers a wide range of features that can be incredibly helpful in managing and optimizing your workflow in the terminal. Here are some practical ways to make the most of it:

  1. Starting Fresh: Before beginning a new project, it’s sometimes useful to clear the history to ensure you’re starting with a clean slate. This can be done using:
   history -c
Enter fullscreen mode Exit fullscreen mode
  1. Saving History for Documentation: When finishing a project, you may want to save your command history as part of your project’s documentation. This allows you to reference the commands you used later:
   history > project_history.txt
Enter fullscreen mode Exit fullscreen mode
  1. Analyzing Command Usage: For larger projects where you frequently repeat commands, it can be helpful to analyze your command usage. The following command generates a list of the most frequently used commands:
   history | awk '{CMD[$2]++} END {for (a in CMD) print CMD[a], a}' | sort -nr | head
Enter fullscreen mode Exit fullscreen mode
  1. Disabling History Temporarily: If you want to run sensitive commands without them being logged in your history file, you can temporarily disable the history feature:
   unset HISTFILE
Enter fullscreen mode Exit fullscreen mode
  1. Reverse Searching: If you need to quickly find a previously used command, you can press Ctrl + r to initiate a reverse search. Start typing, and the terminal will display matching commands from your history.

  2. Keyword Searches: Another way to locate a specific command is by using grep to filter your history. For example:

   history | grep <some string>
Enter fullscreen mode Exit fullscreen mode

These techniques can save time, improve efficiency, and even help with troubleshooting by providing a record of what’s been done. Whether you're clearing history, saving it, or analyzing it, the history command is a versatile tool for any terminal user.

Do you need to use your history?

Do you need to use the history command? Not necessarily. Using it doesn’t automatically make you a better Linux user, nor is it a command you’ll likely rely on daily. However, it’s one of those underrated tools that can save you significant time and effort when you do use it.

Personally, I’ve found it invaluable for documenting the steps I’ve taken to complete a task. It acts like a breadcrumb trail, helping me retrace my actions and ensure repeatability. Despite its usefulness, I think history is a command that doesn’t get the attention it deserves. It’s not flashy, but it’s highly practical.

What about you? What’s a terminal command you think deserves more recognition or should be used more often?

Top comments (0)