Bash history format

January 16th, 2010 by Leandro Morgado

Sometimes I need to find out when a certain command was executed in the bash shell. By default, the bash shell will give you this:

shell> history | tail -n 2
 1004  history
 1005  history | tail -n 2

This won’t tell you the date but rather just the order that they were run in. If you want to temporarily see the date, then you need to set this:

shell> export HISTTIMEFORMAT='%F %T '
shell> history | tail -n 2
 1006  2010-01-16 00:55:47  export HISTTIMEFORMAT='%F %T '
 1007  2010-01-16 00:55:49 history | tail -n 2

This will last as long as your environment variable is set, so if you log out you will need to set it again. The %F and %T are standard strftime strings. Check the man page for the full range of options. If you want to make this setting permanent system wide, they add it to your /etc/profile .

sudo -i vs sudo -s

December 28th, 2009 by Leandro Morgado

A long time ago, every time I needed to do some admin work on Linux I used to “su -” into a root shell. After moving from Debian to Ubuntu, I noticed that “su -” would not work as the root account was disabled. So I started fooling the system with:

shell> sudo su -

But that was more a hack than anything else. You don’t actually need to run su with sudo to get a root shell. You can either sudo right away with:

shell> sudo -i
shell> sudo -s

So what are the differences between these? Well, the -i switch gives you the root shell environment, working the same way as “su -“. The -s switch preserves your own environment, and is equivalent to just running “su” with the dash.

Obviously, I’m not the only one who has come across this question and Ubuntu docs have a full page on this.

Happy rooting! 🙂