Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of history's command

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Remember history and realize China. In Linux operating system, it is also very important to query the commands operated by the administrator, and mastering the call history can improve the efficiency of the administrator management system.

The number of commands cached in history is determined by a variable named HISTSIZE in the/etc/profile file. You can check the current number by env command, or you can determine how many commands are cached by modifying the HISTSIZE value. It is recommended to modify the.bash_profile added to your home directory, not to modify global variables. All of the following variables are added to my home directory.bash_profile. If necessary, please modify them in/etc/profile.

common options

n List the current n commands

-a Clear all commands from cache

-c Clears cached commands in the current shell

-r: Read the contents of the historyfiles to the current shell history memory

-w: Write the current history memory into history files. Linux When you type and execute commands in the shell(console), the shell automatically records your commands in a history list, usually saved in the.bash_history file in the user directory. The default is 1000 entries, but you can change this value. If you type history, history will show you the first 1000 historical commands you have used, numbered them, and you will see a numbered list quickly scrolling across the screen. You probably don't have to look at all the items in the 1000 commands, but you can add numbers to list the last n commands. The history command in linux doesn't just let us query history commands. We can also use related functions to help us execute orders. Running a specific history command history lists all history commands saved by bash and numbers them. We can run a specific history command using an exclamation point sequence.

Common usage:

! NUMBER Execution of Article NUMBER Order

!! crtl+p is the last command executed.

! COMMAND e.g ! c Execute the most recent command that begins with c, remember that it is the most recent command that begins with C from the back to the front. Common Cases

1. Check the time when the history command is used

Because history lists a lot of commands, we can send the history command to the less command through the pipe command for easy viewing, e.g. history| less ;

At the same time, we can set the history environment variable HISTTIMEFORMAT to display the execution time of each command, so that we can easily see and understand when the current system has executed what command.

export HISTTIMEFORMAT='%F %T '

If you want the history command to display time permanently, append the above command to the current user's home directory under.bash_profile.

Ctrl + R Search History

I'm pretty sure this is the history feature you use most often. When you execute a fairly long command sequence, you can simply search the history command for keywords and re-execute the command without typing the entire command again. To do this, press Ctrl +R and enter the keyword. In the following example, I search for "sys" and the command "cat/etc/system-release" with "sys" in the previous command is displayed.

[Note: Press Ctrl+R at the command prompt and the terminal will display the following prompt ―reverse-i-search ‡]

(reverse-i-search)`sys`: cat/etc/system-release

[Note: When you see the command you want, press the Enter key to execute the command again.]

cat /etc/system-release

Sometimes you need to edit a history command before executing it. For example, you can search for "sys" as follows, the terminal displays the history command "cat /etc/system-release", select it change "system" to "centos" and execute it

[Note: Pressing Ctrl+R at the command prompt will display the prompt <$reverse-i-search <$]

(reverse-i-search)`sys`:cat /etc/system-release

[Note: When you see the command you want, press the left or right button to edit it before executing it.]

cat /etc/centos-release

3. Use HISTFILE to change the historical file name

By default, command history is stored in the.bash_history file. Add the following line to the.bash_profile file, log back into the shell, and the.commandline_warrior file will be used to store command history instead of the.bash_history file. You can use this command to track commands executed on different terminals by simply storing commands executed on different terminals in different history files. This way, we can know who is causing the system problem.

vim ~/.bash_profile

HISTFILE=/root/.commandline_warrior

4. Use HISTCONTROL to eliminate consecutive duplicate entries in the command history;

cat /etc/profile

if [ "$HISTCONTROL" = "ignorespace" ] ; then

export HISTCONTROL=ignoreboth

else

export HISTCONTROL=ignoredups

fi

This HISTCONTROL variable is to control whether to display consecutive duplicate entries. If you type ls three times in a row, these three ls are called consecutive repeat commands; if you type ls once, cd once, and ls again, this is not called consecutive repeat commands;

ignoreups is ignoreups to eliminate consecutive duplicate commands, only to eliminate consecutive duplicate entries; to eliminate duplicate entries in the entire command history, you can set HISTCONTROL to eraseups.

ignoespce is a command that starts with a space and is not displayed in history. e.g [spcace] ls , if you go to history, you will not find ls, but we can see that this command is executed.

Ignore both is the result of both options.

Pass the parameters of the previous command to the current command.

!!:$ Pass arguments from previous command to current command

e.g cat /etc/profile

vim !!:$ /etc/profile file.

!^ Pass the first argument from the previous command to the current command

cp /etc/profile /tmp

vim !^ The file/etc/profile is opened.

6. Replace specific parameters of specific commands

In the following example! cp:2 Finds the command history that begins with cp and takes its second argument as the argument to the current command (ls -l).

cp /etc/profile /tmp

ls -l ! cp:2

ls -l /tmp

In the example below "! cp:$"Look for the previous command that started with cp and replace the last argument (again, the second argument in this case) of that command with"ls -l."

ls -l ! cp:$

ls -l /tmp

Similarly, ls -l ! cp:^is a command that starts with cp and replaces the first argument of the command with "ls -l"

ls -l ! cp:^

ls-l /etc/profile

7. Use HISTIGNORE to make history ignore certain instructions when storing

Sometimes you don't want to see basic commands such as "pwd","ls" in the record, you can ignore them with HISTIGNORE.

Note that adding "ls" to HISTIGNORE ignores only "ls" and not "ls -l." Be sure to write out exactly what instructions to ignore.

#export HISTIGNORE="pwd:ls:"

Similarly, this command can be appended to the.bash_profile in the home directory.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report