Linux operation example summary of the history command

  

Linux history command is used to view the used commands, often used in Linux system operations, is also a command that beginners must master, the following small series will give you Introduce an example of the history command in Linux, so that you have an understanding.

1. Use HISTTIMEFORMAT display timestamps

When you execute a command from the command line history, usually only show the number of executed commands and command itself. If you want to see the timestamp of the command history, you can execute:

The code is as follows:

# export HISTTIMEFORMAT=‘%F %T ’

# history |  More

1 2008-08-05 19:02:39 service network restart

2 2008-08-05 19:02:39 exit

3 2008-08- 05 19:02:39 id

4 2008-08-05 19:02:39 cat /etc/redhat-release

Note: This function can only be used when the environment variable HISTTIMEFORMAT After being set, the newly executed bash commands will be marked with the correct timestamp. All previous commands will be displayed as the time to set the HISTTIMEFORMAT variable.

2. Use Ctrl+R to search history

Ctrl+R is a shortcut I often use. This shortcut lets you search the history of commands and is useful when you want to repeat a command. When the command is found, it is usually executed by pressing the Enter key. If you want to adjust the found commands and then execute them, you can press the left or right arrow keys.

The code is as follows:

# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]

(reverse-i-search) `red‘: cat /etc/redhat-release

[Note: Press enter when you see your command, which will execute the command from the history]

# cat /etc/redhat- Release

Fedora release 9 (Sulphur)

3. Quickly repeat the previous command

There are 4 ways to repeat the previous command:

Use the up arrow key and press Enter to execute.

Press ! ! And press Enter to execute.

Enter ! -1 and press Enter to execute.

Press Ctrl+P and press Enter to execute.

4. Execute a specified command from the command history

In the following example, if you want to repeat the 4th command, you can execute it! 4:

The code is as follows:

# history |  More

1 service network restart

2 exit

3 id

4 cat /etc/redhat-release

# ! 4

cat /etc/redhat-release

Fedora release 9 (Sulphur)

5. Execute the previous command by specifying keywords

The following example, enter! Ps and press Enter, the command that starts with ps will be executed:

The code is as follows:

# ! Ps

ps aux |  Grep yp

root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind

root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp

6. Use HISTSIZE to control the total number of rows recorded by the history command < Br>

Append the following two lines to the .bash_profile file and log back in to the bash shell. The number of records in the command history will be changed to 450:

The code is as follows:

# vi ~ /.bash_profile

HISTSIZE=450

HISTFILESIZE=450

7. Changing the history file name using HISTFILE

By default, the command history is stored in ~ In the /.bash_history file. Add the following to the .bash_profile file and log back in to the bash shell, which will use .commandline_warrior to store the command history:

The code is as follows:

# vi ~/.bash_profile

HISTFILE =/root/.commandline_warrior
Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved