LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 06-08-2010, 05:08 PM   #1
andrews-mark
Member
 
Registered: Feb 2007
Location: London
Distribution: debian
Posts: 108

Rep: Reputation: 15
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
 
Old 06-08-2010, 05:46 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by andrews-mark View Post
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?
 
Old 06-08-2010, 07:21 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.
 
Old 06-09-2010, 02:22 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Nice one however it doesn't display the real one, the value of PWD stays at "~".
 
Old 06-09-2010, 04:05 AM   #5
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Code:
alias cd="export HISTTIMEFORMAT=\"$PWD %d %h %Y %H:%M:%S \" && cd"
 
Old 06-09-2010, 04:11 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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...
 
Old 06-09-2010, 04:26 AM   #7
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Code:
alias cd="pwd >> ~/.bash_history && cd"
 
Old 06-09-2010, 05:00 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Agrouf View Post
Code:
pwd >> ~/.bash_history
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...
 
Old 06-12-2010, 08:47 AM   #9
andrews-mark
Member
 
Registered: Feb 2007
Location: London
Distribution: debian
Posts: 108

Original Poster
Rep: Reputation: 15
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
 
Old 06-12-2010, 04:47 PM   #10
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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'.
 
Old 06-12-2010, 05:18 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
DOH ... removed ... didn't work either :/

Last edited by Tinkster; 06-12-2010 at 05:22 PM.
 
Old 06-13-2010, 07:24 AM   #12
andrews-mark
Member
 
Registered: Feb 2007
Location: London
Distribution: debian
Posts: 108

Original Poster
Rep: Reputation: 15
kenhelm - thanks for the 'script' tip
-m
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] history -c is not working in bash script sd|| Programming 1 03-26-2010 12:02 PM
Bash to find and disect current working Directory carl0ski Linux - General 4 10-28-2006 04:36 AM
Bash: keeping track of newly added files madtinkerer Programming 5 09-13-2006 09:30 AM
history not working in bash subu_s AIX 2 12-29-2004 12:49 PM
BASH: How to get current workin directory? gmitra Programming 9 09-20-2003 10:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration