Apologies if the answer to this is obvious, but I haven't been able to find one yet (perhaps because I'm searching with the incorrect terminology).

I am on a Mac running Snow Leopard and I would like to know if it is possible to have a shell command (or a script) run automatically when I enter/cd into that directory.

An example to better illustrate my question: I use RVM for managing versions of Ruby. It employs a similar tactic with it's project .rvmrc files. These files are run when the directory they are contained in is entered in order to use the correct version of Ruby for that specific project.

I would like to do something similar in order to display the contents of a todo.txt file when I enter that files containing directory.

  • Entering as in cding there?
    – Daniel Beck
    May 14 '11 at 9:42
  • Yep "Entering" == "cd-ing" May 14 '11 at 12:04

Add the following to your ~/.bash_profile:

function cd {
    # actually change the directory with all args passed to the function
    builtin cd "$@"
    # if there's a regular file named "todo.txt"...
    if [ -f "todo.txt" ] ; then
        # display its contents
        cat todo.txt

It's possible you already have a similar function for cd — just extend that one to print the contents of todo.txt if it exists.

  • 1
    This won't work with popd and pushd if you use those. Change them accordingly.
    – Daniel Beck
    May 14 '11 at 9:48
  • Thanks very much for the answer! I tried adding the above method to my ~/.bash_profile, but it seems as though the builtin cd method is still being called rather than my custom cd method. I created a test method in my ~/.bash_profile to confirm that methods added there are being called and indeed they are. May 14 '11 at 12:10
  • @Mark in that case, something else modifies your cd again, as I hinted at in my answer — I posted only after confirming it to work for me. Find your current cd definition by entering shopt -s extdebug ; declare -F cd. Also, you need to new shell session or manually load .bash_profile again for the changes to come through.
    – Daniel Beck
    May 14 '11 at 12:14
  • 1
    @Daniel it seems that RVM is the culprit. The output from running that command resulted in the following: "cd 43 /Users/mark/.rvm/scripts/cd". Looking at that script I can see that it is doing something along the lines of what you are suggesting (it also runs the builtin cd "%@" command). If I modify my ~/.rmvrc file I can turn off that functionality which then allows my custom cd method to run, but then I lose RVM's custom functionality. I will have to read RMV's documentation to try and discover whether I can utilise both custom cd functions somehow. May 14 '11 at 12:39
  • 2
    @Daniel I fixed the problem by creating a file in my ~/.rvm/hooks/ directory called "after_cd" which is run by RVM itself when the cd function is called. If anyone is interested in how RVM handles directory changes a great answer can be found by it's creator here May 15 '11 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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