Skip to content

How to Merge All Linux Commands In The History Command?

  • by
merge all linux commands

I often have an incident when accessing a Linux server and using the history command to see what commands have been used, then there will be many differences in appearance. For more details, see the image below:

Display of different history commands between terminals

In the picture above, we can see that the user is running 2 different Linux commands on each terminal, which in total 6 Linux commands have been run on the Linux server. However, when using the history command, there are only 2 Linux commands stored in each terminal and this confuses me if I have to change terminals frequently because these Linux commands do not appear in the history command.

 

Problem

How to merge all Linux commands in the history command?

 

Solution

After I searched the internet, it turned out that to merge all Linux commands in the history command; I had to add the script below to the .bashrc file:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups

# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

 

After that, run the below command:

source /root/.bashrc

And then, close all terminals that are currently connected to the Linux server, then reconnect. After that, all Linux commands should appear if you type these commands in any terminal. 

 

Note

If, after following this method, there are still commands that are not saved when using the history command, then the server should be restarted and after the server restarts, it will record all Linux commands that have been executed in the history command. This method is very useful if we often change terminals so that it will record the commands that have been executed in the previous terminal in the history command in another terminal. You can also use this method besides using the root user by entering the script above into the .bashrc file that is on that user

 

References

unix.stackexchange.com
askubuntu.com

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *