How to Access Command History on Linux
As a Linux user, you are likely familiar with the command line interface, which provides access to the operating system’s tools and utilities. While using the command line, you might want to revisit a command that you previously entered. Fortunately, Linux systems store command history, allowing you to recall and reuse commands easily.
In this article, we will discuss how to access command history on Linux.
First of all, it’s important to note that different Linux distributions and shells may store command history differently. However, most Linux systems use the Bash shell, which stores command history in a file called .bash_history, located in your home directory.
To access your command history, simply open a terminal window and type the following command:
“` bash
history
“`
This command will display a list of your most recent commands, along with a numeric index for each one. By default, the list will show the 500 most recent commands, but you can adjust this limit by setting the HISTSIZE variable in your shell environment.
If you want to execute a command from your history, you can use the ! symbol followed by the command’s index. For example, to rerun the command at index 42, type:
“` bash
!42
“`
Alternatively, you can use a partial string of the command to run it. For instance:
“` bash
!gr
“`
will re-run the latest command starting with the letters “gr”.
To clear your command history, you can use the following command:
“` bash
history -c
“`
This will delete all commands from your command history. Note that this command does not affect any currently running shells, so history data may still be available in open terminal windows.
You can also search your command history using an advanced search feature, the Ctrl+R shortcut. This will bring up a reverse-i-search prompt, allowing you to type a partial command or string to search. As you type, the terminal will display matching commands from your history. Once you find the command you’re looking for, simply press Enter to execute it.
In conclusion, accessing your command history on Linux is a simple task that can save you time and effort. By using a few simple commands and shortcuts, you can access, search and reuse previous commands, benefiting from strategies such as setting environment variables and using history search to improve your efficiency while working from the command line.