
{{ $('Map tags to IDs').item.json.title }}
How to Use history Command
The history
command in Linux is a built-in utility that allows users to view and manage their command line history. It’s a helpful tool for recalling previously executed commands and improving productivity when using the terminal. This tutorial will guide you on how to effectively use the history
command.
1. Viewing Command History
To view your command history, simply type:
history
This will display a list of all commands you’ve executed in the terminal during the current session, along with their corresponding line numbers.
2. Re-running Commands from History
If you want to re-run a command from your history, you can use the exclamation mark followed by the line number. For example:
!42
This will execute the command that appears at line number 42 in your history list.
3. Searching Command History
To search through your command history for specific commands, you can use:
history | grep 'search_term'
Replace 'search_term'
with the keyword you want to find. For instance:
history | grep 'git'
This will show only the commands related to Git that you’ve executed.
4. Clearing Command History
If you want to clear your command history, you can use:
history -c
This command clears the current session’s history. To clear history across sessions, delete the history file, typically found at ~/.bash_history
:
rm ~/.bash_history
Note: Clearing history can be irreversible, so ensure you want to delete it before doing so.
5. Saving Changes to Command History
By default, terminal sessions save command history when closed. However, if you want to force save your history after making changes, you can use:
history -w
This writes the current session’s history to the history file immediately.
6. Conclusion
By utilizing the history
command effectively, you can enhance your productivity on the command line by quickly accessing and reusing previous commands. Keeping track of your command history can significantly streamline your workflow and make terminal usage more efficient. Continue exploring other shell features to improve your command line skills!