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!
- Fix: grunt: command not found (Mac/Windows)
- AppleScript Example with TextEdit
- How to copy Password of Wifi on your Mac
- Install OpenJDK Java (8/11/17) versions using brew on Mac (Intel/M1/M2)
- How to check if a port is in use using terminal [Linux or macOS]
- Cut, Copy and Paste Keyboard Shortcuts on Mac Keyboard
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required.
- How to Remove Widgets from macOS Sonoma 14 Desktop
- Turn off Auto-Capitalization on macOS Ventura 13
- How to Go To /usr/local/bin on Mac Terminal?
- How to Open TextEdit on Mac
- Mac: How to Open Android Studio from Terminal
- How to change the default location of Mac Screenshot
- Display Seconds in Digital Clock on Mac Menu Bar [macOS]
- How to install homebrew (brew) on M1 Mac
- Must Know Homebrew Commands for Mac/Linux Users
- Steps to Uninstall Roblox on Mac (macOS)
- How to change the System Settings Sidebar icon size Mac Ventura 13
- How to set Out Of Office (OOO) on Outlook for Mac [Updated 2023]
- Trigger Notification Center Message using Mac Terminal Command
- [fix] macOS Ventura - Python3 xcrun: error: invalid active developer path missing xcrun at CommandLineTools
- How to Install Rust Language on Mac (macOS)
- How to minimize all Windows in Mac
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS]
- How to make TextEdit the default text Editor on Mac
- Change Google Chrome Browsers default download location - Chrome
- [Nopepad++] How to add text at end of each line - NotepadPlusPlus
- Android Studio : Connection Error : Failed to download patch file - Android-Studio
- How to Delete a Sheet in Excel on Mac - Microsoft
- Remove Apps from Dock when Quit [Mac Ventura] - MacOS
- Create a Zip file using Java Code programmatically - Java
- Fetch as Google Crawl Error or Redirected Status - Google
- AWS S3 CLI BucketAlreadyExists when calling CreateBucket Error make_bucket failed - AWS