How can I run command in a folder without changing my current directory to it?Commandline shortcut for current directory similar to ~ for home directory?Bash aliases - how do I run a file in a different folder without losing my current location?Run program from anywhere without changing directoryHow to point to the current directory while executing commandsHow to run a command (that's meant for parent shell) from a shell script?

Conditional types in Typescript

Three people wearing hats

Fixing the fields in an incorrectly generated file

Why do cargo airlines frequently choose passenger aircraft rather than aircraft designed specifically for cargo?

Are optimal hyperparameters still optimal for a deeper neural net architecture?

Running DOS, Windows 3, and Windows 98 from one FAT32 partition?

Why did my LGA-ORD flight make an S-shaped turn round the time it was passing a storm?

Certainly naive, but I do not understand why this compiles

Should a soda bottle be stored horizontally or vertically?

Why "alle Tale" and not "alle Täler"?

What made the Tusken Raiders unable / unwilling to shoot down Luke's Landspeeder?

Why are green parties so often opposed to nuclear power?

Is DNA a language?

What do these chord annotations mean?

Should user input be validated for its length in PHP (server side) as a security measure?

How to set up a "Forced choice" for players in a game?

How to tell my Mom that I don't care about someone without upsetting her?

What are examples of (collections of) papers which "close" a field?

Do asylum seekers in the Netherlands need fifteen family members in the country?

Does "dd" by itself do anything?

If Alice tries to shift into Bob the same night that Bob is also killed by the militia, what happens?

Is it possible for a moon to have a higher surface gravity than the planet it is attached to?

Which object has been to space the most times?

What is the purpose of the rules in counterpoint composition?



How can I run command in a folder without changing my current directory to it?


Commandline shortcut for current directory similar to ~ for home directory?Bash aliases - how do I run a file in a different folder without losing my current location?Run program from anywhere without changing directoryHow to point to the current directory while executing commandsHow to run a command (that's meant for parent shell) from a shell script?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









18


















may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:



~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key


Though I want something like this:



~$ .folder command --key
~$ another_command --key


Is it possible?










share|improve this question



























  • Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

    – glenn jackman
    Jan 22 '14 at 16:58


















18


















may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:



~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key


Though I want something like this:



~$ .folder command --key
~$ another_command --key


Is it possible?










share|improve this question



























  • Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

    – glenn jackman
    Jan 22 '14 at 16:58














18













18









18


5






may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:



~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key


Though I want something like this:



~$ .folder command --key
~$ another_command --key


Is it possible?










share|improve this question
















may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:



~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key


Though I want something like this:



~$ .folder command --key
~$ another_command --key


Is it possible?







bash command-line scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 '14 at 16:49









Rmano

27.6k9 gold badges85 silver badges155 bronze badges




27.6k9 gold badges85 silver badges155 bronze badges










asked Jan 22 '14 at 16:02









Timur FayzrakhmanovTimur Fayzrakhmanov

23.8k8 gold badges21 silver badges35 bronze badges




23.8k8 gold badges21 silver badges35 bronze badges















  • Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

    – glenn jackman
    Jan 22 '14 at 16:58


















  • Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

    – glenn jackman
    Jan 22 '14 at 16:58

















Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

– glenn jackman
Jan 22 '14 at 16:58






Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ?

– glenn jackman
Jan 22 '14 at 16:58











4 Answers
4






active

oldest

votes


















43



















If you want to avoid the second cd you can use



(cd .folder && command --key)
another_command --key





share|improve this answer



























  • Very quick answer! I even can't to accept it because system doesn't allow me))

    – Timur Fayzrakhmanov
    Jan 22 '14 at 16:12






  • 1





    magic parenthesis! how does that work? +1

    – precise
    Jan 22 '14 at 18:06











  • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

    – Florian Diesch
    Jan 22 '14 at 18:25






  • 7





    I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

    – geirha
    Jan 24 '14 at 9:35











  • +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

    – jaybee
    Jul 10 '17 at 4:11


















8



















Without cd... Not even once. I found two ways:





# Save where you are and cd to other dir
pushd .folder
command --key
# Get back where you were at the beginning.
popd
another_command --key


and second:



find . -maxdepth 1 -type d -name ".folder" -execdir command --key ;
another_command --key





share|improve this answer
































    1



















    A simple bash function for running a command in specific directory:



    # Run a command in specific directory
    run_within_dir()
    target_dir="$1"
    previous_dir=$(pwd)
    shift
    cd $target_dir && "$@"
    cd $previous_dir



    Usage:



    $ cd ~
    $ run_within_dir /tmp ls -l # change into `/tmp` dir before running `ls -al`
    $ pwd # still at home dir





    share|improve this answer
































      0



















      I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:



      #include <unistd.h>
      #include <stdio.h>
      #include <stdlib.h>

      char ENV_PATH[8192] = "PWD=";

      int main(int argc, char** argv)
      if(argc < 3)
      fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]n");
      return 1;


      if(chdir(argv[1]))
      fprintf(stderr, "Error setting working directory to "%s"n", argv[1]);
      return 2;


      if(!getcwd(ENV_PATH + 4, 8192-4))
      fprintf(stderr, "Error getting the full path to the working directory "%s"n", argv[1]);
      return 3;


      if(putenv(ENV_PATH))
      fprintf(stderr, "Error setting the environment variable "%s"n", ENV_PATH);
      return 4;


      execvp(argv[2], argv+2);



      The usage is like this:



      $ in /path/to/directory command --key





      share|improve this answer


























        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        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%2f409244%2fhow-can-i-run-command-in-a-folder-without-changing-my-current-directory-to-it%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown


























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        43



















        If you want to avoid the second cd you can use



        (cd .folder && command --key)
        another_command --key





        share|improve this answer



























        • Very quick answer! I even can't to accept it because system doesn't allow me))

          – Timur Fayzrakhmanov
          Jan 22 '14 at 16:12






        • 1





          magic parenthesis! how does that work? +1

          – precise
          Jan 22 '14 at 18:06











        • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

          – Florian Diesch
          Jan 22 '14 at 18:25






        • 7





          I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

          – geirha
          Jan 24 '14 at 9:35











        • +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

          – jaybee
          Jul 10 '17 at 4:11















        43



















        If you want to avoid the second cd you can use



        (cd .folder && command --key)
        another_command --key





        share|improve this answer



























        • Very quick answer! I even can't to accept it because system doesn't allow me))

          – Timur Fayzrakhmanov
          Jan 22 '14 at 16:12






        • 1





          magic parenthesis! how does that work? +1

          – precise
          Jan 22 '14 at 18:06











        • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

          – Florian Diesch
          Jan 22 '14 at 18:25






        • 7





          I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

          – geirha
          Jan 24 '14 at 9:35











        • +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

          – jaybee
          Jul 10 '17 at 4:11













        43















        43











        43









        If you want to avoid the second cd you can use



        (cd .folder && command --key)
        another_command --key





        share|improve this answer
















        If you want to avoid the second cd you can use



        (cd .folder && command --key)
        another_command --key






        share|improve this answer















        share|improve this answer




        share|improve this answer








        edited Jul 10 '17 at 10:35

























        answered Jan 22 '14 at 16:06









        Florian DieschFlorian Diesch

        70k17 gold badges184 silver badges197 bronze badges




        70k17 gold badges184 silver badges197 bronze badges















        • Very quick answer! I even can't to accept it because system doesn't allow me))

          – Timur Fayzrakhmanov
          Jan 22 '14 at 16:12






        • 1





          magic parenthesis! how does that work? +1

          – precise
          Jan 22 '14 at 18:06











        • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

          – Florian Diesch
          Jan 22 '14 at 18:25






        • 7





          I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

          – geirha
          Jan 24 '14 at 9:35











        • +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

          – jaybee
          Jul 10 '17 at 4:11

















        • Very quick answer! I even can't to accept it because system doesn't allow me))

          – Timur Fayzrakhmanov
          Jan 22 '14 at 16:12






        • 1





          magic parenthesis! how does that work? +1

          – precise
          Jan 22 '14 at 18:06











        • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

          – Florian Diesch
          Jan 22 '14 at 18:25






        • 7





          I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

          – geirha
          Jan 24 '14 at 9:35











        • +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

          – jaybee
          Jul 10 '17 at 4:11
















        Very quick answer! I even can't to accept it because system doesn't allow me))

        – Timur Fayzrakhmanov
        Jan 22 '14 at 16:12





        Very quick answer! I even can't to accept it because system doesn't allow me))

        – Timur Fayzrakhmanov
        Jan 22 '14 at 16:12




        1




        1





        magic parenthesis! how does that work? +1

        – precise
        Jan 22 '14 at 18:06





        magic parenthesis! how does that work? +1

        – precise
        Jan 22 '14 at 18:06













        The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

        – Florian Diesch
        Jan 22 '14 at 18:25





        The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands.

        – Florian Diesch
        Jan 22 '14 at 18:25




        7




        7





        I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

        – geirha
        Jan 24 '14 at 9:35





        I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command.

        – geirha
        Jan 24 '14 at 9:35













        +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

        – jaybee
        Jul 10 '17 at 4:11





        +1 for @geirha's comment. It's a really important point. OP, would you consider editing?

        – jaybee
        Jul 10 '17 at 4:11













        8



















        Without cd... Not even once. I found two ways:





        # Save where you are and cd to other dir
        pushd .folder
        command --key
        # Get back where you were at the beginning.
        popd
        another_command --key


        and second:



        find . -maxdepth 1 -type d -name ".folder" -execdir command --key ;
        another_command --key





        share|improve this answer





























          8



















          Without cd... Not even once. I found two ways:





          # Save where you are and cd to other dir
          pushd .folder
          command --key
          # Get back where you were at the beginning.
          popd
          another_command --key


          and second:



          find . -maxdepth 1 -type d -name ".folder" -execdir command --key ;
          another_command --key





          share|improve this answer



























            8















            8











            8









            Without cd... Not even once. I found two ways:





            # Save where you are and cd to other dir
            pushd .folder
            command --key
            # Get back where you were at the beginning.
            popd
            another_command --key


            and second:



            find . -maxdepth 1 -type d -name ".folder" -execdir command --key ;
            another_command --key





            share|improve this answer














            Without cd... Not even once. I found two ways:





            # Save where you are and cd to other dir
            pushd .folder
            command --key
            # Get back where you were at the beginning.
            popd
            another_command --key


            and second:



            find . -maxdepth 1 -type d -name ".folder" -execdir command --key ;
            another_command --key






            share|improve this answer













            share|improve this answer




            share|improve this answer










            answered Jan 22 '14 at 16:42









            Radu RădeanuRadu Rădeanu

            132k37 gold badges273 silver badges342 bronze badges




            132k37 gold badges273 silver badges342 bronze badges
























                1



















                A simple bash function for running a command in specific directory:



                # Run a command in specific directory
                run_within_dir()
                target_dir="$1"
                previous_dir=$(pwd)
                shift
                cd $target_dir && "$@"
                cd $previous_dir



                Usage:



                $ cd ~
                $ run_within_dir /tmp ls -l # change into `/tmp` dir before running `ls -al`
                $ pwd # still at home dir





                share|improve this answer





























                  1



















                  A simple bash function for running a command in specific directory:



                  # Run a command in specific directory
                  run_within_dir()
                  target_dir="$1"
                  previous_dir=$(pwd)
                  shift
                  cd $target_dir && "$@"
                  cd $previous_dir



                  Usage:



                  $ cd ~
                  $ run_within_dir /tmp ls -l # change into `/tmp` dir before running `ls -al`
                  $ pwd # still at home dir





                  share|improve this answer



























                    1















                    1











                    1









                    A simple bash function for running a command in specific directory:



                    # Run a command in specific directory
                    run_within_dir()
                    target_dir="$1"
                    previous_dir=$(pwd)
                    shift
                    cd $target_dir && "$@"
                    cd $previous_dir



                    Usage:



                    $ cd ~
                    $ run_within_dir /tmp ls -l # change into `/tmp` dir before running `ls -al`
                    $ pwd # still at home dir





                    share|improve this answer














                    A simple bash function for running a command in specific directory:



                    # Run a command in specific directory
                    run_within_dir()
                    target_dir="$1"
                    previous_dir=$(pwd)
                    shift
                    cd $target_dir && "$@"
                    cd $previous_dir



                    Usage:



                    $ cd ~
                    $ run_within_dir /tmp ls -l # change into `/tmp` dir before running `ls -al`
                    $ pwd # still at home dir






                    share|improve this answer













                    share|improve this answer




                    share|improve this answer










                    answered Sep 27 at 2:01









                    wonderwonder

                    1111 bronze badge




                    1111 bronze badge
























                        0



















                        I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:



                        #include <unistd.h>
                        #include <stdio.h>
                        #include <stdlib.h>

                        char ENV_PATH[8192] = "PWD=";

                        int main(int argc, char** argv)
                        if(argc < 3)
                        fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]n");
                        return 1;


                        if(chdir(argv[1]))
                        fprintf(stderr, "Error setting working directory to "%s"n", argv[1]);
                        return 2;


                        if(!getcwd(ENV_PATH + 4, 8192-4))
                        fprintf(stderr, "Error getting the full path to the working directory "%s"n", argv[1]);
                        return 3;


                        if(putenv(ENV_PATH))
                        fprintf(stderr, "Error setting the environment variable "%s"n", ENV_PATH);
                        return 4;


                        execvp(argv[2], argv+2);



                        The usage is like this:



                        $ in /path/to/directory command --key





                        share|improve this answer





























                          0



















                          I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:



                          #include <unistd.h>
                          #include <stdio.h>
                          #include <stdlib.h>

                          char ENV_PATH[8192] = "PWD=";

                          int main(int argc, char** argv)
                          if(argc < 3)
                          fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]n");
                          return 1;


                          if(chdir(argv[1]))
                          fprintf(stderr, "Error setting working directory to "%s"n", argv[1]);
                          return 2;


                          if(!getcwd(ENV_PATH + 4, 8192-4))
                          fprintf(stderr, "Error getting the full path to the working directory "%s"n", argv[1]);
                          return 3;


                          if(putenv(ENV_PATH))
                          fprintf(stderr, "Error setting the environment variable "%s"n", ENV_PATH);
                          return 4;


                          execvp(argv[2], argv+2);



                          The usage is like this:



                          $ in /path/to/directory command --key





                          share|improve this answer



























                            0















                            0











                            0









                            I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:



                            #include <unistd.h>
                            #include <stdio.h>
                            #include <stdlib.h>

                            char ENV_PATH[8192] = "PWD=";

                            int main(int argc, char** argv)
                            if(argc < 3)
                            fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]n");
                            return 1;


                            if(chdir(argv[1]))
                            fprintf(stderr, "Error setting working directory to "%s"n", argv[1]);
                            return 2;


                            if(!getcwd(ENV_PATH + 4, 8192-4))
                            fprintf(stderr, "Error getting the full path to the working directory "%s"n", argv[1]);
                            return 3;


                            if(putenv(ENV_PATH))
                            fprintf(stderr, "Error setting the environment variable "%s"n", ENV_PATH);
                            return 4;


                            execvp(argv[2], argv+2);



                            The usage is like this:



                            $ in /path/to/directory command --key





                            share|improve this answer














                            I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:



                            #include <unistd.h>
                            #include <stdio.h>
                            #include <stdlib.h>

                            char ENV_PATH[8192] = "PWD=";

                            int main(int argc, char** argv)
                            if(argc < 3)
                            fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]n");
                            return 1;


                            if(chdir(argv[1]))
                            fprintf(stderr, "Error setting working directory to "%s"n", argv[1]);
                            return 2;


                            if(!getcwd(ENV_PATH + 4, 8192-4))
                            fprintf(stderr, "Error getting the full path to the working directory "%s"n", argv[1]);
                            return 3;


                            if(putenv(ENV_PATH))
                            fprintf(stderr, "Error setting the environment variable "%s"n", ENV_PATH);
                            return 4;


                            execvp(argv[2], argv+2);



                            The usage is like this:



                            $ in /path/to/directory command --key






                            share|improve this answer













                            share|improve this answer




                            share|improve this answer










                            answered Aug 15 '18 at 2:32









                            LucretielLucretiel

                            101




                            101































                                draft saved

                                draft discarded















































                                Thanks for contributing an answer to Ask Ubuntu!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f409244%2fhow-can-i-run-command-in-a-folder-without-changing-my-current-directory-to-it%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown









                                Popular posts from this blog

                                Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

                                How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

                                Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü