How to Make New Directories in Linux

  1. Understanding the mkdir Command
  2. Creating Parent Directories
  3. Using Git to Manage Directories
  4. Creating Nested Directories
  5. Conclusion
  6. FAQ
How to Make New Directories in Linux

Creating new directories in Linux is a fundamental skill that every user should master. Whether you’re organizing files, managing projects, or collaborating with others, knowing how to create directories efficiently can streamline your workflow. The primary command used for this task is mkdir, which stands for “make directory.”

In this article, we will explore various methods to create directories in Linux, including some tips for using Git in conjunction with these commands. By the end, you’ll be well-equipped to navigate the Linux file system with confidence.

Understanding the mkdir Command

The mkdir command is the cornerstone for creating directories in Linux. It allows users to create new folders in the file system. The basic syntax of the command is straightforward:

mkdir [options] directory_name

Basic Directory Creation

To create a simple directory, you can use the following command:

mkdir my_directory

Output:

(no output)

When you run this command, a new directory named “my_directory” will be created in your current working directory. If you check your directory listing with ls, you’ll see your new folder there.

Creating directories is not just about naming them; you can also create multiple directories at once. For example:

mkdir dir1 dir2 dir3

Output:

(no output)

This command creates three directories named “dir1,” “dir2,” and “dir3” simultaneously. This feature is particularly useful when you want to set up a project structure quickly.

Creating Parent Directories

Sometimes, you may want to create a directory along with its parent directories in one go. The -p option allows you to do just that. For instance:

mkdir -p parent_directory/child_directory

Output:

(no output)

In this example, the command creates “parent_directory” and “child_directory” inside it if they do not already exist. This is a great way to ensure that your directory structure is set up correctly without having to create each parent directory one at a time.

Using Git to Manage Directories

If you’re working with Git, you might want to create directories in the context of a repository. This can be done using the same mkdir command. For example, if you want to create a directory for your project files, you can navigate to your repository and execute:

mkdir project_files

Output:

(no output)

After creating the directory, you can add it to your Git repository:

git add project_files

Output:

(no output)

This command stages the new directory for the next commit. Remember, Git tracks files, not directories. So, to ensure your new directory is included in the repository, you need to place at least one file inside it before committing.

Creating Nested Directories

Creating nested directories can be very useful for organizing your work. You can use the -p option with mkdir to create multiple levels of directories at once. For example:

mkdir -p projects/python_scripts

Output:

(no output)

This command creates a “projects” directory and a “python_scripts” directory within it. This structure is particularly helpful when you are managing multiple projects and want to keep your files organized.

Conclusion

Creating new directories in Linux using the mkdir command is a straightforward yet powerful skill. Whether you’re working on personal projects, collaborating with others, or managing files in a Git repository, understanding how to create and manage directories effectively can enhance your productivity. By utilizing options like -p, you can create complex directory structures with ease. Now that you have the knowledge, you can navigate the Linux file system like a pro!

FAQ

  1. How do I create multiple directories at once?
    You can create multiple directories by listing them in the mkdir command, like this: mkdir dir1 dir2 dir3.

  2. What does the -p option do in the mkdir command?
    The -p option allows you to create parent directories as needed. For example, mkdir -p parent/child will create both if they do not exist.

  3. Can I create directories in a Git repository?
    Yes, you can create directories in a Git repository using the mkdir command, just as you would in any other directory.

  4. Why do I need to add files to a new directory in Git?
    Git tracks files, not empty directories. To include a new directory in a commit, you must have at least one file within it.

  5. How can I check if my directory was created successfully?
    You can use the ls command to list the contents of your current directory and verify that your new directory is present.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Files and Directories