0

Is is possible to have the history of a specific user in one more file other than the default file mentioned as HISTFILE? I would like to have as backup file if main file was removed and let it be like a backup for that one.

Regards, Sriharsha Kalluru.

Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27

4 Answers4

2

You can create a hardlink to the file

cp --link --verbose /home/$USER/.bash_history /somewhere/else/users_history

When the original file in his home is removed the file is still there and preserves the content from being lost.

Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
  • Note that while a hardlink would preserve the file contents only if one of the names were deleted. If a process overwrote or otherwise corrupted the file, it would also impact the linked name. – bigendian Nov 10 '11 at 06:16
  • @bigendian: that is correct. I only suggested this solution because it perfectly matches to the OP's scenario of the file being removed. Thanks for the clarification! – Daniel Böhmer Nov 10 '11 at 10:00
1

Many times I've found myself using Ctrl-R in Bash to get a old command four times the terminal width, just to find out that too many days have passed and it's no longer in the .bash_history file. Here are two lines that will keep track of every command line you type at the bash prompt and use no external processes at all, just plain bash.

My first approach to this problem was increasing the maximum number of lines in the history to a very large quantity. But no matter how large it was, there was always a moment when I needed a long command I typed many months ago and it had already left the history. The current solution came to my mind when I learned about the PROMPT_COMMAND variable, a command that bash executes before showing each prompt. Here are the two lines:

export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
           "$(history 1)" >> ~/.bash_eternal_history'

One goal I set to myself was to achieve it without using any external process, so bash wouldn't have to fork a new process after every ENTER pressed at its prompt. The first line sets the format of history lines to include the date as a Unix timestamp, so you can now when you typed every command. The second line, which is the core of the solution, first ensures that, if a previous PROMPT_COMMAND was set, it gets executed before our stuff and then appends a line of the format:

PID USER INDEX TIMESTAMP COMMAND

to a file called .bash_eternal_history in the current user home.

Adding the username, which at first seemed unnecesary, became useful later to distiguish between "sudo -s" sessions and normal sessions which retain the same value for "~/", and so append lines to the same .bash_eternal_history file.

I hope some of you find these two lines as useful as I do. :-)

bilash.saha
  • 7,226
  • 2
  • 35
  • 40
  • Hi, Thanks for the information and this is the exact one which I require, But if we enter a empty command the previous command will store in the eternal file. So please let me know if there is any way to over come this one. – Sriharsha Kalluru Dec 07 '11 at 05:41
0

Would hard links solve your problem?

Also you can read the man page here.

  • I don't think answering a question with another question would be actually very useful. Neither the RTFM anti-answer. – heltonbiker Oct 20 '11 at 17:21
0

i would write a cronjob that copies the original histfiel to a backuplocation every minute or so. the you don't have to worry about defining a second histfile. otherwise you could write every command the user enters to a alternate file for this approach take a look here:

bash, run some command before or after every command entered from console

Community
  • 1
  • 1
Xtroce
  • 1,749
  • 3
  • 19
  • 43