Creating Directories in Linux: A Comprehensive Guide

create directory linux

If you’re new to Linux, creating directories might seem like a daunting task. But don’t worry, it’s actually quite simple once you get the hang of it. In this guide, we’ll walk you through the process of creating directories in Linux and cover related concepts and methods that will help you better understand the topic.

What is a Directory?

A directory, also known as a folder, is a container used to organize files on a Linux file system. Directories can contain other directories and files, and they can be nested to form a hierarchical structure. Directories are an essential component of Linux file systems and are used to keep files organized and easily accessible.

Creating a Directory

To create a directory in Linux, you can use the mkdir command. The mkdir command stands for “make directory” and is used to create new directories. Here’s the basic syntax:

mkdir [options] directory_name

Let’s break down this command:

  • mkdir is the command used to create a new directory.
  • [options] are optional arguments that can be used to modify the behavior of the command.
  • directory_name is the name of the new directory you want to create.

For example, to create a new directory called “my_directory”, you would use the following command:

mkdir my_directory

This will create a new directory called “my_directory” in the current working directory. If you want to create a directory in a different location, you can specify the full path to the directory:

mkdir /path/to/my_directory

This will create a new directory called “my_directory” in the “/path/to/” directory.

Options for Creating Directories

The mkdir command has several options that you can use to modify its behavior. Here are some of the most commonly used options:

  • -p: This option allows you to create nested directories. For example, if you want to create a directory called “my_directory” inside a directory called “parent_directory”, you can use the following command:
mkdir -p parent_directory/my_directory

This will create a new directory called “parent_directory” if it doesn’t already exist, and then create a new directory called “my_directory” inside it.

  • -m: This option allows you to set the permissions of the new directory. For example, if you want to create a new directory called “my_directory” with read, write, and execute permissions for the owner, but only read and execute permissions for everyone else, you can use the following command:
mkdir -m 755 my_directory

This will create a new directory called “my_directory” with the following permissions:

drwxr-xr-x  2 user user 4096 Jul  7 14:17 my_directory
  • -v: This option enables verbose mode and displays a message for each directory that is created. For example, if you want to create a new directory called “my_directory” and display a message when it’s created, you can use the following command:
mkdir -v my_directory

This will create a new directory called “my_directory” and display a message like the following:

mkdir: created directory 'my_directory'

Conclusion

Creating directories in Linux is a straightforward process that can be done using the mkdir command. By using the options available with the command, you can create nested directories, set permissions, and enable verbose mode to display messages when directories are created. With this guide, you should now have a better understanding of how to create directories in Linux and be able to use this knowledge in a professional setting.