I want to use the script program to record my session any time that I am working within a certain directory, such as my ~/projects directory.

So for example, I would cd into ~/projects, and script would automatically start recording everything to a log in that directory. As soon as I leave ~/projects, then script would stop. Also, ~/projects has many subdirectories; the log file needs to remain at the top level of the directory tree.

Any ideas on how this would be implemented? Even better, is there a method of implementing this that would also work with other programs easily?

  • Just about the only thing I can think of would be to do something like export PROMPT_COMMAND="if [[ "$HOME/projects" == "$PWD" ]]; then unset PROMPT_COMMAND; script; fi in your .profile, but it won't automatically exit and will only trigger if you go into the top directory
    – DopeGhoti
    Dec 29, 2015 at 20:52
  • I've used a similar technique for a custom function which has slightly different actions depending on the $PWD, but its triggered manually. I was thinking about aliasing the cd command in such a way that after changing to the desired directory it also performs this check and starts/ends script appropriately, but I wasn't sure if this might cause unforeseen problems. I'm not yet familiar with other ways to have actions performed in the background based on the $PWD Dec 29, 2015 at 21:26
  • You could override the cd command with a shell function that does command cd "$@" and then checks $PWD and starts script if the user is in the right directory. The problem is exiting script, because it fires up its own shell subprocess. The custom cd shell function could issue an exit as soon as the user moves elsewhere, but since the current directory is local to each subprocess, the invocation of cd that takes the user out of the special directory would not be able to actually change his/her working directory.
    – Kusalananda
    Dec 28, 2016 at 17:48


You must log in to answer this question.

Browse other questions tagged .