161

Why I can't do something like this? mkdir folder/subfolder/ in order to achive this I have to do:

mkdir folder
cd folder
mkdir subfolder

Is there a better way to do it?

1
  • It may be a duplicate, but this question is formulated much more straight-forward. +1 Feb 9, 2022 at 15:49

3 Answers 3

317

You can:

mkdir -p folder/subfolder

The -p flag causes any parent directories to be created if necessary.

1
  • 2
    your folder and subfolder name can be a variable name as well. For example $data_dir='data' and $sub='subdir'. Then we can create directories like mkdir -p $data_dir/$sub
    – Abu Shoeb
    Nov 7, 2017 at 1:45
55

To create multiple sub-folders

mkdir -p parentfolder/{subfolder1,subfolder2,subfolder3}
31

FWIW,

Poor mans security folder (to protect a public shared folder from little prying eyes ;) )

mkdir -p {0..9}/{0..9}/{0..9}/{0..9}

Now you can put your files in a pin numbered folder. Not exactly waterproof, but it's a barrier for the youngest.

3
  • 10
    lol that's cute
    – kpie
    Apr 18, 2018 at 5:38
  • 5
    I upvote this because it made my day (lol) and also learned me something new, thanks :D
    – crgarridos
    Feb 11, 2019 at 17:48
  • 5
    So nice! But a bash rich man can break it by using a command like du -h --max-depth=4 | sort -hr!
    – faghani
    Jul 17, 2019 at 7:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.