1

Trying to understand the functionality of bash_history:

/home/user# ls -a
.  ..  .bash_history  .bash_logout  .bashrc  .cache  .profile  pylabs  .viminfo  .Xauthority

I do see a .bash_history file in the /home/user folder. But why doesn't it have the entire history of the executed commands, output below ?

/home/user# cat .bash_history

su
/home/user#

But the same history is available in the .bash_history file in the /root folder.

 # ls -a
 .  ..  .bash_history  .bashrc  .cache  .profile  .ssh  .viminfo

/home/user# cd /root/
# cat .bash_history

bash script.sh
ls
cd pylabs/
ls
...
...
ls -a
cat .bash_history
cd /root/
#

Question 2: Will the .bash_history be available in some particular folders only, and how does it store all the commands executed in different directories?

/home/user/pylabs# ls -a
.  ..  backup  bkupscript.py  mybackup.py  __pycache__  solution.py
2

The history is per user, not global. Each user has their own history file, and each user's commands are stored in their own file. The default location for bash's history file is $HOME/.bash_history, so this will be /home/user/.bash_history for the user "user" and /root/.bash_history for the "root" user. You can change the name/location of the history file by setting the HISTFILE environmental variable to something else.

Now, in the example you show, you did the following:

  1. Logged in as "user" and immediately switched to root with su. This means that you ran exactly one command as "user" and that's why the history file of "user" only has one entry: su.

  2. As root, you ran various commands and those are all stored in root's history file since they were run by root. Note that root's history file doesn't contain the su command since that was run by "user" and not root.

So, the $HOME/.bash_history file stores all commands run by a user, irrespective of what directory they were run in. However, since each user has their own file, the commands run by root are not stored in the same file as those run by "user".

2

Bash has the stored history in the file you found, but shells that are running will also have a history in memory that has not been saved yet. Use history -w to write your current history out to the file.

1
  • And just like that I learned something logical, that I have pondered on when opening new terminals... why can't I get my latest history entries with me... – andersoyvind Feb 12 '19 at 14:27
0

On a separate note, one of my favorite history command hack is to enable

export HISTTIMEFORMAT=' %F %T '

If you execute history command now, you will see time stamp associated with each displayed history entry

history 

1525   2020-04-04 08:26:03 strace ls
1526   2020-04-04 08:26:08 strace history
1527   2020-04-04 08:27:55 strace ls 

Where

 %F     Equivalent to %Y-%m-%d (the ISO 8601 date format)
 %T     The time in 24-hour notation (%H:%M:%S)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.