Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add recursively add folders to an exported variable. Suppose I have the MATLABPATH variable declared below:


And I want to add all folders inside the folder RECURSIVEFOLDER to MATLABPATH variable. How I do that in some automatic way?


share|improve this question
up vote 1 down vote accepted

Use find to list the directories:

export MATLABPATH="$(find RECURSIVEFOLDER -type d -printf %p:)$MATLABPATH"

The -printf %p: causes each directory to be printed with a : after it, so the find command's output will always end with a :. Provided $MATLABPATH wasn't empty before running this command, that will work fine.

Note that the above will add RECURSIVEFOLDER to the path, as well as its recursive children. If you didn't want that, add -mindepth 1 to the find command, if your find command implements -mindepth, or replace RECURSIVEFOLDER with RECURSIVEFOLDER/*.

share|improve this answer
You may add -mindepth 1 to find command to not add the RECURSIVEFOLDER x). Thanks x) – Werner Aug 4 '13 at 15:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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