How to SSH into a specific directory? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionProblem with colons in ssh commandDifference between Login Shell and Non-Login Shell?Remote command in ssh config filecan't get gnome-session working over SSHCan I recursively print all current remote shells?SSH tunnel through middleman server - how to connect in one step (using key pair)?How to securely allow scp, but not sshDifferent ssh login directory from user homeSSH to 2 local machines via Unix serverSpecific user's SSH-RSA with root access (via AWS EC2)Automatically entering ssh password from Busybox terminalCan't SSH into localhostSSH public key exchange

How to tell that you are a giant?

Overriding an object in memory with placement new

Why light coming from distant stars is not discreet?

Using et al. for a last / senior author rather than for a first author

Identify plant with long narrow paired leaves and reddish stems

What's the purpose of writing one's academic biography in the third person?

What LEGO pieces have "real-world" functionality?

How can I make names more distinctive without making them longer?

Should I use a zero-interest credit card for a large one-time purchase?

Error "illegal generic type for instanceof" when using local classes

List *all* the tuples!

What does "fit" mean in this sentence?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

How do I keep my slimes from escaping their pens?

What is the role of the transistor and diode in a soft start circuit?

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

How to react to hostile behavior from a senior developer?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

Why aren't air breathing engines used as small first stages

What exactly is a "Meth" in Altered Carbon?

What would be the ideal power source for a cybernetic eye?

How to run gsettings for another user Ubuntu 18.04.2 LTS

How to bypass password on Windows XP account?



How to SSH into a specific directory?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionProblem with colons in ssh commandDifference between Login Shell and Non-Login Shell?Remote command in ssh config filecan't get gnome-session working over SSHCan I recursively print all current remote shells?SSH tunnel through middleman server - how to connect in one step (using key pair)?How to securely allow scp, but not sshDifferent ssh login directory from user homeSSH to 2 local machines via Unix serverSpecific user's SSH-RSA with root access (via AWS EC2)Automatically entering ssh password from Busybox terminalCan't SSH into localhostSSH public key exchange



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








35















I frequently login to a server, then cd into a specific directory. Is it possible to simplify these two commands into one?



ssh bob@foo 
cd /home/guest


I'd like to avoid changing anything on 'foo' if possible, as I'll have to clear it with the server administrator. I use bash, but I am open to answers in other shells.










share|improve this question






























    35















    I frequently login to a server, then cd into a specific directory. Is it possible to simplify these two commands into one?



    ssh bob@foo 
    cd /home/guest


    I'd like to avoid changing anything on 'foo' if possible, as I'll have to clear it with the server administrator. I use bash, but I am open to answers in other shells.










    share|improve this question


























      35












      35








      35


      11






      I frequently login to a server, then cd into a specific directory. Is it possible to simplify these two commands into one?



      ssh bob@foo 
      cd /home/guest


      I'd like to avoid changing anything on 'foo' if possible, as I'll have to clear it with the server administrator. I use bash, but I am open to answers in other shells.










      share|improve this question
















      I frequently login to a server, then cd into a specific directory. Is it possible to simplify these two commands into one?



      ssh bob@foo 
      cd /home/guest


      I'd like to avoid changing anything on 'foo' if possible, as I'll have to clear it with the server administrator. I use bash, but I am open to answers in other shells.







      bash ssh






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 16 '13 at 6:19







      spuder

















      asked Aug 15 '13 at 23:04









      spuderspuder

      6,8282671106




      6,8282671106




















          3 Answers
          3






          active

          oldest

          votes


















          18














          Just put as the last line of your ~bob/.bash_profile file on foo:



          cd /home/guest >& /dev/null


          Now each time you log in (whether by SSH or otherwise), the cd command will run. No mucking around with ssh is necessary.



          I know you wrote that you'd "like to avoid changing anything on 'foo' if possible," but if the bob@foo account is yours, changing your own .bash_profile should be acceptable, no?






          share|improve this answer


















          • 2





            Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

            – spuder
            Aug 16 '13 at 1:41






          • 5





            I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

            – konsolebox
            Aug 16 '13 at 4:54






          • 3





            >& and &> are the same in Bash. The latter style is preferred though.

            – user26112
            Aug 16 '13 at 11:16











          • Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

            – DanB
            Sep 11 '13 at 1:40


















          39














          This works with OpenSSH:



          ssh -t bob@foo 'cd /home/guest && exec bash -l'


          The last argument runs in your login shell. The -t flag passed to ssh forces ssh to allocate a pseudo-terminal, which is necessary for an interactive shell. The -l flag passed to bash starts bash as a login shell.






          share|improve this answer




















          • 3





            I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

            – konsolebox
            Aug 16 '13 at 4:55






          • 1





            @konsolebox: That's a good idea. I have added that to my answer.

            – user26112
            Aug 16 '13 at 11:14


















          15














          You can also do it this way, similar to @EvanTeitelman's solution:



          $ ssh -t bob@foo "cd /tmp ; bash"


          Or if you don't know the shell on the other end:



          $ ssh -t bob@foo "cd /tmp && exec $SHELL"


          Or like this:



          $ ssh -t bob@foo 'cd /tmp && exec $SHELL'





          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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/3.0/"u003ecc by-sa 3.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%2funix.stackexchange.com%2fquestions%2f86941%2fhow-to-ssh-into-a-specific-directory%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            18














            Just put as the last line of your ~bob/.bash_profile file on foo:



            cd /home/guest >& /dev/null


            Now each time you log in (whether by SSH or otherwise), the cd command will run. No mucking around with ssh is necessary.



            I know you wrote that you'd "like to avoid changing anything on 'foo' if possible," but if the bob@foo account is yours, changing your own .bash_profile should be acceptable, no?






            share|improve this answer


















            • 2





              Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

              – spuder
              Aug 16 '13 at 1:41






            • 5





              I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

              – konsolebox
              Aug 16 '13 at 4:54






            • 3





              >& and &> are the same in Bash. The latter style is preferred though.

              – user26112
              Aug 16 '13 at 11:16











            • Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

              – DanB
              Sep 11 '13 at 1:40















            18














            Just put as the last line of your ~bob/.bash_profile file on foo:



            cd /home/guest >& /dev/null


            Now each time you log in (whether by SSH or otherwise), the cd command will run. No mucking around with ssh is necessary.



            I know you wrote that you'd "like to avoid changing anything on 'foo' if possible," but if the bob@foo account is yours, changing your own .bash_profile should be acceptable, no?






            share|improve this answer


















            • 2





              Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

              – spuder
              Aug 16 '13 at 1:41






            • 5





              I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

              – konsolebox
              Aug 16 '13 at 4:54






            • 3





              >& and &> are the same in Bash. The latter style is preferred though.

              – user26112
              Aug 16 '13 at 11:16











            • Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

              – DanB
              Sep 11 '13 at 1:40













            18












            18








            18







            Just put as the last line of your ~bob/.bash_profile file on foo:



            cd /home/guest >& /dev/null


            Now each time you log in (whether by SSH or otherwise), the cd command will run. No mucking around with ssh is necessary.



            I know you wrote that you'd "like to avoid changing anything on 'foo' if possible," but if the bob@foo account is yours, changing your own .bash_profile should be acceptable, no?






            share|improve this answer













            Just put as the last line of your ~bob/.bash_profile file on foo:



            cd /home/guest >& /dev/null


            Now each time you log in (whether by SSH or otherwise), the cd command will run. No mucking around with ssh is necessary.



            I know you wrote that you'd "like to avoid changing anything on 'foo' if possible," but if the bob@foo account is yours, changing your own .bash_profile should be acceptable, no?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 16 '13 at 1:36









            DanBDanB

            29614




            29614







            • 2





              Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

              – spuder
              Aug 16 '13 at 1:41






            • 5





              I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

              – konsolebox
              Aug 16 '13 at 4:54






            • 3





              >& and &> are the same in Bash. The latter style is preferred though.

              – user26112
              Aug 16 '13 at 11:16











            • Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

              – DanB
              Sep 11 '13 at 1:40












            • 2





              Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

              – spuder
              Aug 16 '13 at 1:41






            • 5





              I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

              – konsolebox
              Aug 16 '13 at 4:54






            • 3





              >& and &> are the same in Bash. The latter style is preferred though.

              – user26112
              Aug 16 '13 at 11:16











            • Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

              – DanB
              Sep 11 '13 at 1:40







            2




            2





            Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

            – spuder
            Aug 16 '13 at 1:41





            Why yes, I think this will work great. I won't need to get permission from IT to make that change. Could you elaborate on why you need >& /dev/null ?

            – spuder
            Aug 16 '13 at 1:41




            5




            5





            I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

            – konsolebox
            Aug 16 '13 at 4:54





            I think that should be &> /dev/null. It prevents any error message that may be shown just in case cd fails to change directory to /home/directory. If you want to see those messages you could just exclude that.

            – konsolebox
            Aug 16 '13 at 4:54




            3




            3





            >& and &> are the same in Bash. The latter style is preferred though.

            – user26112
            Aug 16 '13 at 11:16





            >& and &> are the same in Bash. The latter style is preferred though.

            – user26112
            Aug 16 '13 at 11:16













            Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

            – DanB
            Sep 11 '13 at 1:40





            Additionally, the redirection to /dev/null prevents the name of the directory from being echoed onscreen, which "cd" may do.

            – DanB
            Sep 11 '13 at 1:40













            39














            This works with OpenSSH:



            ssh -t bob@foo 'cd /home/guest && exec bash -l'


            The last argument runs in your login shell. The -t flag passed to ssh forces ssh to allocate a pseudo-terminal, which is necessary for an interactive shell. The -l flag passed to bash starts bash as a login shell.






            share|improve this answer




















            • 3





              I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

              – konsolebox
              Aug 16 '13 at 4:55






            • 1





              @konsolebox: That's a good idea. I have added that to my answer.

              – user26112
              Aug 16 '13 at 11:14















            39














            This works with OpenSSH:



            ssh -t bob@foo 'cd /home/guest && exec bash -l'


            The last argument runs in your login shell. The -t flag passed to ssh forces ssh to allocate a pseudo-terminal, which is necessary for an interactive shell. The -l flag passed to bash starts bash as a login shell.






            share|improve this answer




















            • 3





              I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

              – konsolebox
              Aug 16 '13 at 4:55






            • 1





              @konsolebox: That's a good idea. I have added that to my answer.

              – user26112
              Aug 16 '13 at 11:14













            39












            39








            39







            This works with OpenSSH:



            ssh -t bob@foo 'cd /home/guest && exec bash -l'


            The last argument runs in your login shell. The -t flag passed to ssh forces ssh to allocate a pseudo-terminal, which is necessary for an interactive shell. The -l flag passed to bash starts bash as a login shell.






            share|improve this answer















            This works with OpenSSH:



            ssh -t bob@foo 'cd /home/guest && exec bash -l'


            The last argument runs in your login shell. The -t flag passed to ssh forces ssh to allocate a pseudo-terminal, which is necessary for an interactive shell. The -l flag passed to bash starts bash as a login shell.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:36









            Community

            1




            1










            answered Aug 15 '13 at 23:17







            user26112














            • 3





              I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

              – konsolebox
              Aug 16 '13 at 4:55






            • 1





              @konsolebox: That's a good idea. I have added that to my answer.

              – user26112
              Aug 16 '13 at 11:14












            • 3





              I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

              – konsolebox
              Aug 16 '13 at 4:55






            • 1





              @konsolebox: That's a good idea. I have added that to my answer.

              – user26112
              Aug 16 '13 at 11:14







            3




            3





            I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

            – konsolebox
            Aug 16 '13 at 4:55





            I wonder, would adding -l option to bash be helpful as well? ... && exec bash -l

            – konsolebox
            Aug 16 '13 at 4:55




            1




            1





            @konsolebox: That's a good idea. I have added that to my answer.

            – user26112
            Aug 16 '13 at 11:14





            @konsolebox: That's a good idea. I have added that to my answer.

            – user26112
            Aug 16 '13 at 11:14











            15














            You can also do it this way, similar to @EvanTeitelman's solution:



            $ ssh -t bob@foo "cd /tmp ; bash"


            Or if you don't know the shell on the other end:



            $ ssh -t bob@foo "cd /tmp && exec $SHELL"


            Or like this:



            $ ssh -t bob@foo 'cd /tmp && exec $SHELL'





            share|improve this answer



























              15














              You can also do it this way, similar to @EvanTeitelman's solution:



              $ ssh -t bob@foo "cd /tmp ; bash"


              Or if you don't know the shell on the other end:



              $ ssh -t bob@foo "cd /tmp && exec $SHELL"


              Or like this:



              $ ssh -t bob@foo 'cd /tmp && exec $SHELL'





              share|improve this answer

























                15












                15








                15







                You can also do it this way, similar to @EvanTeitelman's solution:



                $ ssh -t bob@foo "cd /tmp ; bash"


                Or if you don't know the shell on the other end:



                $ ssh -t bob@foo "cd /tmp && exec $SHELL"


                Or like this:



                $ ssh -t bob@foo 'cd /tmp && exec $SHELL'





                share|improve this answer













                You can also do it this way, similar to @EvanTeitelman's solution:



                $ ssh -t bob@foo "cd /tmp ; bash"


                Or if you don't know the shell on the other end:



                $ ssh -t bob@foo "cd /tmp && exec $SHELL"


                Or like this:



                $ ssh -t bob@foo 'cd /tmp && exec $SHELL'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 16 '13 at 0:24









                slmslm

                256k71544690




                256k71544690



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f86941%2fhow-to-ssh-into-a-specific-directory%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







                    -bash, ssh

                    Popular posts from this blog

                    Frič See also Navigation menuinternal link

                    Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                    fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox