mkdir looks trivial until a missing parent directory or a wrong umask wrecks a deploy script at 2am. By the end of this guide you can create a directory, build a missing tree, set permissions on purpose, and check what actually happened. The examples use GNU mkdir from coreutils 9.4, as installed on the reference machine.
Allow about five minutes. You need a shell and write access to the parent location. Creating directories under system paths normally needs elevated privileges, so start somewhere you own.
Pass the directory name as an argument. This changes the filesystem immediately, so pick a path you can remove or reuse while testing.
mkdir ~/example-project
stat -c '%A %a %n' ~/example-project
The first command produces no output on success. The second prints the permissions, numeric mode and path, for example:
drwxr-xr-x 755 /home/you/example-project
The exact mode depends on your umask: mkdir starts with directory permissions equivalent to 0777 and the umask removes bits from that. It does not create a file with a hidden extension or initialise a project, just an empty directory.
Checkpoint: test -d ~/example-project && echo "directory is ready" prints the confirmation. pwd still reports your original directory; mkdir does not move the shell into the new one.
Without -p, every parent must already exist. Use --parents when you deliberately want mkdir to create the whole path.
mkdir -p ~/example-project/data/incoming
find ~/example-project -maxdepth 2 -type d -print
data and data/incoming.-p, an existing directory is not an error, which makes a setup command safe to run again.A frequent trap: -m does not apply the same mode to every directory in a new chain. With -p -m MODE, only the final directory gets that mode; newly created parent directories use the normal default, and the manpage states that existing parents are unaffected.
mkdir -p -m 750 ~/example-project/private/reports
stat -c '%A %a %n' ~/example-project/private ~/example-project/private/reports
Expect the reports directory to show mode 750, and its newly created parent to normally show 755 with a 022 umask. If you need a different parent mode, set it explicitly with chmod after checking the path.
Checkpoint: test -d ~/example-project/private/reports && echo "tree is ready". A non-zero status means the path was not created, or is not a directory.
Use -m when the directory's access policy matters from its first moment. The value uses chmod syntax, including octal modes.
mkdir -m 750 ~/example-project/shared
stat -c '%A %a %n' ~/example-project/shared
Mode 750 gives the owner read, write and search access, the group read and search access, and everyone else nothing. For a directory, the execute bit means search: a user can traverse it and reach a known entry when other permissions allow it. It does not make files inside executable.
Warning: do not reach for a broad mode such as 777 just to silence a permission error. World-writable directories can let other users add, replace or remove names, depending on surrounding permissions. Decide who needs access, use the narrowest mode that works, and verify with stat. If a service account needs the directory, ownership or group membership may be the correct fix instead.
Add -v or --verbose when a script or long path makes it unclear which components were created.
mkdir -pv ~/example-project/cache/images
test -d ~/example-project/cache/images && echo "image cache exists"
For newly created components, output looks like:
mkdir: created directory '/home/you/example-project/cache'
mkdir: created directory '/home/you/example-project/cache/images'
When every component already exists, -p -v normally prints nothing. Do not parse the human-readable messages as a machine interface; use the exit status and a check such as test -d instead.
A plain mkdir fails if the target already exists, and it also fails when a parent is missing. A missing parent is not fixed by -m; add -p when you actually mean to create a tree.
mkdir ~/example-project/data/incoming
# mkdir: cannot create directory ...: No such file or directory
A permission error means you cannot write the parent, not necessarily that the final directory's mode is wrong. Check the path with namei -l where available, and inspect the parent with ls -ld. Only use elevated privileges for a path that genuinely belongs under system administration:
sudo mkdir -p -m 755 /srv/example-service/data
Review the full path before pressing enter. sudo changes who performs the operation; it does not make an unsafe path safe. Verify ownership and permissions afterwards:
sudo stat -c '%U:%G %A %a %n' /srv/example-service/data
mkdir has no undo option. If you created the example tree and nothing useful is inside it, remove only the exact directory you created, after checking it carefully.
find ~/example-project -maxdepth 3 -print
rmdir ~/example-project/data/incoming
rmdir ~/example-project/data
rmdir removes empty directories only, so it refuses rather than deletes files. If the tree contains data, stop and handle those files separately. Never turn a guessed path into a recursive deletion command.
test -d PATH.-p.stat shows what you intended.rmdir while it stays empty.