Saturday, 22 June 2019

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


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.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...