The history command is a powerful tool that can help you track your activity on the terminal. It can be used to retrieve commands that you have previously executed. In this blog post, we will discuss 6 tips that will help you use the history command more effectively!
what is the history command in Ubuntu
The history command in Ubuntu is used to view a list of commands that have been previously executed.
Open the terminal and type “history” then press enter. This will print a list of all commands that have been stored in the history file along with their corresponding line numbers.
1985 ls -lt howtouseubuntu 1986 ls -l howtouseubuntu 1987 ls -l howtouseubuntu 1988 man ls 1989 cat /proc/loadavg 1990 w 1991 uptime 1992 last reboot
Where your history commands are located
By default, the history commands are located at ~/.bash_history. You can view the contents of this file by typing “cat ~/.bash_history” into the terminal.
If you want to change the location of this file, you can use the HISTFILE environment variable.
For example, if you wanted to store your history in a file called “~/.my_history”, you would type “export HISTFILE=~/.my_history” into the terminal.
Time format of history command in Ubuntu
The history command won’t display what time the commands were executed by default.
The HISTTIMEFORMAT environment variable can be used to control the time formatting of the history command. This variable allows you to specify the format of the date and time that is displayed when the history command is executed. You can use echo $HISTTIMEFORMAT to get your current time format.
You can change this format by typing the following command into the terminal.
export HISTTIMEFORMAT="%F %T "
- %F –> shows Date in the format ‘YYYY-M-D’ (Year-Month-Day)
- %T –> shows Time in the format ‘HH:MM:S’ (Hour:Minute:Seconds)
- %d – Day
- %m – Month
- %y – Year
Note that you need to enclose the date format in double quotes and add a space at the end of the format string. The space ensures that there is a space between the timestamp and the command in the history output.
Your configuration is only valid for the current session. When you exit the current session, this configuration will be gone.
To make the change permanent, we need to add the export command to the shell configuration file.
echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bash_profile
Then use the source command to load HISTTIMEFORMAT from the file into the current command prompt:
Here is one example after our change.
493 2023-03-18 06:47:27 exit 494 2023-03-18 06:47:27 whereis ansible 495 2023-03-18 06:47:27 ansible 496 2023-03-18 06:47:27 python 497 2023-03-18 06:47:27 pyhton3 498 2023-03-18 06:47:27 python3 499 2023-03-18 06:47:27 apt info ansible 500 2023-03-18 06:47:27 rpm 501 2023-03-18 06:47:27 man apt
view commands executed at a specific time in Ubuntu
If you want to view the history of commands that were executed at a specific time, you can combine the grep command.
For example, if you want to view the history of commands that were executed on May 12th, you would type “history |grep 05-12”.
view a specific number of commands in Ubuntu
To use the history command, you simply type “history” into the terminal. This will print a list of all the commands that have been entered into the terminal.
If you want to view a specific number of commands, you can type “history n”, where n is the number of commands you want to view. If you only want to view the last 100 commands that were executed, you would type “history 100”.
Use grep to find specific commands
The grep command is a powerful tool that can be used to search for specific patterns in files.You can use grep to search through your history file for a particular command.
For example, if you wanted to find all instances of the “ls” command in your history, you would type “grep ls ~/.bash_history”.This will print a list of all the lines in your history file that contain the “ls” command.
How to Increase Bash History Size in Ubuntu
Another possible difficulty with the history command is that it can take up a great deal of disk space if you do not have much disk space for your personal files. To prevent this from happening, you need to use three environment variables and they are HISTFILE, HISTFILESIZE, and HISTSIZE.
- The HISTFILE variable specifies the name and location of your Bash history .bash_history file.
- The HISTFILESIZE specifies the maximum number of commands that can be stored in a history file..
- The HISTSIZE variable specifies how many cached commands a shell session should store.
Once you outreach 1000 Ubuntu commands, the oldest commands will be abandoned as new ones are saved.
Let’s see one example. If HISTFILESIZE =2000, HISTSIZE=1000, you will get 1000 commands on the terminal when you run the history command but there are 2000 commands in the history file.
how to delete one command from history command in Ubuntu
To delete one command from your Bash history, you can use the history -d command. For example, if you want to delete the “ls” command from your history, you would get the line number of ls command and type “history -d line number” into the terminal. You can also use vi to delete the line that you want to delete from ~/.bash_history file.
To clear the entire contents of the history file, execute history -c.
We hope you found these tips helpful! If you have any questions, feel free to leave a comment below. Thanks for reading!