VPSWala Blog

How to Create Directories with the mkdir Command in Linux

March 26, 2024, Written by 0 comment

Howdy, folder fanatics! Today, we’re going to tackle one of the most fundamental commands in the Linux terminal: mkdir. Now, I know what you’re thinking, “But wait, isn’t that just for making directories? How exciting could that be?”

Well, buckle up, because we’re about to turn the humble mkdir into a powerhouse of productivity. By the end of this guide, you’ll be creating directories faster than a rabbit makes babies (and with way less mess, thankfully).

What Even Is a Directory?

Before we dive into the nitty-gritty of mkdir, let’s quickly cover what a directory is. In the digital world, directories are like fancy filing cabinets for your files and other directories (it’s directories all the way down, folks).

Think of your computer’s file system as a giant tree, with the root directory as the trunk and all the other directories branching off like…well, branches. It’s a hierarchical structure that helps keep your data nice and organized (or at least, it’s supposed to).

The mkdir Basics

At its core, mkdir is a command that lets you create new directories (or folders, if you’re more of a visual learner). The syntax is dead simple:

mkdir [directory_name]

Just replace [directory_name] with the name you want to give your fresh, empty directory. For example:

mkdir ImportantStuff

Boom! You just created a directory called “ImportantStuff” in your current working directory. Easy peasy, right?

A Note on Paths

Now, what if you want to create a directory somewhere other than your current location? That’s where paths come into play.

You can specify an absolute path (the full, start-to-finish address of the directory) or a relative path (the location relative to your current working directory).

For example, let’s say you want to create a directory called “NewFolder” inside your “Documents” directory. If you’re already in your home directory, you could use the relative path:

mkdir Documents/NewFolder

Or, you could use the absolute path:

mkdir /home/your_username/Documents/NewFolder

Both commands will create the same “NewFolder” directory inside your “Documents” folder.

mkdir’s Secret Superhero Abilities

Sure, mkdir can create basic directories, but did you know it has some nifty bonus powers? Here are a few tricks up its sleeve:

Creating Multiple Directories at Once

Why stop at creating one directory when you can make a whole family of them in one fell swoop? Just separate the directory names with spaces:

mkdir Folder1 Folder2 Folder3

This command will create three new directories—”Folder1,” “Folder2,” and “Folder3″—all in your current working directory. Efficient and satisfying!

Making Nested Directories

Sometimes, you need to create a directory inside another directory that doesn’t exist yet. Instead of doing it in multiple steps, you can use the -p flag to tell mkdir to create any necessary parent directories:

mkdir -p Projects/WebDev/ResponsiveDesign

This command will create the “ResponsiveDesign” directory inside the “WebDev” directory, which is inside the “Projects” directory—even if “Projects” and “WebDev” didn’t exist before. Nested directory inception!

Changing Directory Permissions

By default, new directories inherit the permissions of their parent directory. But what if you want to change those permissions right off the bat? mkdir has a flag for that!

The -m flag lets you set specific permissions for the new directory. For example, to create a directory with read, write, and execute permissions for yourself (the owner), you could use:

mkdir -m 700 SuperSecretFolder

That 700 is an octal code that represents the desired permissions. Don’t worry, we won’t get too deep into the permissions rabbit hole today. Just know that -m is there if you need it!

Parting Wisdom: A Few mkdir Pro Tips

Before we wrap things up, here are a few quick tips to help you become an mkdir master:

  • Use descriptive directory names: Sure, “NewFolder” gets the job done, but a name like “2023_Taxes” or “WebDev_Projects” will make your life much easier down the road.
  • Avoid spaces in directory names: While Linux can handle spaces in directory names, it’s generally better to use underscores (_) or hyphens (-) instead. Trust me, your future self will thank you.
  • Keep it organized: Don’t just create a million directories in your home folder. Group related directories together in a logical way that makes sense to you.
  • Use tab completion: Most terminals support tab completion, which means you can type the first few letters of a directory name and then hit the tab key to autocomplete it. Saves you a ton of typing!

Also Read:

  1. How To Connect To Windows VPS Using RDP in 3 Simple Steps?
  2. How To Install Docker and Kubernetes on Your VPS?
  3. How to install Plex on VPS?

And there you have it, folks! You’re now a bona fide mkdir master, ready to build cozy little nests for all your digital ducklings. Just remember: with great directory-creating power comes great responsibility. So use it wisely, and may the hierarchical forces of the file system be with you!

vpswala Admin

Savita Sathe is an experienced writer and editor with over 10+ years of professional experience creating engaging content across industries. Whether it's blogs, whitepapers, website copy, emails, social media posts, or more. She develops effective hosting and tech content that helps brands connect with their audiences and achieve business goals.

Leave a reply

Your email address will not be published. Required fields are marked *