Making multiple directories with mkdir -p
,I often have to create a handful of directories under one root directory. mkdir
can take multiple arguments, of course, so one can do mkdir -p foo/bar foo/baz
or mkdir foo !#:1/bar !#:1/baz
(the latter, of course, would make more sense given a root directory with a longer name than ‘foo’). But a little trick that I feel slips past a lot of people is to use ..
directory traversal to knock out a bunch of directories all in one pass. Since -p
just makes whatever it needs to, and doesn’t care about whether or not any part of the directory you’re passing exists, mkdir -p foo/bar/../baz
works to create foo/bar
and foo/baz
. This works for more complex structures as well, such as…
% mkdir -p top/mid-1/../mid-2/bottom-2/../../mid-3/bottom-3
% tree
.
└── top
├── mid-1
├── mid-2
│ └── bottom-2
└── mid-3
└── bottom-3