2.5. New Directories

2.5.1. mkdir, mkdir -p

To make a new directory, use the mkdir (make directory) command and supply a path ending with the directory you want created as a command-line argument.

cd ~/cs1302-unix
ls -l
mkdir practice
ls -l
../../_images/mkdir-first-demo.svg
cd ~/cs1302-unix
tree notes
mkdir notes/other
tree notes
../../_images/mkdir-second-demo.svg

The default behavior of mkdir requires that intermediate directories along the path already exist. If one or more directories along the path do not exist, then mkdir will emit a No such file or directory error.

cd ~/cs1302-unix
tree notes
mkdir notes/other/a/b/c
tree notes
../../_images/mkdir-third-demo.svg

When intermediate directories do not exist along the desired path, it is possible to create them one at a time and in the order that they appear along the path from beginning to end. The creators of the mkdir anticipated that such repetitive calls to their command might be undesirable, so they included a “passive” option that forces mkdir to create intermediate directories when possible. To enable the passive option, supply -p as a command-line argument to mkdir before the desired path.

cd ~/cs1302-unix
tree notes
mkdir -p notes/other/a/b/c
tree notes
../../_images/mkdir-fourth-demo.svg

For more information about mkdir, consider using some commands to get help information about the command directly in the terminal.

Test Yourself

  • How can you make more than one directory at the same time?

  • The previous question doesn’t ask you to consider the relationship between the two or more directories. One can be the parent (or grandparent, etc.) of the other or they can be sibling directories. How can you make make two new directories in either scenario?