Make directory and open it

How to

This article describe simpler shorter way how to safe time when you need very often make and open directory.

The chdir syscall (cd bash builtin) should run in the interactive shell not in a child process of that shell, so you cant do it by one command. But there are possibilities how to do it by workaround.

Directly from command line.

This first way you can do in command line, there is no new shell script, function or new command. Them magic of this way is in $_ operator which is most recent parameter, it means that it is variable for directory name we created.

system:/tmp # mkdir directory && cd $_
system:/tmp/directory #

As simple one command.

In case you want to do this by one command you must define a function for that purpose in standard personal initialization file like $HOME/.bashrc e.g.

function mkcd () {
     mkdir -p "$1" && cd "$1"
}

 

After this, you can call command mkcd directly from command line.

system:/tmp # mkcd directory
system:/tmp/directory #

 

Leave a Reply