A very useful, but also indifferent to many, command of Linux, is also history. If you learn to use it with all parameters then you will significantly increase your productivity.
(Photo generated by free AI)
The linux shell stores a history of all the commands you have given it and executed in the past. It gives you the ability to call back saving you from an incredible amount of typing and making your life much easier.
Don't just ask for your last command with the up arrow. history, like almost all Linux commands, has a bunch of parameters that are worth learning about and using.
Table of Contents
Usefulness of history in Linux
In general, the history command allows you to review and redo your previous commands. The longer and more complex a command is, the harder it is to remember and re-type it without making a mistake.
Like most Linux commands, there's more to it than you might think. Learning how to use the history command can improve your Linux terminal experience. It is a good investment of your time. And trust us, there are much better ways to use the history command than just pressing the up arrow repeatedly.
Simple use of history
The history command in its simplest form is written by simply typing its name:history
You will immediately see a numbered and chronological list of previously used commands appear in the terminal window.
The commands are numbered, with the most recently used being the ones with the highest numbers and at the end of the list. If you want to not see the whole list but say for example only the last 10, then type the following:history 10
You can achieve the same result if you use the tail command together with the history command. To do this, type the following:history | tail -n 10
Repeated commands
If you want to reuse a command from the history list type an exclamation point (!) and the command number with no spaces in between.
For example, to repeat command number 18, you would type this command:!18
To repeat the last command, type two exclamation points, again, without spaces:!!
Although the last command can be found with one less click by just pressing the up arrow, the two exclamation marks are useful when you enter a command and forget to use sudo.
So if you type sudo, a space, and double exclamation marks and then press Enter you will get the last command with sudo, which the up arrow cannot.
Accordingly, you can type sudo and with a comma the number of the command from the history list.
You can also request the repetition of a command from the list with a negative sign which means it will start counting backwards. That is, 1 is the last, or 2 the penultimate, etc.
Just type an exclamation point ( ! ), a dash ( – ), and the command number, all without spaces.
Search commands via String
To repeat the last command that starts with a particular string, you can type an exclamation point, then the string without spaces, and then press Enter.
For example, to repeat the last command started with sudo, you would type this command:!sudo
However, there is an element of risk in this. If the last command sudo started with isn't what you think it is, you'll start the wrong command. To provide a safety net, you can use the :p (print) parameter, as shown below:!sudo:p
This tells the history command instead of executing the last sudo command, to simply print it to the terminal. This allows you to preview the command before you use it. If it's the command you want, press the up arrow, then press Enter to use it.
With a specific string
If you want to find a command that contains a specific string, you can use an exclamation mark and a question mark.
For example, to find and execute the first matching command containing the word “iguru”, you would type this command:!?iguru
This will find any command containing the string “iguru”, no matter where it appears in the string.
Interactive search
An interactive search allows you to go to a list of matching commands and repeat the one you want.
Just tap Ctrl + r to start the search.
Now type the search letter or symbol and the first matching command containing that symbol will be displayed. The letters you type appear between the backtick (`) and the apostrophe ('). The corresponding commands are updated as you type each letter.
Each time you press Ctrl+r, you search backwards for the next matching command, which is displayed in the terminal window.
When you press Enter, the displayed command will be executed. To edit a command before executing it, press either the left or right arrow. The command will appear in the command line and you can edit it.
Of course as with any command you can use grep to narrow down your search. For example history | grep iguru
Modification of the last command
If you need to correct a typo and then retry the command, you can use the (^) symbol to modify it. This is a great trick for quick results for whenever you misspell a command or want to run it again with a different option or parameter.
To use it, type (without spaces) the ^ symbol, then the text you want to replace, then a second ^ symbol, then the text you want to replace it with, then press Enter.
For example, suppose you type the following command, accidentally typing “shhd” instead of “sshd”:sudo apt updade
You could easily fix this by typing the following: ^updade^update^
The command is executed with “updade” corrected to “update”.
Delete commands from the history list
If you don't want to keep the misspelled command in your user history list, or you just don't want anyone else to see what commands you issued, then you can delete your commands from the history list with the -d (delete = delete) option.
Here you can also put grep into play to find it easily, and after seeing its number in the history, delete it by writing the command history, then -d and finally the number of the command. Example:
history | grep update
history -d 31
You can also pass a series of commands to the -d option. To delete all list entries from 22 to 32 (inclusive), type this command:history -d 22 32
To delete only the last five commands, you can type a negative number, such as:history -d -5
Manually updating the history file
When you log in or open a terminal session, the history list gets the data from the history file. In Bash, the default file is to .bash_history.
Any changes you make in the current terminal window session are written to the .bash_history file only when you close the terminal window or log out.
You should make out in your mind the list of the file. The list is cached and if your computer shuts down abruptly they will be lost without being archived. So, the list and the history file are different. Usually they coincide but when using the terminal they are not the same.
Let's say you want to open another terminal window and need access to the full list of history, including the commands you typed in the first terminal window. The -a (all = all) option allows you to save all your commands from the first terminal window before opening the second.
To use it, type the following:history -a
Commands are simply written silently to the history file.
If you want to write all changes from the history list to the .bash_history file (if you deleted some old commands, for example), you can use the -w (write = record) option, like so:history -w
Clear history list
To delete all commands from the history list you can use the -c (clear = delete) option, as follows:history -c
If you want to additionally force these changes to the history file, use the -w option, like so:history -w
The .bashrc file
The .bashrc file is executed every time you log in or open a terminal window. It also contains some values that control the behavior of the history command. Let's edit this file with nano or gedit (whichever you have).
Type the following: nano .bashrc
Near the top of the file, you see two entries:
HISTSIZE: The maximum number of entries that the history list can contain.
HISTFILESIZE: The limit for the number of lines a history file can contain.
These two values interact in the following ways:
When you log in or start a terminal window session, the history list is populated from the bash_history file.
When you close a terminal window, the maximum number of commands set to HISTSIZE is saved in the .bash_history file.
If the histappend option is enabled, commands are appended to .bash_history. If histappend is not set, then commands are replaced in to.bash_history.
After saving the commands from the history list to .bash_history, the history file is truncated to contain no more than HISTFILESIZE lines.
Also near the top of the file, you see an entry for the HISTCONTROL value.
You can set this value to do any of the following:
- ignorespaces:Lines starting with a space are not added to the history list.
- ignoredups:Duplicate commands are not added to the history file.
- ignoreboth:Enables both of the above.
Ignoring commands
You can also list specific commands that you don't want added to your history list. Separate them with colons (:) and enclose them in quotation marks (“…”).
To do this add the following lines below HISTFILESIZE (replacing the commands you want ignored):export HISTIGNORE="ls:history"
Use of timestamps
If you want to add timestamps to the history list, you can use the HISTIMEFORMAT setting. To do this, just add a line like the example below:export HISTTIMEFORMAT="%c "
Note that there is a space before the closing quotes. This prevents the timestamp from being stuck on commands in the command list.
Now when you run the history command you will see a date and time stamp.
Note that any commands that were in the history list before you added the timestamps will have the date and time of the first command to receive a timestamp.
%c will give you a very long timestamp. You can make it smaller if you use the following tokens instead of %c :
- %d: Day
- %m: Month
- %y: Year
- %H: Time
- %M: Minutes
- %S: Seconds
- %F: Full date (year-month-date format)
- %T: Full time (format hour : minutes : seconds)
- %c: Complete date and time (format day-month-year and hour : minutes : seconds)
For example you can say: export HISTTIMEFORMAT="%d %m %T "
and will only report day-month and full time
You can also use the history command to check. Sometimes, reviewing the commands you've used in the past can help you determine what might have caused a problem.
Just that the author of the article (or should I say translator better) refers to the Linux operating system in the plural, shows how irrelevant it is. Linux, Office, DOS. The ONE operating system. The ONE software.
The ONE core.
THE MANY “flavors” (see editions, distributions)