Creating a directory in Linux means making a new folder where you can store files and other folders

The most common way to create a directory is with the mkdir command, which stands for "make directory". You type mkdir followed by the name you want to give the folder, and Linux creates it in your current location. If you want to create a folder called "Documents" in your home directory, you would type mkdir Documents and press Enter.

The command works the same way whether you are using Ubuntu, Fedora, CentOS, or any other Linux distribution. You do not need special permissions to create a directory in a location where you already have write access — which is usually your home directory and any folders inside it.

Key Takeaways

  • The mkdir command creates a single new directory: type mkdir foldername and press Enter.
  • To create nested directories all at once, use mkdir -p path/to/nested/folders instead of creating each one separately.
  • You can create multiple directories in one command by listing them: mkdir folder1 folder2 folder3.
  • If you get a "Permission denied" message, you are trying to create a directory in a location where you do not have write access — try your home directory instead.
  • Use ls after creating a directory to verify it was made and to see what else is in that location.

Creating a single directory with mkdir

Open your terminal and navigate to the location where you want the new directory to appear. If you want it in your home directory, you can type cd ~ to go there. Then type mkdir followed by a space and the name of your new folder.

For example, to create a folder called "Projects", type mkdir Projects and press Enter. Linux will create the folder silently — if you see no error message, the directory was made successfully. You can verify this by typing ls and pressing Enter to list everything in your current location. You should see "Projects" in the list.

Directory names can contain letters, numbers, hyphens, and underscores. Avoid spaces in directory names because they make the name harder to type later. If you need a readable name with spaces, use hyphens or underscores instead — for example, my-project instead of my project.

Creating multiple directories at once

If you need to create several directories in the same location, you can do it in one command instead of typing mkdir multiple times. List each directory name separated by a space: mkdir folder1 folder2 folder3. Linux will create all three directories in your current location.

This saves time when you are setting up a project structure with several top-level folders. For instance, if you are organizing a photography project, you might type mkdir raw edited exported metadata to create four folders at once for different stages of your workflow.

Creating nested directories with the -p flag

Sometimes you need to create a directory inside another directory that does not exist yet. Normally, mkdir fails if the parent directory is missing. The -p flag tells mkdir to create all the parent directories as needed, so you can build an entire folder structure in one command.

For example, if you want to create a folder structure like projects/2024/january/client-work but none of those folders exist yet, type mkdir -p projects/2024/january/client-work. Linux will create "projects", then "2024" inside it, then "january" inside that, and finally "client-work" at the end. If any of those folders already exist, mkdir skips them and continues.

The -p flag is especially useful when you are following instructions that tell you to create a specific folder path. It prevents errors from missing parent directories and is safe to use even if some of the folders already exist.

Understanding permissions and error messages

If you see the message "Permission denied" when you try to create a directory, it means you do not have write access to that location. This usually happens when you try to create a folder in a system directory like /etc or /usr, which are protected. You can only create directories in locations where you have permission.

Your home directory — the folder that appears when you type cd ~ — is always a safe place to create new directories. You have full permission there. If you need to create a directory in a protected system location, you would need to use sudo (which runs the command with administrator privileges), but this is rarely necessary for everyday work.

If you see "File exists" when you try to create a directory, it means a folder with that name already exists in your current location. You can use ls to see what is there, or choose a different name for your new directory.

Verifying your new directory was created

After you create a directory, use the ls command to confirm it exists. Type ls and press Enter to see a list of everything in your current location. Your new directory should appear in the list. If you created nested directories with mkdir -p, you can navigate into them with cd to verify the full structure was built correctly.

For a more detailed view that shows when each directory was created and other information, type ls -l instead. This displays each item on its own line with additional details. Directories are marked with a d at the start of the line, so you can easily spot them in a long list of files.

Common mistakes and how to avoid them

The most frequent mistake is typing the directory name with spaces and forgetting that spaces have special meaning in the terminal. If you type mkdir my documents, Linux creates two directories called "my" and "documents" instead of one called "my documents". Use hyphens or underscores instead: mkdir my-documents or mkdir my_documents.

Another common issue is trying to create a directory in a location where you do not have permission. If you get "Permission denied", check that you are in the right location by typing pwd (which shows your current directory path). Then navigate to your home directory with cd ~ and try again.

A third mistake is forgetting the -p flag when you need to create nested directories. If you type mkdir projects/2024/january and the "projects" folder does not exist, you will get an error. Add the -p flag to create all parent directories automatically: mkdir -p projects/2024/january.

Frequently Asked Questions

Can I create a directory with a name that starts with a dash or special character?

Yes, but it becomes harder to work with later. Names starting with dashes can confuse the terminal because dashes usually indicate command flags. Special characters like asterisks, question marks, and dollar signs have special meaning in the terminal. Stick to letters, numbers, hyphens, and underscores to avoid problems.

What is the difference between mkdir and mkdir -p?

Regular mkdir creates only the final directory and fails if parent directories do not exist. The -p flag creates all parent directories as needed, so you can build an entire folder structure in one command. Use -p when you need nested directories.

How do I create a directory with a name that contains spaces?

Put the entire name in quotes: mkdir "my documents" or mkdir 'my documents'. However, this makes the directory harder to navigate to later, so using hyphens or underscores is usually better: mkdir my-documents.

Can I undo a directory I just created?

Yes, use the rmdir command followed by the directory name: rmdir foldername. This only works if the directory is empty. If the directory contains files or other folders, you will need to delete those first or use rm -r to remove the directory and everything inside it.

What happens if I create a directory with the same name as an existing one?

You will get a "File exists" error and nothing happens. The existing directory is not changed or overwritten. You can use ls to see what directories already exist in your current location before creating a new one.