2

Is there a way to save all bash commands from all terminals to a single file? By default, different terminals have different history and that gets confusing. I'd like to save all history to a single file and optionally look up those commands instead? Perhaps shift+uparrow could look up in the history in the central file?

1
  • Naah, the OP will need the user added to each line of the history to tell who did what as well as when, and the suggested link neither shows how to accomplish that nor does it timestamp each line. – K7AAY May 31 '19 at 20:20
4

First, you must tell the shell to add the time and the username to each user's history and expand the size of each history file from its default of 500 lines with something like this command (set the value as you wish) entered at a shell prompt, with

echo 'HISTTIMEFORMAT="%F %T $USER"' >> ~/.bashrc && HISTSIZE=10000

and reboot after that so the change is activated. Why? Well, history without a date and time, and does not identify who entered a command may not be very useful.

Then, periodically copy the contents of ~/.bash_history for each user into a joint history file, and sort it, so the consolidated history will sort out in order of date, time, and user.

After you have done that, delete the source files in each user directory by an admin level script looping through

cd /home/userx && rm ~/.bash_history && history -c

changing x so as to perform that line for every user.

1
  • 1
    Why is timestamping necessary? – Jim L. May 31 '19 at 21:34

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