Table of Contents

6 tips to use history command in Linux

The Linux history command is a powerful tool that can help you track your activity on the terminal. It can also 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 Linux

The history command in Linux is used to view a list of commands that have been previously executed. It can also be used to retrieve commands that were executed at a specific point in time.

To view the history of commands that have been executed, simply type “history” into the terminal. This will print a list of all commands that have been stored in the history file. By default, this file is located at ~/.bash_history.

Know 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 Linux

The HISTTIMEFORMAT environment variable can be used to control the 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.

The default format is “%d/%m/%y %T”. However, you can change this format by typing export HISTTIMEFORMAT= “%F %T” into the terminal.

  • %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
  • %T – Time

view commands executed at a specific time in Linux

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 Linux

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 Linux

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.

  • HISTFILE—/home/<username>/.bash_history
  • HISTFILESIZE—1000
  • HISTSIZE—1000
  • 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 in the history file.

Once you outreach 1000 Linux commands, the oldest commands will be abandoned as new ones are saved.

how to view history file?

To view your Bash history file, you can use the cat command. For example, if you want to view the contents of your ~/.bash_history file, you would type “cat ~/.bash_history” into the terminal.

how to delete one command from history command in Linux

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!

You might also like