This happens to most of us that we used some command in the Terminal in the past and forgot about it, some ssh command or a grep with a regex, or a curl command that you want to reuse but do not recall it, in such case you can use a very powerful command called as history,
Whenever you are working on a shell (say a terminal on macOS, or any other CLI), it maintains a record of commands or operations that you had executed during the current session. Using the history command you can works with this list of maintained records and search and manipulate it.
When you fire a history command (with or without arguments) this list is printed out in the console/terminal.
Syntax:
history [-c] [-d offset] [n] or
history -anrw [filename] or
history -ps arg [arg...]
History Command Examples:
This will print out all the previously typed command in sequence with the mostly newest command at the last.
1. With no arguments$ history
1 printf '%s\n' ${PATH//:/\/* }
2 vi ?
3 vi asf
4 ssh 192.168.1.111
5 help
6 vi help
7 vi abc
8 vi abc
9 vim abc.txt
10 grep '%a'
11 ls -ltr
12 clear
1 grep '%chicago%'
1 grep '%linux%'
.
.
.
564 help history
2. With with number [n] argument: history [n]
As you would have seen in the example 1, we get all the previous history list when you simply use history, if you want to restric the results you can make use of a numeric value post the command,
$ history 10
505 bash
506 ls -ltrh
507 grep '%sweden%'
508 grep '%txt%'
509 curl http://code2care.org
510 ls -ltr
511 ps -ef
512 clear
513 pwd
514 history 10
3. Search history with grep command:
Now this is the most most interesting stuff, you can combine the grep command with history command to find specific key words you are looking for in the command search.
$ history | grep curl
27 curl https://code2care.org
29 history | grep curl
⛔️ If you want to delete all historical command list you can make use of history -c.
⚡️ Did you know?: In initial versions of Unix based Operating Systems the history command was available as a separate program. Which is built-in now-a-days, so the separate history program is not used anymore (mostly!)
Have Questions? Post them here!
- Clear Screen shortcut macOS Terminal
- What is macOS Ventura?
- [fix] How to Show file extensions on all files on Mac
- How to Find Where Mac Terminal App is Located?
- AutoSave button not working on Office on Mac (Word, Excel or Powerpoint)
- macOS say command text to speech using various voices and languages
- How to change Ping TTL value on macOS
- Open .bash_profile File in TextEdit using Terminal
- How to know the Safari Version on Mac
- How to Copy full Absolute Path of a File on Mac
- 13.0 MacOS Ventura release date
- Enable spell check in Sublime Text (macOS)
- How to Display Analog Clock on Mac Menu Bar
- How to turn off Location Services macOS Ventura 13
- How to Open VS Code on Mac
- Add Bookmark macOS Safari
- How to Fix cd: too many arguments Error in Terminal: A Step-by-Step Guide
- How to install wget on macOS
- Select Line Number TextEdit on Mac
- Strikethrough Text in Excel for Mac
- How to Enable or Disable Dark Mode on macOS Ventura 13
- How to hide or display Wifi icon in macOS Bug Sur Menu Bar
- How to install Yarn on Mac (macOS)
- How to fix command not found brew (bash, zsh) on macOS Terminal
- How to Schedule Mails in macOS Ventura
- Ways to Convert Integer or int to Long in Java - Java
- StringJoiner Example with Java Collections - Java
- How to install Python 2.7.xx on macOS 12.3 Monterey or higher - Python
- Drop table using Java JDBC Template - Java
- Iterate over an Array using Java 8 Stream and foreach - Java
- Fahrenheit to Celsius Temperature Convertor: Tool & Formula - Tools
- How to update all installed packages at once in Ubuntu - Ubuntu
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2) - Android-Studio