1

Can anyone tell me how to add a directory, say Idl, which contains many sub-directories to my IDL_PATH in .bashrc such that when I do source .bashrc, I will be able to it is able to run all the executables inside all the different sub-directories?

Is this possible? I am doing

IDL_PATH=.:+/idl/idl70/lib:/home/cs/Idl .... 

The Idl folder has many sub-directories (eg. xidl, idl_code, net etc.). However, I am only able to run files in folder Idl, not those in sub-directories.

The files which I want to run are IDL procedures with .pro extensions. If I put my all .pro files into a single directory, say /home/cs/Idl, then my whole pro files are being run.

Is there any way?

  • Do you need to add the directories to IDL_PATH or PATH? – terdon Mar 20 '16 at 14:08
  • Any thing either PATH or IDL_PATH its just a name to direct a particular path. – Sapna Mishra Mar 20 '16 at 14:14
  • Are you sure? It looks like IDL_PATH is a completely different thing and used by the idl library. Directories in your PATH are searched recursively, so subdirectories are added automatically. It looks like IDL_PATH doesn't work that way. What are you trying to do? Do you just want to be able to run programs in the Idl directory? If so, add Idl to PATH not IDL_PATH. – terdon Mar 20 '16 at 14:19
  • No it is not running even with simple PATH option. I think it should be IDL_PATH. – Sapna Mishra Mar 20 '16 at 14:30
  • I want all the programs which are in various sub directories inside the main Idl directory to be run globally any where in the system. – Sapna Mishra Mar 20 '16 at 14:32
0

It looks like you will have to either add each directory that contains an executable separately or link all executables to the same directory.

  1. Add all directories manually.

    First, collect the list of relevant directories (I am assuming you are running Linux or, in any case, that you have GNU tools):

    find /home/cs/Id -type f -executable -exec dirname {} + | sort -u
    

    The above will find all executable files in /home/cs/Id and print the name of the directory containing them. The sort -u ensures that each directory is only printed once. I created a few test directories, so on my system I get:

    $ find /home/terdon/Idl/ -type f -executable -exec dirname {} + | sort -u
    /home/terdon/Idl/foo/bar/bag/ho
    /home/terdon/Idl/foo/bar/bag/ho/fa/fe/re
    /home/terdon/Idl/foo/bar/baz/foo/bar
    /home/terdon/Idl/foo/bar/baz/foo/bar/baz/foo/bar/baz
    

    Now, you want to get these into the right format for adding to $PATH:

    $ find /home/terdon/Idl/ -type f -executable -exec dirname {} + | sort -u | tr '\n' ':'
    /home/terdon/Idl/foo/bar/bag/ho:/home/terdon/Idl/foo/bar/bag/ho/fa/fe/re:/home/terdon/Idl/foo/bar/baz/foo/bar:/home/terdon/Idl/foo/bar/baz/foo/bar/baz/foo/bar/baz:
    

    Copy that (excluding the final :) and add it to your PATH:

    PATH="$PATH:/home/terdon/Idl/foo/bar/bag/ho:/home/terdon/Idl/foo/bar/bag/ho/fa/fe/re:/home/terdon/Idl/foo/bar/baz/foo/bar:/home/terdon/Idl/foo/bar/baz/foo/bar/baz/foo/bar/baz"
    
  2. Find all executable files and symlink them to a directory already in your PATH.

    find /home/terdon/Idl/ -type f -executable -exec ln -s {} ~/bin \;
    

    On some Linux systems (Ubuntu, for example) the ~/bin directory is automatically added to your PATH if it exists. If this doesn't happen on your system, add it yourself:

    PATH="$PATH:/home/cs/bin"
    
0

I think one has to enumerate the directories, then. This generates the line for you automatically:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright © 2016 Martin Ueding <dev@martin-ueding.de>
# Licensed under the MIT license

import argparse
import os


def main():
    options = _parse_args()

    additions = []

    for base in options.base:
        for dirpath, dirnames, filenames in os.walk(base):
            additions.append(dirpath)

    line = ':'.join(additions)

    if options.short:
        print(line)
    else:
        print('PATH=$PATH:'+line)


def _parse_args():
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('base', nargs='+', help='Basepath')
    parser.add_argument('--short', action='store_true')
    options = parser.parse_args()

    return options


if __name__ == '__main__':
    main()

Save that snippet as /usr/bin/path-recurse and apply chmod +x /usr/bin/path-recurse. Then you can use it in your prompt either to generate the line with path-recurse /tmp /foobar … and it will give you the Bash line:

PATH=$PATH:/tmp:/tmp/kde-muIusrrC:/tmp/vs0cOIg:/tmp/.esd-1000:/tmp/ssh-oGWOvuUnyqPF:/tmp/.Test-unix:/tmp/.font-unix:/tmp/.XIM-unix:/tmp/.ICE-unix:/tmp/.X11-unix 

Alternatively you can use the following in your .bashrc:

PATH=$PATH:$(path-recurve /tmp /foobar …)

This will then dynamically go through all the folders whenever the .bashrc is sourced.

0

I think I have solved my problem. All I did in my .bashrc file I wrote:

PATH=${PATH}:$(find ~/Idl -type d | tr '\n' ':' | sed 's/:$//')

Then: > source .bashrc

Now I am able to run all programs and scripts inside various sub-directories under my main Idl directory.

Thanks all for your kind attentions.

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.