How to run a file in a different directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
4
down vote

favorite
1












There is a file that I would like execute in a different folder, under about four sub-directories.



For example:



My pwd may be /home/directoryA. However, the file I would like
execute may be in directoryD. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/ and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile



  1. Is it possible to execute the file without actually being in the directory where the file is?


  2. Is there a shortcut way of executing the file somefile without going into the directory?







share|improve this question


















  • 1




    Beside the point, but note that directoryD and DirectoryD are not the same.
    – wjandrea
    May 17 at 3:33







  • 2




    @wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
    – WinEunuuchs2Unix
    May 17 at 4:40










  • I've updated my answer based on new comments read under other answers.
    – WinEunuuchs2Unix
    May 17 at 12:32














up vote
4
down vote

favorite
1












There is a file that I would like execute in a different folder, under about four sub-directories.



For example:



My pwd may be /home/directoryA. However, the file I would like
execute may be in directoryD. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/ and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile



  1. Is it possible to execute the file without actually being in the directory where the file is?


  2. Is there a shortcut way of executing the file somefile without going into the directory?







share|improve this question


















  • 1




    Beside the point, but note that directoryD and DirectoryD are not the same.
    – wjandrea
    May 17 at 3:33







  • 2




    @wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
    – WinEunuuchs2Unix
    May 17 at 4:40










  • I've updated my answer based on new comments read under other answers.
    – WinEunuuchs2Unix
    May 17 at 12:32












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





There is a file that I would like execute in a different folder, under about four sub-directories.



For example:



My pwd may be /home/directoryA. However, the file I would like
execute may be in directoryD. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/ and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile



  1. Is it possible to execute the file without actually being in the directory where the file is?


  2. Is there a shortcut way of executing the file somefile without going into the directory?







share|improve this question














There is a file that I would like execute in a different folder, under about four sub-directories.



For example:



My pwd may be /home/directoryA. However, the file I would like
execute may be in directoryD. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/ and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile



  1. Is it possible to execute the file without actually being in the directory where the file is?


  2. Is there a shortcut way of executing the file somefile without going into the directory?









share|improve this question













share|improve this question




share|improve this question








edited May 17 at 12:09









ToxicMender

143




143










asked May 16 at 23:07









Carltonp

211




211







  • 1




    Beside the point, but note that directoryD and DirectoryD are not the same.
    – wjandrea
    May 17 at 3:33







  • 2




    @wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
    – WinEunuuchs2Unix
    May 17 at 4:40










  • I've updated my answer based on new comments read under other answers.
    – WinEunuuchs2Unix
    May 17 at 12:32












  • 1




    Beside the point, but note that directoryD and DirectoryD are not the same.
    – wjandrea
    May 17 at 3:33







  • 2




    @wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
    – WinEunuuchs2Unix
    May 17 at 4:40










  • I've updated my answer based on new comments read under other answers.
    – WinEunuuchs2Unix
    May 17 at 12:32







1




1




Beside the point, but note that directoryD and DirectoryD are not the same.
– wjandrea
May 17 at 3:33





Beside the point, but note that directoryD and DirectoryD are not the same.
– wjandrea
May 17 at 3:33





2




2




@wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
– WinEunuuchs2Unix
May 17 at 4:40




@wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked.
– WinEunuuchs2Unix
May 17 at 4:40












I've updated my answer based on new comments read under other answers.
– WinEunuuchs2Unix
May 17 at 12:32




I've updated my answer based on new comments read under other answers.
– WinEunuuchs2Unix
May 17 at 12:32










4 Answers
4






active

oldest

votes

















up vote
6
down vote













No you don't need to use:



cd home/directoryA/directoryB/directoryC/DirectoryD
./somefile 


You can simply run the command by prefixing it with its path:



/home/directoryA/directoryB/directoryC/DirectoryD/somefile


Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:



./directoryB/directoryC/DirectoryD/somefile



I noticed OP has expanded scope via comments under other answers. Here is some additional information:



  • To find out where somefile is located use: locate somefile.

  • If somefile was added today you need to first update the locate database by running sudo updatedb.

  • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.

  • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).

  • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.

  • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile





share|improve this answer





























    up vote
    1
    down vote













    Sure! If somefile is marked as executable, you can run it with



    ~/directoryA/directoryB/directoryC/DirectoryD/somefile


    Want to know if somefile is executable? Go to its directory and run



    find . -maxdepth 1 -perm -111 -type f


    to see all the executables in that directory.






    share|improve this answer






















    • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
      – Carltonp
      May 16 at 23:25










    • Also, can you let me know how to find out what directory a file is located in?
      – Carltonp
      May 16 at 23:27






    • 2




      If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
      – wjandrea
      May 17 at 3:31










    • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
      – K7AAY
      May 17 at 20:13


















    up vote
    1
    down vote













    The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.



    Add in .bashrc file:



    export PATH=$PATH:/../your_directory





    share|improve this answer






















    • /.. is the same as /. Is that supposed to be /... ?
      – wjandrea
      May 24 at 1:59

















    up vote
    -2
    down vote













    There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).



    You can run:



    # Assuming it is a script you made, changing file permission to make it executable
    chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

    # Appending your shell profile with an alias to run the script from wherever you are
    echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile

    # replace ~/.profile with config file of whichever shell you use
    # Also replace source with python if the script is a python script or whichever interpreter it requires for execution
    # Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script


    Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).



    You can then run the script as executable in any directory, like:



    somename


    P.S.:
    As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.






    share|improve this answer






















    • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
      – David Foerster
      May 17 at 22:15










    • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
      – wjandrea
      May 24 at 2:18











    • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
      – wjandrea
      May 24 at 2:24











    • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
      – ToxicMender
      May 24 at 21:07











    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1037162%2fhow-to-run-a-file-in-a-different-directory%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote













    No you don't need to use:



    cd home/directoryA/directoryB/directoryC/DirectoryD
    ./somefile 


    You can simply run the command by prefixing it with its path:



    /home/directoryA/directoryB/directoryC/DirectoryD/somefile


    Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:



    ./directoryB/directoryC/DirectoryD/somefile



    I noticed OP has expanded scope via comments under other answers. Here is some additional information:



    • To find out where somefile is located use: locate somefile.

    • If somefile was added today you need to first update the locate database by running sudo updatedb.

    • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.

    • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).

    • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.

    • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile





    share|improve this answer


























      up vote
      6
      down vote













      No you don't need to use:



      cd home/directoryA/directoryB/directoryC/DirectoryD
      ./somefile 


      You can simply run the command by prefixing it with its path:



      /home/directoryA/directoryB/directoryC/DirectoryD/somefile


      Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:



      ./directoryB/directoryC/DirectoryD/somefile



      I noticed OP has expanded scope via comments under other answers. Here is some additional information:



      • To find out where somefile is located use: locate somefile.

      • If somefile was added today you need to first update the locate database by running sudo updatedb.

      • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.

      • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).

      • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.

      • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile





      share|improve this answer
























        up vote
        6
        down vote










        up vote
        6
        down vote









        No you don't need to use:



        cd home/directoryA/directoryB/directoryC/DirectoryD
        ./somefile 


        You can simply run the command by prefixing it with its path:



        /home/directoryA/directoryB/directoryC/DirectoryD/somefile


        Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:



        ./directoryB/directoryC/DirectoryD/somefile



        I noticed OP has expanded scope via comments under other answers. Here is some additional information:



        • To find out where somefile is located use: locate somefile.

        • If somefile was added today you need to first update the locate database by running sudo updatedb.

        • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.

        • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).

        • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.

        • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile





        share|improve this answer














        No you don't need to use:



        cd home/directoryA/directoryB/directoryC/DirectoryD
        ./somefile 


        You can simply run the command by prefixing it with its path:



        /home/directoryA/directoryB/directoryC/DirectoryD/somefile


        Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:



        ./directoryB/directoryC/DirectoryD/somefile



        I noticed OP has expanded scope via comments under other answers. Here is some additional information:



        • To find out where somefile is located use: locate somefile.

        • If somefile was added today you need to first update the locate database by running sudo updatedb.

        • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.

        • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).

        • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.

        • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 24 at 2:07

























        answered May 16 at 23:20









        WinEunuuchs2Unix

        34.6k756131




        34.6k756131






















            up vote
            1
            down vote













            Sure! If somefile is marked as executable, you can run it with



            ~/directoryA/directoryB/directoryC/DirectoryD/somefile


            Want to know if somefile is executable? Go to its directory and run



            find . -maxdepth 1 -perm -111 -type f


            to see all the executables in that directory.






            share|improve this answer






















            • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
              – Carltonp
              May 16 at 23:25










            • Also, can you let me know how to find out what directory a file is located in?
              – Carltonp
              May 16 at 23:27






            • 2




              If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
              – wjandrea
              May 17 at 3:31










            • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
              – K7AAY
              May 17 at 20:13















            up vote
            1
            down vote













            Sure! If somefile is marked as executable, you can run it with



            ~/directoryA/directoryB/directoryC/DirectoryD/somefile


            Want to know if somefile is executable? Go to its directory and run



            find . -maxdepth 1 -perm -111 -type f


            to see all the executables in that directory.






            share|improve this answer






















            • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
              – Carltonp
              May 16 at 23:25










            • Also, can you let me know how to find out what directory a file is located in?
              – Carltonp
              May 16 at 23:27






            • 2




              If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
              – wjandrea
              May 17 at 3:31










            • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
              – K7AAY
              May 17 at 20:13













            up vote
            1
            down vote










            up vote
            1
            down vote









            Sure! If somefile is marked as executable, you can run it with



            ~/directoryA/directoryB/directoryC/DirectoryD/somefile


            Want to know if somefile is executable? Go to its directory and run



            find . -maxdepth 1 -perm -111 -type f


            to see all the executables in that directory.






            share|improve this answer














            Sure! If somefile is marked as executable, you can run it with



            ~/directoryA/directoryB/directoryC/DirectoryD/somefile


            Want to know if somefile is executable? Go to its directory and run



            find . -maxdepth 1 -perm -111 -type f


            to see all the executables in that directory.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 17 at 20:06

























            answered May 16 at 23:19









            K7AAY

            3,73221443




            3,73221443











            • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
              – Carltonp
              May 16 at 23:25










            • Also, can you let me know how to find out what directory a file is located in?
              – Carltonp
              May 16 at 23:27






            • 2




              If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
              – wjandrea
              May 17 at 3:31










            • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
              – K7AAY
              May 17 at 20:13

















            • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
              – Carltonp
              May 16 at 23:25










            • Also, can you let me know how to find out what directory a file is located in?
              – Carltonp
              May 16 at 23:27






            • 2




              If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
              – wjandrea
              May 17 at 3:31










            • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
              – K7AAY
              May 17 at 20:13
















            Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
            – Carltonp
            May 16 at 23:25




            Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory.
            – Carltonp
            May 16 at 23:25












            Also, can you let me know how to find out what directory a file is located in?
            – Carltonp
            May 16 at 23:27




            Also, can you let me know how to find out what directory a file is located in?
            – Carltonp
            May 16 at 23:27




            2




            2




            If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
            – wjandrea
            May 17 at 3:31




            If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ?
            – wjandrea
            May 17 at 3:31












            @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
            – K7AAY
            May 17 at 20:13





            @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in.
            – K7AAY
            May 17 at 20:13











            up vote
            1
            down vote













            The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.



            Add in .bashrc file:



            export PATH=$PATH:/../your_directory





            share|improve this answer






















            • /.. is the same as /. Is that supposed to be /... ?
              – wjandrea
              May 24 at 1:59














            up vote
            1
            down vote













            The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.



            Add in .bashrc file:



            export PATH=$PATH:/../your_directory





            share|improve this answer






















            • /.. is the same as /. Is that supposed to be /... ?
              – wjandrea
              May 24 at 1:59












            up vote
            1
            down vote










            up vote
            1
            down vote









            The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.



            Add in .bashrc file:



            export PATH=$PATH:/../your_directory





            share|improve this answer














            The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.



            Add in .bashrc file:



            export PATH=$PATH:/../your_directory






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 24 at 1:57









            wjandrea

            7,10742054




            7,10742054










            answered May 17 at 8:54









            Pierre Legay

            111




            111











            • /.. is the same as /. Is that supposed to be /... ?
              – wjandrea
              May 24 at 1:59
















            • /.. is the same as /. Is that supposed to be /... ?
              – wjandrea
              May 24 at 1:59















            /.. is the same as /. Is that supposed to be /... ?
            – wjandrea
            May 24 at 1:59




            /.. is the same as /. Is that supposed to be /... ?
            – wjandrea
            May 24 at 1:59










            up vote
            -2
            down vote













            There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).



            You can run:



            # Assuming it is a script you made, changing file permission to make it executable
            chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

            # Appending your shell profile with an alias to run the script from wherever you are
            echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile

            # replace ~/.profile with config file of whichever shell you use
            # Also replace source with python if the script is a python script or whichever interpreter it requires for execution
            # Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script


            Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).



            You can then run the script as executable in any directory, like:



            somename


            P.S.:
            As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.






            share|improve this answer






















            • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
              – David Foerster
              May 17 at 22:15










            • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
              – wjandrea
              May 24 at 2:18











            • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
              – wjandrea
              May 24 at 2:24











            • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
              – ToxicMender
              May 24 at 21:07















            up vote
            -2
            down vote













            There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).



            You can run:



            # Assuming it is a script you made, changing file permission to make it executable
            chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

            # Appending your shell profile with an alias to run the script from wherever you are
            echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile

            # replace ~/.profile with config file of whichever shell you use
            # Also replace source with python if the script is a python script or whichever interpreter it requires for execution
            # Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script


            Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).



            You can then run the script as executable in any directory, like:



            somename


            P.S.:
            As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.






            share|improve this answer






















            • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
              – David Foerster
              May 17 at 22:15










            • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
              – wjandrea
              May 24 at 2:18











            • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
              – wjandrea
              May 24 at 2:24











            • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
              – ToxicMender
              May 24 at 21:07













            up vote
            -2
            down vote










            up vote
            -2
            down vote









            There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).



            You can run:



            # Assuming it is a script you made, changing file permission to make it executable
            chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

            # Appending your shell profile with an alias to run the script from wherever you are
            echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile

            # replace ~/.profile with config file of whichever shell you use
            # Also replace source with python if the script is a python script or whichever interpreter it requires for execution
            # Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script


            Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).



            You can then run the script as executable in any directory, like:



            somename


            P.S.:
            As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.






            share|improve this answer














            There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).



            You can run:



            # Assuming it is a script you made, changing file permission to make it executable
            chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

            # Appending your shell profile with an alias to run the script from wherever you are
            echo "alias somename="source ~/directoryA/directoryB/directoryC/directoryD/somefile"" >> ~/.profile

            # replace ~/.profile with config file of whichever shell you use
            # Also replace source with python if the script is a python script or whichever interpreter it requires for execution
            # Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script


            Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).



            You can then run the script as executable in any directory, like:



            somename


            P.S.:
            As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 17 at 14:12

























            answered May 17 at 6:28









            ToxicMender

            143




            143











            • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
              – David Foerster
              May 17 at 22:15










            • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
              – wjandrea
              May 24 at 2:18











            • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
              – wjandrea
              May 24 at 2:24











            • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
              – ToxicMender
              May 24 at 21:07

















            • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
              – David Foerster
              May 17 at 22:15










            • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
              – wjandrea
              May 24 at 2:18











            • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
              – wjandrea
              May 24 at 2:24











            • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
              – ToxicMender
              May 24 at 21:07
















            @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
            – David Foerster
            May 17 at 22:15




            @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged.
            – David Foerster
            May 17 at 22:15












            -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
            – wjandrea
            May 24 at 2:18





            -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH?
            – wjandrea
            May 24 at 2:18













            Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
            – wjandrea
            May 24 at 2:24





            Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source.
            – wjandrea
            May 24 at 2:24













            i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
            – ToxicMender
            May 24 at 21:07





            i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.
            – ToxicMender
            May 24 at 21:07













             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1037162%2fhow-to-run-a-file-in-a-different-directory%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            pylint3 and pip3 broken

            Missing snmpget and snmpwalk

            How to enroll fingerprints to Ubuntu 17.10 with VFS491