0

Quick question:

Is it possible to use "mkdir" to make a new directory AND change to that directory at the same time using a single 'mkdir' command?

Whole question:

I have this question:

What single Linux “mkdir” command could replace the sequence of commands?
         mkdir a
         cd a
         mkdir b
         cd b
         mkdir c
         cd ../..

My answer is:

         mkdir a b c && cd c

Is there a single "mkdir" command, without using any other commands, perhaps with some flags or something, I can use to make AND change directory at the same time?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Matt
  • 365
  • 1
  • 5
  • 10
  • And https://unix.stackexchange.com/questions/125385/combined-mkdir-and-cd ... – muru Nov 22 '18 at 07:05
  • 1
    Your question title and your question body don't match.  The question body says (paraphrased) can I combine these three `mkdir` commands into a single `mkdir` command? If you understood the list of six commands (which, apparently, you don't), you would see that you end up in the top-level directory where you started. – G-Man Says 'Reinstate Monica' Nov 22 '18 at 07:41
  • I believe that this question has been misinterpreted and closed incorrectly.  (OK, it probably is a duplicate, but not of any of the questions linked here.) [Filipe Brandenburger's answer](https://unix.stackexchange.com/q/483369/80216#483371) is the right answer to *this* question. – G-Man Says 'Reinstate Monica' Nov 22 '18 at 07:45
  • @G-Man dupe of https://unix.stackexchange.com/q/84191/ or https://unix.stackexchange.com/questions/49263/recursive-mkdir then? – muru Nov 22 '18 at 07:56
  • @Stephen can you edit the duplicates list to include one of these two as well? – muru Nov 22 '18 at 07:59
  • Have you tested your answer? Does it work? – ctrl-alt-delor Nov 22 '18 at 10:10
  • Yeah, I misread the question completely and misunderstood the ../.., thanks anyway! – Matt Nov 22 '18 at 10:20

2 Answers2

2

The question you present of using a single mkdir command to do the same as the other steps doesn't really involve changing directories. It ends with cd ../.. which brings you back to the directory you were in at the start.

In effect, that sequence of commands creates a directory a, then a directory b within it (in other words, a/b), then a directory c within the just created b (in other words, a/b/c.)

You can do the same with a single mkdir command that creates the nested directories after creating their parents:

mkdir a a/b a/b/c

Another way is using mkdir's -p option, which will create the parent directories if necessary, so you don't need to specify them:

mkdir -p a/b/c

This doesn't answer your question in the title (for mkdir + cd look at the duplicates from the comments), but addresses the question in your text, about the equivalent single mkdir command for that sequence, in which at the end of the sequence the directory is the same as at the start of it.

filbranden
  • 21,113
  • 3
  • 58
  • 84
1

Do it as a function:

mkcdir ()
{
    mkdir -p -- "$1" &&
      cd -P -- "$1"
}
Michael Prokopec
  • 2,202
  • 7
  • 21