My Profile Photo

Rich Werden

Web Developer & Software Engineer


A site for Rich to write about code and show some completed projects for future reference...


#TIL: mkdir "-p" flag

We all know that on the terminal command-line you use mkdir to make directories. (Honestly, if ya didnโ€™t, ya should. ๐Ÿ˜‰)

I only recently found out about the -p โ€œflagโ€/modifier for mkdir with which we can make multiple *sub*-directories at once:

mkdir -p top/middle/bottom

Now I have a folder called โ€œtopโ€ which has a folder โ€œmiddleโ€ inside. โ€œmiddleโ€, has its own folder inside called โ€œbottomโ€1.

BUT WAIT THEREโ€™S MORE!โ€ฆ

With -p flag you can also make multiple directories at the *same sub-level*. Use a pair of { } and separate the names by ,. This time, say, Iโ€™d get โ€œmiddleโ€ to contain 3 directories inside instead of one - mkdir -p top/middle/{bottom1,bottom2,bottom3}


ABSURD EXAMPLE:

I already knew that you can add โ€œ โ€ between names to make multiple directories at once at the same level: mkdir dir1 dir2 dir3, but by combining all of this into an abusurd example I would never actually use, I can make a giant (completely made up nonesense) tree structure at once:

mkdir -p newProject/client/{assets/{pdfs,images},dynamic/{configs,media}} otherProject/server/{db{query,cache},app/{model,view}}

Which will make this:

/
โ”œโ”€ newProject/
โ”‚  โ””โ”€โ”€ client/
โ”‚      โ”œโ”€โ”€ assets/
โ”‚      โ”‚   โ”œโ”€โ”€ images/
โ”‚      โ”‚   โ””โ”€โ”€ pdfs/
โ”‚      โ””โ”€โ”€ dynamic/
โ”‚          โ”œโ”€โ”€ configs/
โ”‚          โ””โ”€โ”€ media/
โ””โ”€ otherProject/
   โ””โ”€ server/
      โ”œโ”€โ”€ app/
      โ”‚   โ”œโ”€โ”€ model/
      โ”‚   โ””โ”€โ”€ view/
      โ”œโ”€โ”€ dbcache/
      โ””โ”€โ”€ dbquery/

So, a random possibly useful thing to knowโ€ฆ
Cheers!

โฏ โ˜… โœŽ โœฆ โœฎ โœฏ โ‚ โฆฟ โฏ โ˜… โœŽ โœฆ โœฎ โœฏ โ‚ โฆฟ โฏ โ˜… โœŽ โœฆ โœฎ โœฏ โ‚ โฆฟ โฏ โ˜… โœŽ โœฆ

  1. Otherwise, weโ€™d do it in steps (mkdir top, then cd top, then mkdir middle, then cd middle, then mkdir bottom).ย