Introduction:
The command line interface (CLI) is a powerful tool that allows users to interact with their computer systems using text-based commands. Among the various commands available, one particularly handy and often overlooked command is ‘history’. In this article, we will delve into the depths of the ‘history’ command, exploring its features, functionalities, and how it can enhance your command line experience. So fasten your seatbelts as we embark on a journey through time with the ‘history’ command.
Understanding the Basics:
At its core, the ‘history’ command is used to display a list of previously executed commands in chronological order. This invaluable feature allows users to recall and reissue commands, making it a lifesaver in cases where you need to repeat a complex or lengthy command without typing it all over again. By default, the ‘history’ command displays the command number followed by the command itself.
List Last/All Executed Commands in Linux:
Executing simple history commands from the terminal will show you a complete list of the last executed commands with line numbers.
[cloudchase] ~ % history
997 kubectl get pods -n jenkins
998 ssh 123.23.34.1
999 python3
1000 pip
1001 d ..
1002 cd ..
1003 ls
1004 git -v
1005 which git
1006 git clone https://github.com/cloduchase/jenkins-pipeline-shared.git
1007 cd jenkins-pipeline-shared
1008 pwd
1009 git remote -v
1010 git branch
1011 git checkout -b feature/cloudchase
1012 git branch
List All Commands with Date and Timestamp:
Set the ‘HISTTIMEFORMAT’ variable using the desired format. For example, to display the date and time in the “YYYY-MM-DD HH:MM:SS” format, type the following command
[cloudchase]$ export HISTTIMEFORMAT='%F %T '
1 2013-06-09 10:40:12 cat /etc/issue
2 2013-06-09 10:40:12 clear
List Specific User’s Executed Commands:
Bash keeps records of history in a ‘~/.bash_history’ file. We can view or open files to see the command history.
[cloudchase]$ vi .bash_history
cd /cloudchase/
cd kafka-1.0.3/
./configure
Delete or Clear the History of Commands:
With up and down arrow, we can see previously used commands which may be helpful or may irate you. Deleting or clearing all the entries from bash history list with ‘-c‘ options.
[cloudchase]$ history -c
Search Commands in History Using Grep Command:
Search the command through ‘.bash_history‘ by piping your history file into ‘grep‘ as below. For example, the below command will search and find ‘pwd‘ command from the history list.
[cloudchase]$ history | grep pwd
113 2013-06-09 10:40:12 pwd
141 2013-06-09 10:40:12 pwd
198 2013-06-09 15:46:23 history | grep pwd
Recall Last Executed Command:
Recall a previously used specific command. Combination of Bang and 8 (!8) commands will recall the number 8 command which you have executed.
[cloudchase]$ !8
Conclusion:
The ‘history‘ command is an indispensable tool for command line enthusiasts, developers, and system administrators alike. By harnessing its features and functionalities, you can streamline your workflow, save time, and become more efficient in your day-to-day tasks. From reissuing commands to advanced techniques like history expansion and command substitution, the ‘history’ command opens up a world of possibilities.