Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It looks like when adding a directory into $PATH, its subdirectories are not added recursively. So can I do that? Or is there a reason why this is not supported?

share|improve this question
4  
Why on earth do you need that? –  alex Jul 31 '11 at 7:35
    
@alex: Why do you think there is no need? –  Tim Jul 31 '11 at 11:16
2  
@Tim the reason it's not "generally" supported is it's a security risk and generally not needed. If you recursively add directories to your path who knows what might get stuck in there... makes it a lot harder to audit. –  xenoterracide Jul 31 '11 at 14:44
4  
@Tim: it's a really surprising and unusual thing to want, that's why. –  alex Jul 31 '11 at 15:21
add comment

2 Answers

up vote 4 down vote accepted

Add them recursively using find like so:

PATH=$PATH$( find $HOME/scripts/ -type d -printf ":%p" )

WARNING: As mentioned in the comments to the question this isn't encouraged as it poses a security risk because there is no guarantee that executable files in the directories added aren't malicious.

It's probably a better solution to follow Gilles' answer and use stow

share|improve this answer
4  
Warning: This answer is technically correct but as alex and xenoterracide note this is a crazy-insecure thing to do and you should definitely think twice before actually using this. –  Caleb Aug 1 '11 at 9:56
2  
Please add a warning to your answer about why this is not a good, safe thing to do in practice. –  Caleb Aug 1 '11 at 9:58
    
I wouldn't consider it particularly dangerous; adding random directories to $PATH is dangerous, but adding subdirectories isn't markedly worse. But it's usually useless, and possibly inefficient (and might even throw you onto environment size limits sometimes). –  Gilles Aug 2 '11 at 22:12
add comment

The usual unix directory structure has application files sorted into directories by kind: bin for executables, lib for libraries, doc for documentation and so on. That's when they are installed in separate directories; often applications are grouped into a few directories (hence many systems have just three directories in $PATH: /usr/local/bin, /usr/bin and /bin). It is rare to have both executable files and subdirectories inside a directory, so there's no demand for including a directory's subdirectories in $PATH.

What might occasionally be useful is to include all the bin subdirectories of subdirectories of a given directory in $PATH:

for d in /opt/*/bin; do PATH="$PATH:$d"; done

However, this is rarely done. The usual method when executables in non-standard directories are to be in $PATH is to make symbolic links in a directory in the path such as /usr/local/bin. The stow utility (or xstow) can be useful in that regard.

share|improve this answer
add comment

Your Answer

 
discard

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.