Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

Learn about the history command in Linux

Last Modified : 04 Mar, 2023 / Created : 04 Mar, 2023
639
View Count
Let's understand the Linux history command. How and when can we use it?




# Linux history command how to use
The "history" command in Linux is one of the most commonly used commands. When you type "history," it displays a list of recently used commands along with their corresponding numbers.
> history
...
177 git status
178 git pull
179 cd ~/.ssh
180 ll
181 exit
182 history

You can easily check the previously entered command line history as shown above.


! Learn about available options for history


The "history" command can have several useful options set. By default, history will display the last 500 (or 1000) entries entered. Here are some frequently used options:


1. If you want to view by page:
- Starting from the first page, press the space bar to move one page at a time.
history | less

2. If you want to see only the last 10 commands
- Simply output the recent 10 commands.
hisotry | tail

3. If you want to show only a certain number of items
- The following displays the last 5, 15, and 25 items respectively.
history 5
history 15
history 25

4. Using grep with history
- This command is commonly used with pipelines and the grep format. For example, to only view a list containing "git", one can input the command as follows:
history | grep git

Additional) Using pipelines consecutively.
You can use pipelines consecutively as shown below. If you use the keywords and tail simultaneously, it will be as follows.
history | grep git | tail

You can find commands using git, but you can only see the latest 10.


! Other useful history tips and information


Here are some tips or information that can be useful when used or utilized together.

1. How to run the desired line again.
When using history, using the exclamation mark symbol allows the user to execute that command again, which is very convenient. For example, to execute the 100th line again, use it as follows.
!100

2. Show and output the last command.
This is a way to execute the last command by showing it again. It uses two exclamation marks (!!).
!!

3. If you want to see the command again, print it.
If you want to output the 100th command, use "p".
!100:p



! Delete the history of history.


The following is the command to delete the history contents.

1. If you only want to delete one, use the option -d.
history -d 100
// 100번째 history만 삭제됨

2. To delete everything, use the -c option.
To delete all the contents of the history, follow the steps below.
history -c
// 모든 history 내용을 삭제


We have briefly looked at the Linux history command up to this point.

Previous

Learning about the make command in Linux

Previous

Setting Up a New Service in Linux's systemctl