+5 votes
275 views
History Linux command Examples terminal

in Linux / Unix by (551k points)
reopened | 275 views

1 Answer

+3 votes
Best answer

1. View or list command history in Linux
2. Search for commands in Linux with History
3. Change a command executed in Linux
4. Clear the Linux command history
5. View a specific number of Linux command history line
6. Repeat a command that starts with a string in Linux
7. Edit History file in Linux
8. Execute a single command without logging in
9. Execute all commands without leaving a Linux log
10. Ignore specific commands in Linux with History
11. Increase the size of the history file in Linux
12. Add timestamps to the History command on Linux
13. Edit the location of the history file in Linux

One of the most ideal ways to simplify work on Linux computers is to have the ability to execute orders almost autonomously. This is possible with a special command of the Linux distributions called "History" or history, which, as we can already suspect, is responsible for saving commands to the Linux file that we executed in the terminal session. This gives us the possibility to resort to these commands again without having to enter them manually..

It is a very useful command that we can combine with certain parameters to execute certain orders again or to keep a detailed log of Linux commands of each command executed in Linux and TechnoWikis will explain how to use it effectively.

Next, we will see a compilation of different utilities that we can carry out with the History command in Linux..


1. View or list command history in Linux

Step 1

It is the basic way to use this command since to see all the commands in the session by executing the following in the terminal. We can see that each command that has been executed is listed with an identifier.
 History 
image
Step 2

To execute any of these commands, we will use the sign! followed by the appropriate number, for example:
 !one 
image

2. Search for commands in Linux with History


The History command is versatile, it allows us to execute the last command by entering the signs !!, in turn this can be combined with the grep command in order to find commands matching a text pattern.
Step 1

For example, we will look for all the commands that have used the term “update”. As we see, each one comes out with its respective identifier.
 History | grep Update 
image
Step 2

An alternative is to use the key combination Ctrl-R in order to access a recursive search in the command history, when using this the terminal will have the following text. Then we can enter the desired search criteria.
 (reverse-i-search) ``: 
image

3. Change a command executed in Linux


The History command allows us to execute a command using a different syntax, for example, we can do the following. This will change the search from update to SSH.
 ^ update ^ ssh ^ 

4. Clear the Linux command history


Undoubtedly, when we use hundreds of commands, the history will display a significant number of lines and many of these commands will no longer be necessary, a good advice from TechnoWikis is to clear the history of either a particular line or in its entirety, Options for this are as follows:
  • Delete a specific line: History -d <line number>
  • Clear all history: History -c
image

5. View a specific number of Linux command history line


We may only want to see a certain number of commands executed, for example, the last 6, for this we will enter the following:
 History 6 
image

6. Repeat a command that starts with a string in Linux


We see that to execute the last command with History we enter !! While to repeat a specific command we enter! # of line, this is useful but we can also execute commands that start with a string, this is achieved with the syntax! string:
 sudo 
image

7. Edit History file in Linux


By default, the History command file is written at the time of closing the session, this is a natural thing worse if we handle SSH sessions where the waiting time is exceeded, the history of that session will simply disappear, this can be avoided by writing directly to the bash_History file using the -w parameter:
 History -w 
image

8. Execute a single command without logging in


This is an option that gives us the possibility to execute a command without it being stored in the bash history, for example:
 echo "TechnoWikis"; History -d $ (History 1) 
image

9. Execute all commands without leaving a Linux log


It is possible that we disable the variable of the History command with which the record of the executed commands is saved, this will make everything we do not be recorded, for this we execute the following:
 echo $ HISTFILE unset HISTFILE 
image

10. Ignore specific commands in Linux with History


It will be possible to indicate a list of commands which will not be recorded in the history file, for this we will use the $ HISTIGNORE variable, this variable is not configured by default, there we simply enter the following:
 echo 'export HISTIGNORE = "ls: cd"' >> ~ / .bashrc 

11. Increase the size of the history file in Linux

Step 1

The History command makes use of the $ HISTSIZE and $ HISTFILESIZE variables to register the commands with a default capacity of 1000 and 2000 commands respectively: image
Step 2

These values ​​are stored in the / etc / profile directory and their value can be edited with the new data at the end of the line ~ / .bashrc. These changes will be effective at the next login. image

12. Add timestamps to the History command on Linux

Step 1

When we use the History command we only see the command executed but no date and time details, with the $ HISTTIMEFORMA variable we can add these values:
 echo 'export HISTTIMEFORMAT = "% c"' >> ~ / .bashrc 
Step 2

After this we close session and using History we will see the following:

image


13. Edit the location of the history file in Linux


For administration reasons we may want to change the default path of the History file which is ~ / .bash_History, for this we will use the $ HISTFILE variable like this:
 echo $ HISTFILE your - user 
With the use of History we have total control of each and every one of the commands we execute in Linux.

by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (551k points) | 309 views
+5 votes
1 answer
asked Feb 22, 2020 in Linux / Unix by backtothefuture (551k points) | 333 views
+4 votes
1 answer
asked Feb 18, 2020 in Linux / Unix by backtothefuture (551k points) | 289 views
+3 votes
1 answer
asked Dec 6, 2019 in Linux / Unix by backtothefuture (551k points) | 248 views
+5 votes
1 answer
asked Oct 4, 2019 in Linux / Unix by backtothefuture (551k points) | 228 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,632 questions
10,764 answers
510 comments
3 users