Creating a directory in Linux using the mkdir command

To create a directory in Linux, you use the mkdir command followed by the name you want to give the directory. Open your terminal, type mkdir my_folder, and press Enter. Linux will create a new directory called my_folder in your current location. That is the entire process for a single directory.

The mkdir command works the same way whether you are using Ubuntu, Fedora, CentOS, or any other Linux distribution. The terminal is where you type the command, and the directory appears when ready after you press Enter. You do not need special permissions to create a directory in a location you own, such as your home folder.

Key Takeaways

  • The mkdir command creates a single directory: type mkdir directory_name and press Enter.
  • To create nested directories all at once, use the -p flag: mkdir -p path/to/nested/folders.
  • Directory names can contain letters, numbers, hyphens, and underscores, but spaces require quotes around the name.
  • You can create multiple directories in one command by listing them: mkdir folder1 folder2 folder3.
  • Use ls to verify that your new directory was created in the location you intended.

Creating a single directory with a straightforward name

The simplest case is creating one directory with a straightforward name. Open your terminal and type the command exactly as shown: mkdir documents. Press Enter. The directory called documents now exists in your current folder. You can verify this by typing ls and pressing Enter — you will see documents listed among your files and folders.

If you want to create a directory with a name that contains spaces, you must put the name in quotes. For example, mkdir "My Documents" creates a directory with that exact name, including the space. Without the quotes, Linux would interpret "My" and "Documents" as two separate directory names and try to create both.

Creating multiple directories at once

You can create several directories in a single command by listing their names after mkdir, separated by spaces. Type mkdir photos videos music and press Enter. Linux will create all three directories in your current location at the same time. This is faster than running mkdir three separate times.

This method works well when the directories are all at the same level and in the same location. If you need to organize them into a hierarchy — for example, a photos folder that contains subfolders for 2023, 2024, and 2025 — use the nested approach described in the next section instead.

Creating nested directories in one command

When you need to create a folder structure with multiple levels, such as projects/work/2024, you normally would have to create projects first, then work inside it, then 2024 inside that. The -p flag lets you create all of them at once. Type mkdir -p projects/work/2024 and press Enter. Linux creates projects, then work inside it, then 2024 inside work — all in one step.

The -p flag also prevents an error if a directory already exists. If projects already exists on your system, the command mkdir -p projects/work/2024 will still succeed and create only the missing folders (work and 2024). Without the -p flag, you would get an error message saying projects already exists.

Checking your work and understanding directory paths

After you create a directory, verify it exists by typing ls to list the contents of your current location. You will see your new directory listed. If you created nested directories, you can navigate into them to confirm the structure. Type cd projects/work/2024 to move into the 2024 folder, then type pwd to see your full path — it will show something like /home/username/projects/work/2024.

The path tells you exactly where you are in the directory structure. The forward slashes separate each level. If you ever get lost, typing cd ~ takes you back to your home directory, and typing ls shows you what is there. This is how you orient yourself when working in the terminal.

Common naming conventions and what to avoid

Directory names can contain letters, numbers, hyphens, and underscores. Names like project_2024, backup-files, and data123 all work well. Avoid spaces unless you put the entire name in quotes, because spaces confuse the terminal. Avoid special characters like asterisks, question marks, and dollar signs — they have special meaning in the terminal and will cause errors.

Linux is case-sensitive, which means Documents and documents are two different directories. Many people use lowercase for directory names to keep things consistent and avoid confusion. Choose a naming style and stick with it across your system so you can find things later.

What to do if mkdir returns an error

The most common error is "Permission denied," which means you do not have permission to create a directory in that location. This usually happens when you try to create a directory in a system folder that requires administrator access. Create directories in your home folder instead, where you have full permissions. Type cd ~ to go to your home folder, then create your directory there.

Another error is "File exists," which means a directory with that name already exists in your current location. If you meant to create it anyway and want to avoid the error, use the -p flag. If you did not realize it already existed, use ls to see what is there, and choose a different name for your new directory.

Frequently Asked Questions

Can I create a directory with a name that starts with a dot?

Yes. A name like mkdir .hidden creates a directory that does not show up when you type ls by itself. To see hidden directories, type ls -a. Hidden directories are useful for storing configuration files, but for most purposes you should use regular names.

What is the difference between mkdir and mkdir -p?

mkdir creates a single level of directory and fails if the parent folder does not exist. mkdir -p creates all missing levels at once and does not fail if directories already exist. Use -p when you want to build a complete folder structure in one command.

Can I change a directory's name after I create it?

Yes, use the mv command. Type mv old_name new_name to rename a directory. For example, mv documents my_documents renames the documents folder to my_documents. The directory and everything inside it stays in the same location.

Do I need to be in a specific folder to create a directory?

No. You can create a directory anywhere you have permission by typing the full path. For example, mkdir ~/projects/new_folder creates new_folder inside your projects directory, no matter where you currently are. The tilde (~) represents your home directory.

What happens if I create a directory with the same name as a file?

Linux will not let you. If a file called documents already exists, mkdir will return an error saying the file exists. You must choose a different name or delete the file first — but be careful deleting files, because deletion is permanent in the terminal.