How do you run a shell command/script automatically when entering/cd-ing a directory on Snow Leopard?

7

2

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.

Mark Scholtz

Posted 2011-05-14T08:24:46.717

Reputation: 73

Entering as in cding there? – Daniel Beck – 2011-05-14T09:42:10.857

Yep "Entering" == "cd-ing" – Mark Scholtz – 2011-05-14T12:04:43.367

Answers

8

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
    fi
}

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.

Daniel Beck

Posted 2011-05-14T08:24:46.717

Reputation: 98 421

1This won't work with popd and pushd if you use those. Change them accordingly. – Daniel Beck – 2011-05-14T09:48:00.083

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. – Mark Scholtz – 2011-05-14T12:10:16.097

@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 – 2011-05-14T12:14:01.510

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. – Mark Scholtz – 2011-05-14T12:39:18.293

@Mark You should be able to add the if part of my function definition to the cd function in that file as I mentioned in the last sentence of my answer. Depends on how they implement their functionality, though. – Daniel Beck – 2011-05-14T12:58:24.183

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

– Mark Scholtz – 2011-05-15T07:36:02.087