bash history and keeping track of current working directory
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash history and keeping track of current working directory
Hi all,
I would like to keep track of not only what bash commands I used and when, but also where they were issued from, i.e. what was the current working directory when I issued "foobar" on a particular day and time. Can we ask bash history to keep track of working directories too? I have tried to get an idea of this reading the enormous "man bash", but I don't seem to have an answer yet either way.
-mark
I would like to keep track of not only what bash commands I used and when, but also where they were issued from, i.e. what was the current working directory when I issued "foobar" on a particular day and time.
AFAIK there's no variable, switch or configuration option for it. Why on earth would you want to do that anyway?
Add this to your .bashrc: export HISTTIMEFORMAT="$PWD %d %h %Y %H:%M:%S ".
Notice the $PWD in the format. That's the command ( as in echo $PWD) to show the current working directory. It does not give you exactly what you want. It shows the home directory, but the sequence of commands that include the cd command should allow you to keep track on where the commands were issued, as in this example from my history:
Quote:
454 /home/bigrigdriver08 Jun 2010 19:14:44 cd Documents
455 /home/bigrigdriver08 Jun 2010 19:14:45 ls
456 /home/bigrigdriver08 Jun 2010 19:15:39 less "HELPFUL HINTS.txt"
457 /home/bigrigdriver08 Jun 2010 19:15:46 cd personal
458 /home/bigrigdriver08 Jun 2010 19:15:47 la
459 /home/bigrigdriver08 Jun 2010 19:15:56 history | tail
Note the cd command followed by commands in that directory, then cd to another directory.
Last edited by bigrigdriver; 06-08-2010 at 07:24 PM.
Nope. Doesn't work either. Plus the "fun" part of it all is if you remove the "$PWD" part from HISTTIMEFORMAT and view 'history' afterwards you'll find the PWD part didn't stick in history entries...
One shouldn't write to the file like that. Don't ask me why but if that would be the default then there would be no "-a" and "-n" switch to the 'history' built-in...
thanks for the help.
In brief, it seems that keeping track of the directory from which a command was made is not an option with bash history, but there are various ways to keep track of the current working directory using file redirection.
My primary interest was really just to log my activity, that includes knowing where in the filesystem I was at any time.
-mark
The 'script' command can be used to log a terminal session. http://linux.die.net/man/1/script
The log includes the shell prompts which can be customised to display the date and the current working directory. http://www.cyberciti.biz/tips/howto-...up-prompt.html
'script' is different to bash history because it not only logs a command but also any output the command puts into the terminal window.
Example:
Enter this into a terminal and then enter some commands:
script -a -f
Terminal text will be appended (-a) to the default file 'typescript' keeping the file up to date (-f).
Use CTRL d to exit 'script'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.