Is there a shortcut to mkdir foo and immediately cd into it?












83














This is something I do frequently



$ mkdir foo
$ cd foo


This works as a single command, but it's more keystrokes and saves no time.



$ mkdir foo && cd foo


Is there a shortcut for this?



Edit



With the use of help below, this seems to be the most elegant answer.



# ~/.bashrc
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "`$1' already exists"
else
mkdir $1 && cd $1
fi
}









share|improve this question




















  • 1




    You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
    – Andy
    Jun 15 '10 at 14:50






  • 2




    (1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
    – quack quixote
    Jun 15 '10 at 16:25


















83














This is something I do frequently



$ mkdir foo
$ cd foo


This works as a single command, but it's more keystrokes and saves no time.



$ mkdir foo && cd foo


Is there a shortcut for this?



Edit



With the use of help below, this seems to be the most elegant answer.



# ~/.bashrc
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "`$1' already exists"
else
mkdir $1 && cd $1
fi
}









share|improve this question




















  • 1




    You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
    – Andy
    Jun 15 '10 at 14:50






  • 2




    (1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
    – quack quixote
    Jun 15 '10 at 16:25
















83












83








83


24





This is something I do frequently



$ mkdir foo
$ cd foo


This works as a single command, but it's more keystrokes and saves no time.



$ mkdir foo && cd foo


Is there a shortcut for this?



Edit



With the use of help below, this seems to be the most elegant answer.



# ~/.bashrc
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "`$1' already exists"
else
mkdir $1 && cd $1
fi
}









share|improve this question















This is something I do frequently



$ mkdir foo
$ cd foo


This works as a single command, but it's more keystrokes and saves no time.



$ mkdir foo && cd foo


Is there a shortcut for this?



Edit



With the use of help below, this seems to be the most elegant answer.



# ~/.bashrc
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "`$1' already exists"
else
mkdir $1 && cd $1
fi
}






command-line bash terminal shortcuts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 10 '14 at 22:59









gparyani

1,28472141




1,28472141










asked Jun 15 '10 at 14:22









macek

2,507143650




2,507143650








  • 1




    You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
    – Andy
    Jun 15 '10 at 14:50






  • 2




    (1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
    – quack quixote
    Jun 15 '10 at 16:25
















  • 1




    You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
    – Andy
    Jun 15 '10 at 14:50






  • 2




    (1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
    – quack quixote
    Jun 15 '10 at 16:25










1




1




You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
– Andy
Jun 15 '10 at 14:50




You can rename the function to mkdir if you use command mkdir $1 instead of just mkdir $1 in the function body.
– Andy
Jun 15 '10 at 14:50




2




2




(1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
– quack quixote
Jun 15 '10 at 16:25






(1) why not simply "mkdir $1 ; cd $1" instead of "&&"? that way the "cd" succeeds even if the "mkdir" fails, and you don't need the does-it-already-exist scaffolding. (2) as written your function won't work (to prompt you for a directory name). you need to put that in a separate "if" clause from the existence test (currently in "elif").
– quack quixote
Jun 15 '10 at 16:25












9 Answers
9






active

oldest

votes


















42














I'm no Linux/bash expert, but try putting this in your .bashrc.



function mkdir
{
command mkdir $1 && cd $1
}


PS Thanks to Dennis for using command mkdir.






share|improve this answer



















  • 2




    Instead of `which mkdir`, just use command mkdir.
    – Dennis Williamson
    Jun 15 '10 at 14:33








  • 1




    Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
    – Andy
    Jun 15 '10 at 14:49






  • 2




    command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
    – grawity
    Jun 15 '10 at 18:39










  • That also works for zsh btw
    – Florian Loch
    Dec 23 '14 at 11:37



















91














The bash, zsh Shells



If you don't want another function to remember and don't mind bashisms:



$ mkdir /home/foo/doc/bar && cd $_


The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:



foo bar
bar baz


The fish Shell



In the fish shell, I would type the following:




> mkdir /home/foo/doc/bar
> cd alt + ↑


The alt key combined with either the up or the down arrow key will cycle through command parameter history.






share|improve this answer



















  • 4




    This works in zsh too.
    – Nabil Kadimi
    Feb 16 '14 at 10:01






  • 3




    Can you explain what $_ is? Newbie here.
    – arg20
    Jun 25 '14 at 15:16



















16














For oh-my-zsh users:
$ take 'directory_name'



Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet






share|improve this answer



















  • 1




    Can you explain this in more detail?
    – bwDraco
    Jul 13 '15 at 5:47



















10














What about:



$ mkdir newdirname; cd $_


It's a bit easier than using &&, combining quack quixote's and kzh's answers.






share|improve this answer

















  • 24




    The point of && is that cd will not be executed if the mkdir command fails
    – slhck
    Mar 14 '12 at 9:29






  • 1




    @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
    – moala
    Feb 2 '13 at 9:13



















7














You can try something like this:



#!/bin/sh
mkdir $1 && cd $1


Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.






share|improve this answer

















  • 4




    how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
    – Erik Allik
    Oct 13 '13 at 16:15



















5














$echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
$mkcd < pathtofolder/foldername >





share|improve this answer





























    1














    Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:



    function mkcd
    mkdir -pv $argv;
    cd $argv;
    end


    The -pv tag allows for the creation of directories with sub-directories.






    share|improve this answer































      0














      Depending on the desired outcome if the directory already exists.



      Fail if directory already exists



      mkcd() {
      mkdir $1 && cd $1
      }


      Change directory regardless



      mkcd() {
      mkdir $1 ; cd $1
      }


      Usage
      mkcd some/path/to/my/dir






      share|improve this answer































        0














        I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :



        function mkdir
        {
        command mkdir $1 && cd $1
        }


        So I changed it and now its working great!



        function mkcd
        {
        command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
        }





        share|improve this answer





















          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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/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%2fsuperuser.com%2fquestions%2f152794%2fis-there-a-shortcut-to-mkdir-foo-and-immediately-cd-into-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          9 Answers
          9






          active

          oldest

          votes








          9 Answers
          9






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          42














          I'm no Linux/bash expert, but try putting this in your .bashrc.



          function mkdir
          {
          command mkdir $1 && cd $1
          }


          PS Thanks to Dennis for using command mkdir.






          share|improve this answer



















          • 2




            Instead of `which mkdir`, just use command mkdir.
            – Dennis Williamson
            Jun 15 '10 at 14:33








          • 1




            Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
            – Andy
            Jun 15 '10 at 14:49






          • 2




            command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
            – grawity
            Jun 15 '10 at 18:39










          • That also works for zsh btw
            – Florian Loch
            Dec 23 '14 at 11:37
















          42














          I'm no Linux/bash expert, but try putting this in your .bashrc.



          function mkdir
          {
          command mkdir $1 && cd $1
          }


          PS Thanks to Dennis for using command mkdir.






          share|improve this answer



















          • 2




            Instead of `which mkdir`, just use command mkdir.
            – Dennis Williamson
            Jun 15 '10 at 14:33








          • 1




            Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
            – Andy
            Jun 15 '10 at 14:49






          • 2




            command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
            – grawity
            Jun 15 '10 at 18:39










          • That also works for zsh btw
            – Florian Loch
            Dec 23 '14 at 11:37














          42












          42








          42






          I'm no Linux/bash expert, but try putting this in your .bashrc.



          function mkdir
          {
          command mkdir $1 && cd $1
          }


          PS Thanks to Dennis for using command mkdir.






          share|improve this answer














          I'm no Linux/bash expert, but try putting this in your .bashrc.



          function mkdir
          {
          command mkdir $1 && cd $1
          }


          PS Thanks to Dennis for using command mkdir.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 15 '10 at 15:29

























          answered Jun 15 '10 at 14:29









          Andy

          2,46431528




          2,46431528








          • 2




            Instead of `which mkdir`, just use command mkdir.
            – Dennis Williamson
            Jun 15 '10 at 14:33








          • 1




            Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
            – Andy
            Jun 15 '10 at 14:49






          • 2




            command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
            – grawity
            Jun 15 '10 at 18:39










          • That also works for zsh btw
            – Florian Loch
            Dec 23 '14 at 11:37














          • 2




            Instead of `which mkdir`, just use command mkdir.
            – Dennis Williamson
            Jun 15 '10 at 14:33








          • 1




            Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
            – Andy
            Jun 15 '10 at 14:49






          • 2




            command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
            – grawity
            Jun 15 '10 at 18:39










          • That also works for zsh btw
            – Florian Loch
            Dec 23 '14 at 11:37








          2




          2




          Instead of `which mkdir`, just use command mkdir.
          – Dennis Williamson
          Jun 15 '10 at 14:33






          Instead of `which mkdir`, just use command mkdir.
          – Dennis Williamson
          Jun 15 '10 at 14:33






          1




          1




          Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
          – Andy
          Jun 15 '10 at 14:49




          Thanks Dennis. There's nothing under man command - could you direct me to a reference? (I can work out what it does, but it pays to be thorough;)
          – Andy
          Jun 15 '10 at 14:49




          2




          2




          command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
          – grawity
          Jun 15 '10 at 18:39




          command is described in the manual of bash (which it is a built-in of; it's not a separate command). You could also try help command.
          – grawity
          Jun 15 '10 at 18:39












          That also works for zsh btw
          – Florian Loch
          Dec 23 '14 at 11:37




          That also works for zsh btw
          – Florian Loch
          Dec 23 '14 at 11:37













          91














          The bash, zsh Shells



          If you don't want another function to remember and don't mind bashisms:



          $ mkdir /home/foo/doc/bar && cd $_


          The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:



          foo bar
          bar baz


          The fish Shell



          In the fish shell, I would type the following:




          > mkdir /home/foo/doc/bar
          > cd alt + ↑


          The alt key combined with either the up or the down arrow key will cycle through command parameter history.






          share|improve this answer



















          • 4




            This works in zsh too.
            – Nabil Kadimi
            Feb 16 '14 at 10:01






          • 3




            Can you explain what $_ is? Newbie here.
            – arg20
            Jun 25 '14 at 15:16
















          91














          The bash, zsh Shells



          If you don't want another function to remember and don't mind bashisms:



          $ mkdir /home/foo/doc/bar && cd $_


          The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:



          foo bar
          bar baz


          The fish Shell



          In the fish shell, I would type the following:




          > mkdir /home/foo/doc/bar
          > cd alt + ↑


          The alt key combined with either the up or the down arrow key will cycle through command parameter history.






          share|improve this answer



















          • 4




            This works in zsh too.
            – Nabil Kadimi
            Feb 16 '14 at 10:01






          • 3




            Can you explain what $_ is? Newbie here.
            – arg20
            Jun 25 '14 at 15:16














          91












          91








          91






          The bash, zsh Shells



          If you don't want another function to remember and don't mind bashisms:



          $ mkdir /home/foo/doc/bar && cd $_


          The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:



          foo bar
          bar baz


          The fish Shell



          In the fish shell, I would type the following:




          > mkdir /home/foo/doc/bar
          > cd alt + ↑


          The alt key combined with either the up or the down arrow key will cycle through command parameter history.






          share|improve this answer














          The bash, zsh Shells



          If you don't want another function to remember and don't mind bashisms:



          $ mkdir /home/foo/doc/bar && cd $_


          The $_ (dollar underscore) bash command variable contains the most recent parameter. So if a user were to type the following at the command line: echo foo bar && echo $_ baz, then the output would be as follows:



          foo bar
          bar baz


          The fish Shell



          In the fish shell, I would type the following:




          > mkdir /home/foo/doc/bar
          > cd alt + ↑


          The alt key combined with either the up or the down arrow key will cycle through command parameter history.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 '17 at 14:10

























          answered Aug 12 '11 at 11:26









          kzh

          2,22362231




          2,22362231








          • 4




            This works in zsh too.
            – Nabil Kadimi
            Feb 16 '14 at 10:01






          • 3




            Can you explain what $_ is? Newbie here.
            – arg20
            Jun 25 '14 at 15:16














          • 4




            This works in zsh too.
            – Nabil Kadimi
            Feb 16 '14 at 10:01






          • 3




            Can you explain what $_ is? Newbie here.
            – arg20
            Jun 25 '14 at 15:16








          4




          4




          This works in zsh too.
          – Nabil Kadimi
          Feb 16 '14 at 10:01




          This works in zsh too.
          – Nabil Kadimi
          Feb 16 '14 at 10:01




          3




          3




          Can you explain what $_ is? Newbie here.
          – arg20
          Jun 25 '14 at 15:16




          Can you explain what $_ is? Newbie here.
          – arg20
          Jun 25 '14 at 15:16











          16














          For oh-my-zsh users:
          $ take 'directory_name'



          Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet






          share|improve this answer



















          • 1




            Can you explain this in more detail?
            – bwDraco
            Jul 13 '15 at 5:47
















          16














          For oh-my-zsh users:
          $ take 'directory_name'



          Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet






          share|improve this answer



















          • 1




            Can you explain this in more detail?
            – bwDraco
            Jul 13 '15 at 5:47














          16












          16








          16






          For oh-my-zsh users:
          $ take 'directory_name'



          Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet






          share|improve this answer














          For oh-my-zsh users:
          $ take 'directory_name'



          Reference: https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 9 '15 at 16:22

























          answered Jul 12 '15 at 15:39









          Avijit Gupta

          26128




          26128








          • 1




            Can you explain this in more detail?
            – bwDraco
            Jul 13 '15 at 5:47














          • 1




            Can you explain this in more detail?
            – bwDraco
            Jul 13 '15 at 5:47








          1




          1




          Can you explain this in more detail?
          – bwDraco
          Jul 13 '15 at 5:47




          Can you explain this in more detail?
          – bwDraco
          Jul 13 '15 at 5:47











          10














          What about:



          $ mkdir newdirname; cd $_


          It's a bit easier than using &&, combining quack quixote's and kzh's answers.






          share|improve this answer

















          • 24




            The point of && is that cd will not be executed if the mkdir command fails
            – slhck
            Mar 14 '12 at 9:29






          • 1




            @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
            – moala
            Feb 2 '13 at 9:13
















          10














          What about:



          $ mkdir newdirname; cd $_


          It's a bit easier than using &&, combining quack quixote's and kzh's answers.






          share|improve this answer

















          • 24




            The point of && is that cd will not be executed if the mkdir command fails
            – slhck
            Mar 14 '12 at 9:29






          • 1




            @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
            – moala
            Feb 2 '13 at 9:13














          10












          10








          10






          What about:



          $ mkdir newdirname; cd $_


          It's a bit easier than using &&, combining quack quixote's and kzh's answers.






          share|improve this answer












          What about:



          $ mkdir newdirname; cd $_


          It's a bit easier than using &&, combining quack quixote's and kzh's answers.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 14 '12 at 2:09









          Alex

          10912




          10912








          • 24




            The point of && is that cd will not be executed if the mkdir command fails
            – slhck
            Mar 14 '12 at 9:29






          • 1




            @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
            – moala
            Feb 2 '13 at 9:13














          • 24




            The point of && is that cd will not be executed if the mkdir command fails
            – slhck
            Mar 14 '12 at 9:29






          • 1




            @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
            – moala
            Feb 2 '13 at 9:13








          24




          24




          The point of && is that cd will not be executed if the mkdir command fails
          – slhck
          Mar 14 '12 at 9:29




          The point of && is that cd will not be executed if the mkdir command fails
          – slhck
          Mar 14 '12 at 9:29




          1




          1




          @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
          – moala
          Feb 2 '13 at 9:13




          @slhck actually that was the point of quixote: cd even if mkdir fails (for instance if it already exists, forcing the user to write a second command to actually cd to the path). However contrarily to what this answers says, that's not for easiness: && is not more complicated than ;.
          – moala
          Feb 2 '13 at 9:13











          7














          You can try something like this:



          #!/bin/sh
          mkdir $1 && cd $1


          Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.






          share|improve this answer

















          • 4




            how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
            – Erik Allik
            Oct 13 '13 at 16:15
















          7














          You can try something like this:



          #!/bin/sh
          mkdir $1 && cd $1


          Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.






          share|improve this answer

















          • 4




            how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
            – Erik Allik
            Oct 13 '13 at 16:15














          7












          7








          7






          You can try something like this:



          #!/bin/sh
          mkdir $1 && cd $1


          Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.






          share|improve this answer












          You can try something like this:



          #!/bin/sh
          mkdir $1 && cd $1


          Save this script to some place that is in your path, for example, /usr/local/bin or ~/bin (you have to put this last one into your path in your ~/.profile file). Then you can simply call it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 15 '10 at 14:30









          petersohn

          2,18931423




          2,18931423








          • 4




            how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
            – Erik Allik
            Oct 13 '13 at 16:15














          • 4




            how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
            – Erik Allik
            Oct 13 '13 at 16:15








          4




          4




          how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
          – Erik Allik
          Oct 13 '13 at 16:15




          how can this work? it seems to only cd inside the context of the execution of the ~/bin/mkcd script, not the caller of the script.
          – Erik Allik
          Oct 13 '13 at 16:15











          5














          $echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
          $mkcd < pathtofolder/foldername >





          share|improve this answer


























            5














            $echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
            $mkcd < pathtofolder/foldername >





            share|improve this answer
























              5












              5








              5






              $echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
              $mkcd < pathtofolder/foldername >





              share|improve this answer












              $echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
              $mkcd < pathtofolder/foldername >






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 19 '13 at 6:01









              mshameers

              15111




              15111























                  1














                  Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:



                  function mkcd
                  mkdir -pv $argv;
                  cd $argv;
                  end


                  The -pv tag allows for the creation of directories with sub-directories.






                  share|improve this answer




























                    1














                    Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:



                    function mkcd
                    mkdir -pv $argv;
                    cd $argv;
                    end


                    The -pv tag allows for the creation of directories with sub-directories.






                    share|improve this answer


























                      1












                      1








                      1






                      Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:



                      function mkcd
                      mkdir -pv $argv;
                      cd $argv;
                      end


                      The -pv tag allows for the creation of directories with sub-directories.






                      share|improve this answer














                      Here is a simple function I put in my ~/.config/fish/config.fish file which accomplishes this task:



                      function mkcd
                      mkdir -pv $argv;
                      cd $argv;
                      end


                      The -pv tag allows for the creation of directories with sub-directories.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 25 '18 at 16:23

























                      answered Jul 23 '18 at 12:30









                      Alex Fenwood Hughes

                      113




                      113























                          0














                          Depending on the desired outcome if the directory already exists.



                          Fail if directory already exists



                          mkcd() {
                          mkdir $1 && cd $1
                          }


                          Change directory regardless



                          mkcd() {
                          mkdir $1 ; cd $1
                          }


                          Usage
                          mkcd some/path/to/my/dir






                          share|improve this answer




























                            0














                            Depending on the desired outcome if the directory already exists.



                            Fail if directory already exists



                            mkcd() {
                            mkdir $1 && cd $1
                            }


                            Change directory regardless



                            mkcd() {
                            mkdir $1 ; cd $1
                            }


                            Usage
                            mkcd some/path/to/my/dir






                            share|improve this answer


























                              0












                              0








                              0






                              Depending on the desired outcome if the directory already exists.



                              Fail if directory already exists



                              mkcd() {
                              mkdir $1 && cd $1
                              }


                              Change directory regardless



                              mkcd() {
                              mkdir $1 ; cd $1
                              }


                              Usage
                              mkcd some/path/to/my/dir






                              share|improve this answer














                              Depending on the desired outcome if the directory already exists.



                              Fail if directory already exists



                              mkcd() {
                              mkdir $1 && cd $1
                              }


                              Change directory regardless



                              mkcd() {
                              mkdir $1 ; cd $1
                              }


                              Usage
                              mkcd some/path/to/my/dir







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 19 '16 at 17:34









                              Olaf

                              1034




                              1034










                              answered Oct 13 '15 at 11:51









                              bingles

                              1063




                              1063























                                  0














                                  I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :



                                  function mkdir
                                  {
                                  command mkdir $1 && cd $1
                                  }


                                  So I changed it and now its working great!



                                  function mkcd
                                  {
                                  command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
                                  }





                                  share|improve this answer


























                                    0














                                    I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :



                                    function mkdir
                                    {
                                    command mkdir $1 && cd $1
                                    }


                                    So I changed it and now its working great!



                                    function mkcd
                                    {
                                    command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
                                    }





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :



                                      function mkdir
                                      {
                                      command mkdir $1 && cd $1
                                      }


                                      So I changed it and now its working great!



                                      function mkcd
                                      {
                                      command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
                                      }





                                      share|improve this answer












                                      I found that the function below can only make one directory, if I want to make subdirectories at the same time, it doesn't work :



                                      function mkdir
                                      {
                                      command mkdir $1 && cd $1
                                      }


                                      So I changed it and now its working great!



                                      function mkcd
                                      {
                                      command mkdir -pv $1 && cd $1 && echo "Now in `pwd`"
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 20 '17 at 5:32









                                      user5723841

                                      1




                                      1






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Super User!


                                          • 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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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%2fsuperuser.com%2fquestions%2f152794%2fis-there-a-shortcut-to-mkdir-foo-and-immediately-cd-into-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

                                          Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                          Aardman Animations

                                          Are they similar matrix