Animated folder symbolizing how to create directories in Linux using the mkdir command.

How to Create Directories In Linux – “Mkdir” Syntax & Examples

Hey There! Welcome!

This lesson is all about creating directories within the terminal. We’ll also delve into the Linux naming convention and learn how to use character escaping effectively. So let’s get started!

Create Linux Directories with "mkdir" - In your current Location:

Throughout your journey with Linux, one essential skill you’ll need to master is creating directories. Keeping your files organized in several directories will help you locate and manage them with ease, as well as streamline your workflow.

Thankfully, creating directories in Linux is as easy as pie, and who doesn’t love pie?

To demonstrate let’s create an “images” directory within our current location. All we have to do is use the mkdir command, which is short for “make directory”, followed by the path we want our new directory to have. Here’s an example:

				
					ubuntu@thenerd.academy:~$ mkdir /desired/path

				
			

In our case, since we want to create this new directory within our current location, which, as represented by the squiggly line, is our users home directory, we can simply type the path to our current location, followed by a slash and the desired name for our new directory.

				
					ubuntu@thenerd.academy:~$ mkdir /home/ubuntu/images

				
			

In essence, this path represents the location our new directory would have if it already existed.

But wait!  While this command would work perfectly well here is a little time saving trick we can use.

Instead of typing an absolute path every time we want to create a directory, we can use a relative path to save us from all that tedious typing.

Need a refresher on relative paths? Since we’re located in our user’s home directory, we can subtract that part from our path, leaving us with just the name of our new directory.

				
					ubuntu@thenerd.academy:~$ mkdir images

				
			

In practice, this means whenever we want to create files or directories in our current location, we only need to specify their names. Time to put it to the test:

				
					ubuntu@thenerd.academy:~$ mkdir images
ubuntu@thenerd.academy:~$ ls
images
ubuntu@thenerd.academy:~$

				
			

Great! Our directory is alive and kicking.

Create Linux Directories From & In any Terminal Location:

To further hammer this concept home, let’s create a subdirectory within the “images” directory we just made. We’ll name it “cats” and pretend it’s for storing pictures of our adorable cat.

Can you figure out the command we need to use to create this new directory? Remember, our objective is to create a directory called “cats” inside the “images” directory using a relative path. I’ll give you 10 seconds. Ready, set, go!

Figured it out? Here’s the answer. To kick things off we need to use the mkdir command, no surprises there. Then, we need to specify the path we want our new directory to have.

To begin, we need to determine the relative path to the “images” directory, since that’s where we want to create our new directory. Because the “images” directory is located in our current directory, its relative path is simply its name.

Next, since we want to create our new directory within the “images” directory, we need to follow up with a slash and the name we want our new directory to have.

				
					ubuntu@thenerd.academy:~$ mkdir images/cats
ubuntu@thenerd.academy:~$

				
			

And just like that, we’re good to go! Now, let’s double-check our work.

As we learned in a previous lesson, we can use the ls command to display the contents of a specific directory by entering its path as the argument. In our case, here’s what that would look like. Let’s put it to work!

				
					ubuntu@thenerd.academy:~$ ls images
cats
ubuntu@thenerd.academy:~$

				
			

Success! Our directory was indeed created, and we didn’t even have to navigate there! Let’s break out the catnip and celebrate!

Common Pitfalls: Why choosing the Right Name is Important:

Now that we’ve mastered the art of creating directories, let’s take a look at a few pitfalls to avoid when it comes to naming directories. Consider this command:

				
					ubuntu@thenerd.academy:~$ mkdir my files

				
			

At first glance, it looks like we’re creating a directory called “my files”, right? Well, let’s give it a go and see what happens!

				
					ubuntu@thenerd.academy:~$ mkdir my files
ubuntu@thenerd.academy:~$
				
			
No errors pop up, so we’re off to a good start. Let’s check if our directory was created using the ls command.
				
					ubuntu@thenerd.academy:~$ ls
my  files
ubuntu@thenerd.academy:~$ 

				
			

Uh-oh! Instead of one directory, we ended up with two separate directories: one named “my” and the other “files”.

But why did this happen? It’s because the terminal treats spaces as separators between multiple arguments. So, the mkdir command thought we wanted to create two different directories based on the two arguments we provided.

So what should we have done instead? Am glad you asked! We should have adhered to something called the Linux naming convention!

The Linux Naming Convention Explained:

A naming convention is a set of guidelines that define how items within the command line should be named to prevent confusion and unexpected behaviors.

In general, names within the command line should consist of letters, numbers, and the following special characters: periods, underscores, and dashes.

Special characters are not interpreted by their literal values and may be misinterpreted, leading to unexpected results, as we just experienced (more about the details of the naming convention here)

For convenience, it’s also recommended that we use lowercase characters exclusively and avoid excessively long file names, which can lead to finger fatigue. With these guidelines in mind, let’s pick a better name for our new directory. How about something like this:

				
					ubuntu@thenerd.academy:~$ mkdir my_files

				
			

This name has got it all: lowercase letters, an underscore instead of a space, and isn’t excessively long. Let’s try it out! Time for the moment of truth – let’s verify with the ls command.

				
					ubuntu@thenerd.academy:~$ ls
my  files  my_files
ubuntu@thenerd.academy:~$ 

				
			

Success! Our command was interpreted correctly, and our directory was created. High fives all around!

But Sometimes You Might Need to Bend the Rules:

Even though it’s strongly advised that we follow the naming convention when creating directories, sometimes we might need to bend the rules and use special characters or spaces anyway.

We can do this with a nifty technique called character escaping. Escaping employs special characters to indicate that spaces or other special characters should be interpreted literally.

For example, to escape a single character, we need to use a backslash before it. Here’s what that would look like:

				
					ubuntu@thenerd.academy:~$ mkdir my\ files

				
			

The backslash tells the command prompt to treat the character that follows as part of the name of our new directory and not as a separator between two different arguments.

An alternative to the backslash is using single or double quotes, which are especially useful for escaping multiple characters at once. Here’s what that would look like:

				
					ubuntu@thenerd.academy:~$ mkdir 'My Awesome Files'

				
			

This way, both of the spaces in the name of our new directory are escaped at once. How cool is that?

Keep in mind that we’ll need to use these escaping characters every time we refer to this directory. For instance, here’s what the absolute path for this directory would look like:

				
					/home/ubuntu/'My Awesome Files'
				
			

Weird, right? That’s why it’s best to stick to the naming convention and save ourselves the hassle.

Final Thoughts:

Congratulations! You are now proficient in creating and naming directories in Linux, which brings you one step closer to mastering this powerful operating system.

Fun Facts to brighten your day:

  • Why did the Linux user create a “cats” directory? Because they wanted to have a “purr-fect” file organization!

  • Using absolute paths in Linux is like telling a joke with the entire backstory. Using relative paths? Just the punchline!

  • Remember, using backslashes or quotes in directory names is like telling Linux, “This space is not what you think it is.” It’s the command line version of a plot twist!

  • Ever created a directory named . or ..? Spoiler alert: you can’t! These are reserved for special purposes: . refers to the current directory, and .. to the parent directory. Trying to outsmart Linux with this can be a hilarious exercise in futility!

  • Linux supports directory names of up to 255 characters. Imagine typing mkdir this-is-a-really-long-directory-name-that-just-keeps-going-and-going-and-you-get-the-idea. Not only is this a workout for your fingers, but also a test of your patience!

Test Your Knowledge (Practice Quiz):

There has been error loading this quiz, please refresh the page or enable JavaScript to continue.

0/5

Take your Knowledge to the next level!

Learn the Linux command line with our interactive course!

Animated Rocket symbolizing how awesome our Linux command line course is.

Welcome back!

Don't have an account? Select the register button to create one!

First time around?

You're in the right place! Fill out the form to create your account: