How to Create Directories In Linux - "Mkdir" Syntax & Examples (2024)

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 imagesubuntu@thenerd.academy:~$ lsimagesubuntu@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.

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 imagescatsubuntu@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 filesubuntu@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:~$ lsmy filesubuntu@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:~$ lsmy files my_filesubuntu@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 mkdirthis-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!

Start Watching!

How to Create Directories In Linux - "Mkdir" Syntax & Examples (1)

How to Create Directories In Linux - "Mkdir" Syntax & Examples (2024)

FAQs

How to Create Directories In Linux - "Mkdir" Syntax & Examples? ›

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP". It's a good practice to organize files by creating directories and putting files inside of them instead of having all files in one directory.

How do I create a list of directories in Linux? ›

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How to create multiple directory and subdirectories in Linux? ›

To recursively create multiple directories at once within the same parent directory, you can use the following syntax: $ mkdir -p <path> … This command will create the src and tmp directories within the same parent directory, including their intermediate directories public , utils , and logs .

How do you write into a directory in Linux? ›

To do so, type cd followed by the path to the directory you want to create a file in and press Enter.. For example, you could type cd /home/username/Documents to navigate to your Documents folder. Alternatively, you can type cd / to navigate to the Root directory, or type cd ~ to navigate to your Home/User directory.

Which command is used to make a new directory? ›

The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md .

How to create a directory in Linux example? ›

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP".

What is the syntax of mkdir in Linux? ›

The basic syntax for using this command is mkdir {dir} (replace {dir} with the desired name of your directory). Before creating any directory or file, remember that most Linux filesystems are case-sensitive.

How to use mkdir in bash? ›

The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

How do I write current directory in Linux? ›

Use the pwd command to write to standard output the full path name of your current directory (from the /(root) directory).

How to set directory in Linux? ›

Changing to another directory (cd command)
  1. To change to your home directory, type the following: cd.
  2. To change to the /usr/include directory, type the following: cd /usr/include.
  3. To go down one level of the directory tree to the sys directory, type the following: cd sys.

Which command is used to directory in Linux? ›

The mkdir command in Linux

The mkdir command allows you to create directories from within the terminal.

How do I create multiple directories in mkdir? ›

To create multiple directories at once, you can specify multiple directory names separated by spaces: Syntax: mkdir [directorie_name_1] [directorie_name_1] [directorie_name_1] .......

What is P in mkdir? ›

Feb 6, 2023. mkdir -p is a command in Linux and Unix-like operating systems used to create a new directory. The -p option stands for "parent" and it allows you to create a directory hierarchy (a tree of directories) in one step, without getting an error if the parent directories already exist.

How do I get a list of only directories in Linux? ›

There are several ways in which directories in the current path can be displayed:
  1. Using ls with -l to print directories: $ ls -d */
  2. Using ls -F with grep : $ ls -F | grep "/$"
  3. Using ls -l with grep : $ ls -l | grep "^d" ...

What command is used to list directories in Linux? ›

ls command is used to list the contents of a current directory. Copied! ls lists files and directories in the bare format where details like file types, size, modified date, modified time, permission, links, etc can't be viewed. There are some options that can be used with ls command to change the output format.

How do I list different directories in Linux? ›

To specify a different directory for the "ls" command in the Linux command-line, you can use the "-d" option followed by the path of the directory you want to list. The "ls" command is used to list the contents of a directory, and by default, it displays the contents of the current working directory.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6083

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.