What a directory is and why you need to create one
A directory in Linux is a folder — a container that holds files and other directories. Creating a directory is how you organize your work, separate projects, and keep your system tidy. You create directories the same way you would create a folder on Windows or Mac, except you type a command instead of right-clicking.
The most common command to create a directory is mkdir, which stands for "make directory". When you type mkdir followed by a name, Linux creates that folder in whatever location you are currently in. This is the tool you will use for almost every directory you need to make.
Understanding how to create directories is foundational because nearly everything else you do in Linux — organizing files, running projects, backing up data — depends on having the right folder structure in place first.
Key Takeaways
- The mkdir command creates a single directory: type mkdir foldername and press Enter.
- The mkdir -p command creates a directory and any missing parent folders in one step, useful for building nested folder structures.
- Directory names can contain letters, numbers, hyphens, and underscores, but spaces require special handling with quotes or backslashes.
- You can verify a directory was created by typing ls to list the contents of your current location.
- Directories are created in your current working directory unless you specify a different path.
Creating a single directory with mkdir
Open your terminal and navigate to the location where you want the directory to appear. If you want it in your home folder, you can type cd ~ to go there. Then type mkdir followed by a space and the name you want:
mkdir my_project
Press Enter. Linux creates the directory silently — if nothing prints to the screen, the command worked. To confirm, type ls and press Enter. You will see my_project listed among the contents of your current location. The directory now exists and is ready to hold files.
The name you choose can contain letters, numbers, hyphens, and underscores. Avoid spaces in directory names because they complicate commands later. If you must use a space, wrap the name in quotes: mkdir "my project" or use a backslash before the space: mkdir my\ project.
Creating nested directories in one command
Sometimes you need to create a directory inside another directory that does not yet exist. For example, you might want projects/website/images, but the projects folder does not exist yet. Typing mkdir projects, then mkdir projects/website, then mkdir projects/website/images takes three commands.
The -p flag (which stands for "parents") creates all the missing folders at once. Type:
mkdir -p projects/website/images
Linux creates projects, then website inside it, then images inside that. All three directories appear in one command. This is especially useful when you are setting up a new project structure and know exactly how you want it organized from the start.
Understanding paths and where directories are created
When you type mkdir foldername, Linux creates that directory in your current working directory — the folder you are currently inside. You can see where you are by typing pwd (print working directory). The output shows your location as a path, like /home/username or /home/username/projects.
If you want to create a directory somewhere other than your current location, include the path in the command. For example, mkdir /home/username/documents/archive creates the archive folder inside documents, regardless of where you are. You can also use ~ as shorthand for your home directory: mkdir ~/documents/archive does the same thing.
Relative paths also work. If you are in /home/username and want to create a folder inside documents, type mkdir documents/newfiles. Linux interprets documents as "relative to where I am now" and creates newfiles inside it.
Naming directories clearly and avoiding common mistakes
Choose directory names that describe what goes inside them. Names like project_2024, client_files, or backup_march are clear. Names like stuff, temp, or folder1 become confusing when you have many of them. Clear names save you time when you are looking for something months later.
Avoid starting directory names with a dot unless you want them hidden. In Linux, directories beginning with a dot (like .config) are hidden by default and do not show up when you type ls. They still exist — type ls -a to see them — but they are out of the way. This is useful for configuration folders you rarely need to access, but confusing if you hide a directory by accident.
If you try to create a directory that already exists, mkdir returns an error message like "File exists". This is not a problem — it just means the directory is already there. The -p flag also suppresses this error, so mkdir -p projects will not complain if projects already exists.
Verifying your directory was created
After you create a directory, confirm it exists by listing the contents of your current location. Type ls and press Enter. You will see all files and directories in your current folder, including the one you just made. If you created a nested structure like mkdir -p a/b/c, type ls to see the a directory, then type ls a to see b inside it, then type ls a/b to see c.
For a more detailed view, type ls -l. This shows each item on its own line with permissions, size, and date information. Directories are marked with a d at the start of the line, so you can distinguish them from files at a glance.
Organizing multiple directories at once
If you need to create several directories in the same location, you can list them all in one mkdir command. Type:
mkdir folder1 folder2 folder3
This creates all three directories in your current location. This is faster than typing mkdir three separate times, and it is useful when you are setting up a project with multiple sections that are all at the same level.
You can also combine this with the -p flag to create multiple nested structures. For example, mkdir -p project1/src project1/docs project2/src project2/docs creates two projects, each with src and docs folders inside. This builds a consistent structure across multiple projects in one command.
Frequently Asked Questions
What happens if I create a directory with a name that already exists?
Linux returns an error message saying the file or directory already exists and does not create a duplicate. The existing directory is unchanged. If you use mkdir -p with an existing directory name, no error appears — the command straightforward recognizes the directory is already there and continues.
Can I create a directory with spaces in the name?
Yes, but you must tell Linux the space is part of the name. Wrap the name in quotes: mkdir "my folder" or use a backslash before the space: mkdir my\ folder. Without either, Linux treats the space as a separator and tries to create two directories instead of one.
How do I create a directory in a location I do not have permission to access?
You cannot create a directory where you lack write permission. The mkdir command returns a "Permission denied" error. You can create directories in your home folder and its subfolders without special permission. For system-wide locations like /usr or /etc, you would need administrator (sudo) access, which is rarely necessary for personal work.
What is the difference between mkdir and mkdir -p?
mkdir creates a single directory and fails if the parent folder does not exist. mkdir -p creates the directory and any missing parent folders in one step. Use mkdir -p when you are building a nested structure all at once, and plain mkdir when you are creating a single folder in a location that already exists.
Can I undo a directory creation?
Yes, with the rmdir command, which removes an empty directory. Type rmdir foldername to delete it. If the directory contains files, rmdir refuses to delete it. You would need to empty it first or use rm -r, which is more powerful and requires care because it cannot be undone.