I've come across an interesting problem that I have not yet been able to solve satisfyingly.

Consider a note taking command that takes two arguments:

note $project  $note

That simply files the content of $note under a project according to the first parameter.

Now I have two projects where I enter some data:

note "projectA" "asdfasdf"
note "projectB" "weradfvsaf"

This worked well when I had few projects. Then I got more projects and I had an Idea: Use the current path to determine the name of the project:


So I wrote a wrapper for the above function that parses the project name out of the directory path.

This worked very well, until I started to have several projects spanned across multiple paths:


Now my problem is: How can I continue to be lazy? I thought the easiest way would be to create a file, for instance project.rc, that only contains the name of the project that is contained in the folder and all subfolders of where it is located. The wrapper simply looks in the current dir for this file and continues to traverse upwards in the directory tree until the file is found. Then the file is read and the $project parameter is set accordingly.

I am not really familiar with shell scripting; Could anyone come up with a solution for this problem?


This will do the trick:

 PPATH=$(while [[ "$PWD" != / && ! -f project.rc ]]; do cd ..; done; echo "$PWD")

The variable PPATH will then contain the path of the project from which you call the command. You can then find all the info you need in your $PPATH/project.rc. (The variable will be positioned to / if there is no such file in parent directories.)

  • 1
    Simpler: PPATH=$(pwd -P); while [[ ! -e project.rc && -n $PPATH ]]; do PPATH=${PPATH#/*}; done – Gilles Apr 23 '12 at 23:26
  • Indeed, it's more efficient and the "not found" exception can be handled a bit more easily with this one. – Stéphane Gimenez Apr 24 '12 at 8:03

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.