180

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
  • 1
    It may be a duplicate, but this question is formulated much more straight-forward. +1 Commented Feb 9, 2022 at 15:49

3 Answers 3

347

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
    Commented Nov 7, 2017 at 1:45
61

To create multiple sub-folders

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

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.

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

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