Ubuntu – How to uniqely record every new command I use, and possibly timestamp it

bashbashrccommand line

I've been on Linux for more than 6 months now but never went too much into the CLI (command-line interface or terminal or shell)

Now as I ask questions here, get answers, or help from other sites, I learn new commands…

How can I can store every new command in a text file? Only new/unique commands, not repetitions of the same command.

Here's an example:

  1. In the terminal, I enter the commands like this-
$  command1
$  command2
$  command3
$  command4
$  command1
  1. Now, these commands should get saved in a text file say commandrec like this
command1
command2
command3
command4

NOTE: The last command in the terminal which was again command1 is not recorded/saved again in the text file.

And the next time I open the terminal, and enter a new command command 5, it should get appended to the list in commandrec

(but if the command was used earlier on some other date, it should still be ignored. For example, command 1 entered again along with command 5 on a new day/time but command1 not recorded as already used)

  1. The commandrec file will be looking something like this
31/05/12 12:00:00
command1
command2
command3
command4
01/06/12 13:00:00
command 5

(the time and date thing would be great if possible, but okay even if that isn't there)

This way, I can have a record of all commands used by me to date.

How can this be done?

Best Answer

How to successfully experiment with changing these settings in .bashrc

  • Before experimenting: Save your current .bash_history to another file with cp.
  • While experimenting with what works: rm ~/.bash_history after you change .bashrc with history control parameters, otherwise the combo of old and new entries may give you weird results.
    • e.g., all entries before timestamps were enabled will show the same current date/time!
  • Once you find something that works, DO NOT rm any more!!

  • Note also that any changes to .bashrc are reflected only when you exit and then instantiate a new terminal/shell.

Increasing history size and eliminating duplicates

  • Add these lines to your ~/.bashrc:

    export HISTCONTROL=erasedups
    export HISTSIZE=10000
  • History size is set to 10000, duplicates are automatically eliminated so it's plenty of space :)

Timestamping

  • Time can be set easily by adding another line of the form export HISTTIMEFORMAT="%F %T", this will display the time/date before each line when using history
    • Example:
      izx@preciseunity:~$ history
      1  2012-06-01 07:16:22 rm ~/.bash_history
      2  2012-06-01 07:16:23 exit
      3  2012-06-01 07:16:27 ls
      4  2012-06-01 07:16:31 test
      5  2012-06-01 07:16:34 ls /etc
      6  2012-06-01 07:16:36 down
      7  2012-06-01 07:16:37 up
      8  2012-06-01 07:16:40 hustory
      9  2012-06-01 07:16:48 history
      
    • Note that because duplicates are eliminated, the time shown for a command is the latest (newest) time it was last executed.
    • You can customize the HISTTIMEFORMAT to your choice/locale based on strftime