How to mkdir and switch to new directory in one line
Asked Answered
C

8

11

I have been using the linux console for some time now. One thing that irritates me is that every time I create a new directory using mkdir I have to cd to change to it. Is there a single command solution to create and switch to the directory just created?

Right now I do:

mkdir php5
cd php5

can I do:

mkdir -someswitch php5

I want something simple and clean. A good example is git branch somebranch which makes new branch and git checkout -b somebranch which makes and switches to new branch.

Crozier answered 28/4, 2013 at 9:52 Comment(2)
Interesting, but I doubt there is. I don't see anything on man mkdir or mkdir --help.Upstanding
You could write a simple shell script to do that.Covering
V
8

The portable way to do this is with a shell function--not a bash function (using bashims like function). Put this in the relevant .profile for interactive use:

 mkdir () {
    case $1 in
       (-c) command mkdir -p "$2" && cd "$2";;
       (*)  command mkdir "$@";;
    esac
 }

This adds the -c option to mkdir for interactive use. Without -c the utility acts as it always does.- And note the quoting of "$2" so this works with directories with white space in their name.

Valaree answered 28/4, 2013 at 10:15 Comment(4)
But wouldn't it call itself recursively ad infinitum?Bohannan
@Bohannan No, due to the command, which is the POSIX way to express "I want to execute the simple command of that name." This skips any alias or function of the same name, should one be defined. See also pubs.opengroup.org/onlinepubs/9699919799/utilities/command.htmlValaree
What is the distinction between a "shell function" and a "bash function"?Botheration
@WilliamPursell A bash function uses bashisms, like the function keyword used in another answer, while a shell function does not.Valaree
W
11

Found some magic:

mkdir foo && cd $_
Winifred answered 23/11, 2020 at 15:25 Comment(1)
this should be on topMultifoliate
V
8

The portable way to do this is with a shell function--not a bash function (using bashims like function). Put this in the relevant .profile for interactive use:

 mkdir () {
    case $1 in
       (-c) command mkdir -p "$2" && cd "$2";;
       (*)  command mkdir "$@";;
    esac
 }

This adds the -c option to mkdir for interactive use. Without -c the utility acts as it always does.- And note the quoting of "$2" so this works with directories with white space in their name.

Valaree answered 28/4, 2013 at 10:15 Comment(4)
But wouldn't it call itself recursively ad infinitum?Bohannan
@Bohannan No, due to the command, which is the POSIX way to express "I want to execute the simple command of that name." This skips any alias or function of the same name, should one be defined. See also pubs.opengroup.org/onlinepubs/9699919799/utilities/command.htmlValaree
What is the distinction between a "shell function" and a "bash function"?Botheration
@WilliamPursell A bash function uses bashisms, like the function keyword used in another answer, while a shell function does not.Valaree
N
3

nothing prevents you from creating your own alias or small script

mkdir $1 && cd $1
Nodababus answered 28/4, 2013 at 9:56 Comment(2)
Well, a small script is not as easy as it seems. Upon exiting the script, the PWD would be restored.Vardon
mkdir -p is much better.Candelabra
A
3

Or you can use ';' to separate commands, like:

mkdir php5.3 ; cd php5.3

Anthea answered 28/4, 2013 at 10:1 Comment(1)
This will give you two errors if creating the directory fails.Walkthrough
C
1

Shortest and fastest :

cd (mkdir your_folder) 
Coopt answered 5/4 at 8:21 Comment(0)
P
0

this is what I've been using, not sure if it's in best practices but it works.

function mk() {
    $file = $args[0]
    mkdir $file | cd
}
Permanency answered 6/5, 2021 at 15:15 Comment(0)
W
-1

You can define a function in bash to do this:

function mkcd() {
    mkdir $1 && eval cd $1
}
Walkthrough answered 28/4, 2013 at 9:56 Comment(3)
It prevents expansion of the argument before it's passed to cd. This will allow it to work with directory names that contain weird characters.Walkthrough
But $1 is expanded before eval is executed. This can't possible do what you think it does.Valaree
@LarsKotthoff: Sorry, I don't get it. IMO, for that you should use quotes: mkdir "$1" && cd "$1". If something, eval will make impossible to cd into directories with $ in their name.Bohannan
F
-1

A close answer that can help. It is easy to type.

 mkdir php7 && cd !$ 
Fluffy answered 14/3, 2017 at 19:37 Comment(1)
I wonder why this is not working on my shell. I have a bash shell though.Calk

© 2022 - 2024 — McMap. All rights reserved.