To make a directory in Linux, use the mkdir command followed by the directory name, such as mkdir notes. To create parent directories that do not yet exist, add the -p option, as in mkdir -p projects/demo.
What a directory is in Linux
A directory is a filesystem object that organizes names of files and other directories. It is the Linux equivalent of what many graphical file managers call a folder. Directories can be nested, so a directory may contain files, child directories, or both.
When you run a command in a terminal, it operates in a current working directory unless you give it a different path. Knowing where you are helps you avoid creating a directory in the wrong place. Run pwd to print the current directory, and use ls to see its contents.
Linux treats directory names as case-sensitive: Notes and notes are different names. A name can contain spaces, but spaces need careful handling in shell commands. For simple directory names, lowercase letters, digits, hyphens, and underscores are easy to work with.
Create a directory with mkdir
The basic form is mkdir name. For example, to create a directory called drafts in the current location, enter:
mkdir drafts
If the command succeeds, it normally prints no message. Check that the directory exists with ls, or ask for details with ls -ld drafts. The -d option makes ls show the directory itself rather than listing its contents.
You can create more than one directory in a command by giving mkdir multiple names:
mkdir images documents backups
Each name is created in the current working directory. If any requested name already exists as a directory, mkdir reports that it cannot create it; it does not replace the existing directory or erase its contents.
Create nested directories with -p
Without an option, mkdir expects the parent directory in a path to exist already. For instance, mkdir work/reports fails if work does not exist. Add -p when you want to create missing parent directories along the path:
mkdir -p work/reports/weekly
This creates work, then reports inside it, then weekly inside reports, as needed. If the directories already exist, mkdir -p leaves them in place and does not treat their existence as an error. That makes it useful in setup scripts that may be run more than once.
The option does not mean “replace this directory.” It does not clear existing contents, and it cannot turn an existing regular file into a directory. If a component of the path is a file, the command fails because a file cannot serve as a parent directory.
Choose a path: relative or absolute
A relative path is interpreted from the current working directory. If pwd shows /home/sam, then mkdir projects creates /home/sam/projects. Relative paths are convenient when you are already working in the intended location.
An absolute path starts from the filesystem root, written as /. For example, mkdir -p /home/sam/projects/demo names the target without relying on the current directory. Creating directories in system locations may require extra permissions, and changing system directories without understanding their purpose can disrupt software or services.
Special path components can help navigate. A single dot, ., means the current directory; two dots, .., mean its parent. For example, mkdir ../shared attempts to create shared beside the current directory. Check the resolved location before using a relative path if you are unsure where it leads.
Names with spaces and special characters
Put a name containing spaces in quotes so the shell passes it as one argument:
mkdir "class notes"
Alternatively, escape each space with a backslash: mkdir class\ notes. Quoting is often easier to read. The same principle applies to paths, such as mkdir -p "course files/week one".
Some characters have special meaning to the shell. Quoting a path helps prevent the shell from treating those characters as instructions or patterns. A leading hyphen can also be confusing because command-line tools commonly use hyphens to introduce options. Use a path such as ./-archive to make clear that the name refers to an item in the current directory.
Permissions and ownership
Creating a directory requires permission to write to its parent directory and permission to access that parent. A “Permission denied” message usually means your account lacks the required access, or a security policy prevents the operation. Check the parent with ls -ld parent and confirm that you are working in the intended location.
Do not automatically rerun a failed command with administrator privileges. First ask whether the directory belongs in a protected system location. For personal work, creating it under your home directory is usually more appropriate. Administrative access should be used only when needed and when you understand why the location requires it.
New directories are created with permissions influenced by the process’s umask, which removes selected permissions from the default mode. Consequently, the exact permissions can vary between accounts and systems. Check them with ls -ld directory. The command’s -m option can request a permission mode at creation, but permission notation can be easy to misread; consult the local command documentation before relying on a particular mode.
Common errors and safe checks
- “File exists”: A file or directory already has that name. Use
ls -l nameto inspect it; do not delete it merely to make the command succeed. - “No such file or directory”: A parent path may be missing, or the path may contain a typo. Use
mkdir -pif creating missing parents is intended, and check spelling and capitalization. - “Permission denied”: You may not be allowed to write to the parent directory. Verify the location and permissions rather than using elevated access automatically.
- Unexpected location: A relative path depends on the current directory. Run
pwdbefore creating it, or use a full path.
Before creating directories in bulk, inspect the command and its destination. A misspelled path can create a valid directory in an unintended location. The mkdir command itself does not remove files, but avoid combining directory setup with destructive commands unless you understand each command’s effect.
Using mkdir in scripts
Scripts often use mkdir -p to ensure a working directory exists before writing output. For example:
mkdir -p output/logs
If the path is already present as directories, this is safe to repeat. If a regular file blocks part of the path, the command still fails, which is useful: the script should not silently treat a file as a directory. A script should also check whether commands succeed when later steps depend on the directory being available.
Use quotes around path variables in shell scripts, for example mkdir -p "$target_dir", so a value containing spaces remains one path argument. Scripts that accept paths from users should validate those paths according to their purpose; blindly constructing paths from untrusted input can lead to unintended locations.
Frequently Asked Questions
How do I make a directory in Linux?
Run mkdir directory_name in a terminal. Replace directory_name with the name you want, then check the result with ls.
How do I create a folder and its parent directories?
Use mkdir -p parent/child. The command creates missing parts of the path and leaves existing directories in place.
Does mkdir create a directory if it already exists?
Plain mkdir reports an error when the name already exists. With -p, an existing directory is accepted, but an existing regular file with that name is not.
How do I create a directory with a space in its name?
Quote the name, as in mkdir "my folder", or escape the space as mkdir my\ folder.
Why does mkdir say “Permission denied”?
Your account may lack write permission for the directory’s parent, or the location may be protected. Confirm the target with pwd and inspect the parent’s permissions before deciding what access is appropriate.
Quick recap
Use mkdir name for a directory in the current location, and use mkdir -p path/to/name when parent directories may be missing. Check your location with pwd, quote names containing spaces, and treat permission errors as a reason to verify the destination rather than to use administrator access by default.