Learn GNU/Linux Commands (12): History - history, clear

In Shell, commands that users input are stored in a file. For Bash, it is "~/.bash_history". In the terminal, users can get the previously input lines with the keyboard's up or down arrow keys. There are commands for access the input history as well.


View the Input History

history [N]

Display the last (latest) N user input line(s) in the history list. If N is omitted, the whole history list will be displayed.

For example:

[texpion@com ~]$ history 3
101  cd ~
102  ls
103  history 3


Reuse commands in the history list

As mentioned before, previously input lines can be recalled with the up or down arrow keys. We can also execute lines from the history list with commands.

!N

Execute the N-th line in the history list. In the last example, "!101" will run "cd ~".

!-N

Execute the last N-th line in the history list. In the last example, "!-2" will run "ls".

!!

Execute the last line in the history list. In the last example, "!!" will run "history 3".

Though the "! series" commands are not recommended because it can be dangerous if the number is wrong.


Search the History

In the terminal, we can press CTRL+R to search the history. After this, the prompt will change to:

(reverse-i-search)`':

Type the keywords and the current line will change to the latest match line. To search more previous lines, press CTRL+R again. To execute the line, press Enter or Return. To edit the line, press the left or right arrow key. To exit, press ESC or the up or down arrow key.


Delete the History

Delete a single line

history -d N

Delete (clear) all history

history -c


Clear the terminal

clear

option:

  • -x: do not try to clear scrollback.



Related

Comments

  1. I read your post and got it quite informative. I am impressed by the details that you have shared in this post and It reveals how nicely you understand this subject. Thanks for sharing this article here. How to create a file in Linux

    ReplyDelete

Post a Comment

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

OBS Studio settings for best quality (loseless) recording

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

Install VMAF on Fedora, CentOS/Red Hat (RHEL)