Is there a way to have in the .bash_history file, listed: the directory where the command has been typed, the date, and the command ?

up vote 3 down vote accepted

History command showing the directory: NO! :(

History command showing the the date: YES! :)

That's because (from man history):

The history list is an array of history entries.  A  history  entry is
declared as follows:

   typedef void * histdata_t;

   typedef struct _hist_entry {
     char *line;
     char *timestamp;
     histdata_t data;

So, nothing about the directory where the command has been typed.

To know the exact time certain command was executed, see help history:

If the $HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry.  No time stamps are printed otherwise.

So all you have to do is to set $HISTTIMEFORMAT something like this in the cusrrent shell:


To have it permanently set, run the following command:

echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc

The above command will add a new line (export HISTTIMEFORMAT="%F %T ") at the end of your ~/.bashrc file.

Now, the output of history will look something like this:

 1613  2013-11-13 13:00:15 cat .bash_history
 1614  2013-11-13 13:01:04 man history
 1615  2013-11-13 13:11:58 help history
 1616  2013-11-13 13:19:07 ls
 1617  2013-11-13 13:19:09 cd
 1618  2013-11-13 13:19:15 history
  • ok.. but I really really need a way to have the directory where the command has been typed. Is there a way to do that? – Apahidean Iancu Nov 13 '13 at 11:43
  • I added this: export HISTTIMEFORMAT="%F %T " in the .bashrc, and it doesn't work – Apahidean Iancu Nov 13 '13 at 12:01
  • @ApahideanIancu First reopen your terminal or source ~/.bashrc, then you should run history command to see the result. – Radu Rădeanu Nov 13 '13 at 12:27
  • @ApahideanIancu Also, the history commands will all be merged and written to the common ~/.bash_history file once you exit from the terminal. The times will be saved in ~/.bash_history in unix time format (each line where the time is saved will look something like: #1384343470). – Radu Rădeanu Nov 13 '13 at 12:42

You CAN write the current working directory in a history file as well, but you have to make an own history file:

Write your .bashrc as follows:

export CUSTOM_HISTFILE="/tmp/bash_history" #path of the new history file
export PROMPT_COMMAND="history -a; history -c; history -r; date | xargs echo -n >>$CUSTOM_HISTFILE; echo -n ' - ' >>$CUSTOM_HISTFILE; pwd | xargs echo -n >>$CUSTOM_HISTFILE; echo -n ' - ' >>$CUSTOM_HISTFILE; tail -n 1 $HISTFILE >>$CUSTOM_HISTFILE; $PROMPT_COMMAND"

It's a bit circumstantial, but it works. An entry could look like this:

Mit Nov 13 13:44:39 CET 2013 - /home/test - ls -la

Can you please clear your question a bit. There is a command called fc(fixed command) in ubuntu which enables you to display the history file, edit and re-execute previous command. Basically it is the alias set up for history command.

By default,

fc  -l lists the 16 most recent commands.

You can also use

history command itself to see the command history.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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