Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix command to create directory and go into it [closed]

Generally, I've to hit two commands:

$ mkdir dir_name
$ cd dir_name

to create directory and go to it.

Is there single command by using which we can achieve above?

like image 260
Alpha Avatar asked Oct 05 '13 12:10

Alpha


People also ask

How do I go back to current directory in Unix?

To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

How do you close a directory in Unix?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

What is the Unix command to create a directory?

The mkdir command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.” With mkdir , you can also set permissions, create multiple directories (folders) at once, and much more.

How do I jump to a directory in Linux?

Navigate directories. Open a window, double-click on a folder, and then double-click on a sub-folder. Use the Back button to backtrack. The cd (change directory) command moves you into a different directory.


4 Answers

You can add a function in your .bash_profile:

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

And use it like:

mkdircd test_folder
like image 61
m0nhawk Avatar answered Sep 30 '22 20:09

m0nhawk


You can combine them both into a single command:

$ mkdir dir_name && cd dir_name

Note that the second half will run only if the first half succeeds. That is, if your directory already exists, it will not change directories.

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

$ mkdir dir_name; cd dir_name
like image 32
AbdullahC Avatar answered Sep 30 '22 19:09

AbdullahC


You can try like this:-

$ mkdir dir_name && cd dir_name
like image 43
Rahul Tripathi Avatar answered Sep 30 '22 19:09

Rahul Tripathi


Yes you can do it like this

$ mkdir dir_name && cd dir_name

The shell will interpret && as a logical AND. When using && the second command is executed only if the first one succeeds (returns a zero exit status).

like image 42
meda Avatar answered Sep 30 '22 19:09

meda