Ubuntu Tip:How to Add Date And Time To Your Bash History

Sponsored Link
This is very useful tip if there are multiple people maintaining or using ubuntu desktop,server or even if you are the maintainer, but can't remember exactly when you did or changed something.

If you want to add this settings to globally use /etc/bash.bashrc

First you need to Edit your $HOME/.bashrc file

gedit $HOME/.bashrc

Add the following line

export HISTTIMEFORMAT="%h/%d - %H:%M:%S "

Save and exit the file.

From next login instead of:

574  tail -f /var/log/maillog
575  mailq | tail -15
576  tail -f /var/log/maillog
577  less /var/log/maillog

you get:

1002  May/09 -- 11:46:16 grep log /var/log/maillog
1003  Apr/09 -- 14:17:40 passwd test
1004  Apr/09 -- 14:50:28 history 15

Some more bash history tips

  • The most efficent way to search your history is to hit Ctrl R and type the start of the command. It will autocomplete as soon as there’s a match to a history entry, then you just hit enter
  • If you don't want to save duplicate commands use the following option in your bashrc file ($HOME/.bashrc)

export HISTCONTROL=ignoreboth

  • If you want to set the size of the history file use the following option in your bashrc file ($HOME/.bashrc)

export HISTSIZE=500

Sponsored Link

You may also like...

8 Responses

  1. x33a says:

    Really useful tips, thanks.

    You forgot to add the history command. it displays the last 500 (or what you have set) commands typed.

  2. Andrew says:

    Didn’t work for me (ubuntu 8.04), got this when I logged in again:
    -bash: export: `-‘: not a valid identifier
    -bash: export: `%H:%M:%S’: not a valid identifier
    -bash: export: `“’: not a valid identifier

    Also I used “export” before all those commands, ie I’m pretty sure “HISTSIZE=500” should be “export HISTSIZE=500”.

    I like your blog. Enjoy getting a new ubuntu thing every couple of days… Thanks.

  3. admin says:

    @Andre
    I have updated article with export command

  4. Andrew says:

    Any idea why I’m getting those ‘not a valid identifier’ errors?

  5. Andrew says:

    Oh, I’ve fixed it. You should change it to single quotes:
    export HISTTIMEFORMAT=’%h/%d – %H:%M:%S ‘

  6. karlzt says:

    thanks for the tip

    love this site

  7. Brad says:

    Any idea how to setup a new user so that hitting tab will complete a word, up will show the last history, and on top of that the prompt will say user@machine: rather than just $:

    The initial user you create during setup has these settings but new ones do not and it drives me crazy. I tried copying the whole bashrc over and no luck.

  8. John24 says:

    I fit into the first category. ,

Leave a Reply

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