Creating a directory in Linux

A directory in Linux is a folder that holds files and other directories. You create one using the mkdir command, which stands for "make directory". The basic command is simple: type mkdir directoryname and press Enter, and Linux creates a new folder in your current location.

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 you own, though you will need write permission in the folder where you are creating it.

Key Takeaways

  • Use mkdir directoryname to create a single directory in your current location.
  • Use mkdir -p path/to/nested/directories to create multiple levels of folders at once, including parent directories that do not yet exist.
  • Directory names can contain letters, numbers, hyphens, and underscores, but spaces require quotes around the entire name.
  • Check what directory you are currently in by typing pwd (print working directory) before you create a new folder.
  • List the contents of your current directory with ls to confirm the new directory was created.

The basic mkdir command

Open your terminal and type the command exactly as shown. Replace directoryname with the name you want to give your folder:

mkdir directoryname

Press Enter. Linux creates the directory and returns you to the command prompt with no message — this is normal behavior. The directory now exists in whatever folder you were in when you typed the command. To see it, type ls and press Enter. Your new directory will appear in the list.

If you want to create a directory with a space in the name, put the entire name in quotes:

mkdir "My Documents"

Creating nested directories all at once

Sometimes you need to create multiple levels of folders, one inside another. Without the right flag, mkdir will fail if the parent directory does not exist. Use the -p flag to create all the parent directories automatically:

mkdir -p Projects/WebDesign/ClientWork

This command creates the Projects folder, then WebDesign inside it, then ClientWork inside that — all in one step. If Projects already exists, mkdir skips creating it and moves on to the next level. The -p flag stands for "parents" and is one of the most useful options for mkdir.

Without the -p flag, the same command would fail with an error message saying the Projects directory does not exist. Using -p prevents that error and saves you from typing three separate commands.

Checking your current location before creating

Before you create a directory, it helps to know where you are in the file system. Type pwd (print working directory) and press Enter. The terminal shows the full path to your current location, starting with a forward slash:

pwd

You might see something like /home/username or /home/username/Documents. This tells you exactly where mkdir will create your new directory. If you are in the wrong location, use the cd command to move to a different folder first. For example, cd Documents moves you into the Documents folder.

Naming your directory

Linux directory names are case-sensitive, meaning MyFolder and myfolder are two different directories. Names can contain letters, numbers, hyphens, and underscores. Avoid spaces unless you put the name in quotes, because spaces can confuse the command line.

Good directory names are short and descriptive: mkdir Projects, mkdir backup_2024, or mkdir client-files. Avoid special characters like asterisks, question marks, or forward slashes, because Linux uses these for other purposes.

If you make a mistake and want to remove the directory, use rmdir directoryname — but only if the directory is empty. If it contains files or other directories, you will need to remove those first or use rm -r directoryname to remove everything inside it.

Verifying the directory was created

After you type mkdir and press Enter, the terminal usually gives no response. This silence means the command worked. To confirm, type ls and press Enter to list everything in your current directory. Your new folder will appear in the list.

You can also use ls -l to see more details, including the date the directory was created. Directories appear with a d at the start of the line, which tells you it is a folder rather than a file. If you created nested directories with mkdir -p, type ls -R to see all the levels you just created.

Common mistakes and how to fix them

The most common error is trying to create a directory inside a parent that does not exist. You type mkdir Projects/WebDesign but Projects does not exist yet, and you get an error. The fix is to add the -p flag: mkdir -p Projects/WebDesign.

Another mistake is forgetting to quote a name with spaces. If you type mkdir My Documents without quotes, Linux creates two directories called My and Documents instead of one called My Documents. Use quotes to keep the name together: mkdir "My Documents".

If you get a "permission denied" error, you do not have write permission in the current directory. Check your location with pwd and move to a folder you own, such as your home directory. Type cd ~ to go to your home folder, then try mkdir again.

Frequently Asked Questions

What is the difference between mkdir and mkdir -p?

mkdir creates a single directory and fails if the parent directory does not exist. mkdir -p creates all parent directories automatically, so it never fails due to missing folders. Use -p whenever you are creating nested directories.

Can I create multiple directories with one command?

Yes. Type mkdir folder1 folder2 folder3 to create three separate directories at the same level. All three will appear in your current location. You can also combine this with -p for nested structures: mkdir -p dir1/sub1 dir1/sub2 creates two subdirectories inside dir1.

What happens if I try to create a directory that already exists?

mkdir returns an error saying the directory already exists and does not create a duplicate. If you use mkdir -p on a path where some directories already exist, it skips the existing ones and creates only the missing levels.

How do I create a directory with a specific name that contains special characters?

Most special characters should be avoided in directory names. If you must use them, put the entire name in single or double quotes: mkdir 'folder-name_2024'. Stick to letters, numbers, hyphens, and underscores for the easiest results.

Can I create a directory in a location other than my current folder?

Yes. Type the full path or relative path to the location: mkdir /home/username/Documents/NewFolder creates NewFolder in Documents, no matter where you currently are. You can also use mkdir ~/NewFolder to create a directory in your home folder from anywhere.