Is it possible to use Wildcards in $PATH

3 replies [Last post]
jtladner
User offline. Last seen 6 years 28 weeks ago. Offline
Joined: 04/01/2011

My 'scripts' folder is getting a bit cluttered and so I would like to start organizing scripts into sub folders. However, I don't want to have to add each sub-folder to my $PATH variable. Is it possible to use a wildcard to tell the computer to look for scripts within all subdirectories in the scripts folder?

I tried adding '~/scripts/*' to my $PATH, but the start did not have the desired 'wildcard' effect. Instead it seemed to just be looking for a folder with the name '*'.

Any solution to this would greatly improve my ability to refind scripts that I am sure I have already written. Thanks

Steve
User offline. Last seen 4 years 7 weeks ago. Offline
Joined: 08/15/2010
Adding multiple folders to the PATH

Hi JT --

Interesting question. I could benefit from something similar.

I imagine there are a few ways to do this, but to me, the easiest would be with a little loop, to step through all the folders in the directory. (The first line uses the backtick symbol to assign the output of the ls command to MYFILES. Remember there cannot be any spaces on either side of the equals signs...)

MYFILES=`ls -F $HOME/scripts | grep "\/" `
for FOLDER in $MYFILES; do
  PATH="$PATH:$HOME/scripts/$FOLDER"
  echo "Adding $FOLDER"
done
export PATH

If anyone has a more straightforward way to do it, I'd be interested to see...

jtladner
User offline. Last seen 6 years 28 weeks ago. Offline
Joined: 04/01/2011
Thanks Steve!

Steve,

Thats great. Seems to be doing just what I wanted.

One small typo though for people that are just cutting and pasting, 'scripts' is missing a 't' in third line in Steve's solution.

Jason

Steve
User offline. Last seen 4 years 7 weeks ago. Offline
Joined: 08/15/2010
PATH

oops. thanks. fixed typo...