How To Use the history Command in Linux

0

Linux is a command-based operating system that relies primarily on commands to execute tasks. During a terminal session, you run various commands; noting them is not feasible as it can be time-intensive. That’s why the history command is handy to view previously run commands in the terminal.

It helps you recall and reuse earlier commands and troubleshoot unexpected system behavior. So this short blog will briefly explain how to use the history command in Linux, including its usage, options, and some examples.

How To Use the history Command in Linux

You can run the below command to check the history of the previously executed commands:

history-command-in-linux How To Use the history Command in Linux How To Use the history Command in Linux 1713883372 524 How To Use the history Command in Linux

The above command, by default, shows a list of up to 1,000 commands. If you wish to view a specific number of commands, go for the below command:

 

Where N is the required number of preceding commands. For example, to see the last 3 commands executed, we will enter:

history-command-to-check-specific-commands How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 107 How To Use the history Command in Linuxhistory-command-to-check-specific-commands How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 107 How To Use the history Command in Linux

Please combine history with the grep command to search for a particular earlier command. For instance, to search for occurrences of cd:

history-command-with-grep-command-in-linux How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 173 How To Use the history Command in Linuxhistory-command-with-grep-command-in-linux How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 173 How To Use the history Command in Linux

If you want to reuse any previous commands, please check that command’s line number. For example, let’s reuse the cd ~/Documents available in the 9th row:

history-command-to-check-a-row How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 693 How To Use the history Command in Linuxhistory-command-to-check-a-row How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 693 How To Use the history Command in Linux

If you want to clear the history, then please use the -c option:

c-option-in-history-command How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 113 How To Use the history Command in Linuxc-option-in-history-command How To Use the history Command in Linux How To Use the history Command in Linux 1713883373 113 How To Use the history Command in Linux

Moreover, the ‘-d N’ option deletes a specific entry at N. For example, let’s delete the history from raw 200 to 275:

A Quick Wrap-up

The history command is a valuable tool in Linux’s range of commands. It allows you to recall and manage previously executed commands. This blog demonstrated how to use the history command with practical examples. Furthermore, we explained two primary options to manage the command history. 

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More