DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Linux mkdir: Create Directories, Build Paths, and Fix Common Errors

Creating a directory from the Linux command line is simple; choosing the right path and understanding why a command fails are the parts that tend to trip people up.

The basic command is:

mkdir project
Enter fullscreen mode Exit fullscreen mode

That creates project inside your current working directory. To check where you are first, run pwd.

Choose a path that matches the task

You can pass mkdir a relative or absolute path. A relative path is interpreted from your current directory:

mkdir projects/demo
Enter fullscreen mode Exit fullscreen mode

This works only if projects already exists. An absolute path starts with / and refers to the same location regardless of your current directory:

mkdir /home/alex/projects/demo
Enter fullscreen mode Exit fullscreen mode

Absolute paths can be more predictable in scripts, where you might not know what the current working directory will be.

To create several separate directories at once, pass multiple names:

mkdir logs backups temp
Enter fullscreen mode Exit fullscreen mode

These are three sibling directories, not a nested path.

Create a directory tree with -p

Plain mkdir won’t create missing parent directories. For example, this fails if projects or app doesn’t exist yet:

mkdir projects/app/logs
Enter fullscreen mode Exit fullscreen mode

Use -p when you want mkdir to create the missing parts of the path too:

mkdir -p projects/app/logs
Enter fullscreen mode Exit fullscreen mode

It also succeeds if the target directory already exists, which makes it handy in scripts that may run more than once. For a closer look at this behavior, see what mkdir -p does.

Names, spaces, and hidden directories

The shell treats spaces as separators between arguments. Quote a name with spaces so it stays a single argument:

mkdir "Project Archive"
Enter fullscreen mode Exit fullscreen mode

A directory whose name starts with a dot is hidden from a plain ls listing:

mkdir .local-data
ls -a
Enter fullscreen mode Exit fullscreen mode

It’s still a normal directory; the leading dot is a naming convention, not a special permission or attribute.

Set permissions deliberately

By default, the permissions on a new directory are affected by your shell’s umask. A common umask of 022 results in permissions of 755: the owner can read, write, and enter the directory, while group and other users can read and enter it.

For a private directory, you can request a specific mode when creating it:

mkdir -m 700 private-data
Enter fullscreen mode Exit fullscreen mode

Mode 700 grants access to the owner only. When -m is used with -p, the specified mode applies to the final directory, not the missing parent directories created along the way. If you need to inspect or adjust permissions afterward, this guide to changing Linux file permissions explains the common approaches.

Diagnose the error before reaching for sudo

File exists

Something already occupies that name. Check whether it’s a directory or a file:

ls -ld path/to/name
Enter fullscreen mode Exit fullscreen mode

If it’s already a directory, you may be able to use it as-is. If it’s a file, mkdir won’t overwrite it. The -p option suppresses the error for an existing directory, but not for a file blocking the path.

Permission denied

Creating a directory requires write and execute permission on its parent directory. Inspect the parent before changing anything:

ls -ld path/to/parent
Enter fullscreen mode Exit fullscreen mode

Using sudo mkdir may create a directory owned by root, leaving your normal user unable to write inside it. First confirm that the path is the one you intended. If you do need elevated privileges, consider who should own the new directory afterward.

No such file or directory

A parent in the path is missing. If you want mkdir to create the missing parents, retry with -p. If the error instead says Read-only file system, the problem isn’t a missing directory: the filesystem doesn’t currently allow writes.

Verify what you created

Use ls -ld to show the directory entry, its permissions, and its owner:

ls -ld project
Enter fullscreen mode Exit fullscreen mode

The -d matters: without it, ls project lists the directory’s contents instead of describing the directory itself.

To create a directory and enter it only if creation succeeds, chain the commands with &&:

mkdir project && cd project
Enter fullscreen mode Exit fullscreen mode

If mkdir fails, the shell won’t run cd. That’s a useful small guard against continuing under the assumption that a new directory was created.

For everyday use, remember the distinction: mkdir name creates one directory, while mkdir -p path/to/name also creates missing parents and tolerates an existing target directory.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)