Unix command to create and navigate to a directory

As a rule, I have to press two commands:

$ mkdir dir_name
$ cd dir_name

to create a directory and navigate to it.

Is there one team using which we can achieve higher?

+4
source share
5 answers

You can combine them into one command:

$ mkdir dir_name && cd dir_name

Please note that the second half will only work if the first half is successful. That is, if your directory already exists, it will not change directories.

If you want to change the directory independently, use a semicolon instead:

$ mkdir dir_name; cd dir_name
+3
source

You can add a function to your .bash_profile:

function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }

And use it like:

mkdircd test_folder
+4
source

: -

$ mkdir dir_name && cd dir_name
+2

, :

$ mkdir dir_name && cd dir_name

The shell interprets &&as logical AND. When using, the &&second command is executed only if the first is successful (returns a zero exit status).

+2
source

Two more commands, but if you just don't want to enter the directory name twice, you can do this:

$ mkdir name
$ cd !$
+2
source

All Articles