Learning the fundamentals of Linux commands is necessary for effective file management and seamless navigation. An example of such a basic command is mkdir, or “make directory.” Users of Linux systems can easily create directories or folders with mkdir. Becoming adept in Linux administration begins with understanding how to create directories with mkdir, whether you’re a newbie to Linux or an experienced user trying to improve your command-line skills. To determine what actions should be taken or just have to free up disk space first finding the size of the directory in Linux is necessary.
This tutorial will show you how to use the mkdir command to create directories in Linux, a straightforward yet effective method. In Linux/Unix, the mkdir command is a command-line tool that lets users make new folders. “Makdir” is an acronym for “make directory.” Along with many other features, mkdir allows you to create many folders at once and specify permissions.
Required Conditions
- A system similar to UNIX or Linux.
- Ability to use a command line or terminal.
- An individual with the ability to create and modify directory settings.
Linux Command Syntax for mkdir
The syntax for the mkdir command is as follows:
mkdir [options] dir_name
- The [options] section changes how mkdir behaves and is optional. The section below contains a list of the available options.
- The name or names of the directories you wish to create are contained in the dir_name. Multiple directory names, separated by spaces, can be used.
Linux Command Options for mkdir
You can change how the mkdir command behaves by using the arguments. The most popular mkdir options are included in the table below along with their descriptions:
- -p : if parent directories aren’t there, creates them.
- -m a = [rwx] [dir_name]: sets the new directory’s file modes, or permissions.
- -v: shows a notification for every directory that is created.
- –version: shows the license details and version number before exiting.
- -Z: Sets the SELinux security context for directories.
- -help: Assists with details regarding mkdir options.
mkdir Linux Samples
Here are some real-world examples to assist you better grasp how mkdir functions.
Example 1: In Linux, Create a New Directory
Use the terminal to create a directory by passing the appropriate directory name to the mkdir command.
We build a directory called Linux in this example. Linux commands and options are case-sensitive, so be mindful while capitalizing them:
mkdir Linux
The terminal displays an empty line if the procedure is successful. Use the ls command to confirm the creation of the directory:
ls -l
Example 2: Establish a Directory in a Particular Place
Give the complete directory path when creating a directory or directories anywhere other than your current working directory. As an illustration:
mkdir /tmp/example
In the given location, the command creates a directory example. Now let’s navigate to that directory and make sure:
cd /tmp ls -l
The directory was formed by listing its contents.
Example 3: Creating Several Directories
Using mkdir, you can create directories one at a time, but if you need to create more than a few, it can take a while. To prevent that, use a single mkdir command and list the directory names separated by a space
Example:
mkdir dir1 dir2 dir3
As an illustration: The three directories listed are created by the aforementioned command.
Nevertheless, you may also create several directories by using mkdir with curly brackets {}. To construct many directories, for instance, enumerate them without gaps between curly brackets:
mkdir {test1,test2,test3}
Using the same approach, you can also create a batch of folders. To define the number of directories, use the curly brackets prefixed with the pattern. As an illustration:
mkdir dir{1..15}
From dir1 to dir15, 15 directories are created by the above operation.
The directory names should not have any spaces added to the curly brackets. The names in question will have additional characters if you do.
Example 4: Using Directory Names to Incorporate Environment Variables
You can also utilize environment variables in directory names when using mkdir. The current user is used as the directory name when we create a directory in the example that follows:
mkdir $USER
Listing the contents of the directory reveals that the current user’s name was used when the directory was established.
Example 5: How Parent Directories Are Made
When using mkdir to build a structure with numerous subdirectories, the -p option must be added. This guarantees that mkdir adds any parent directories that are missing during the procedure.
For example, use the following command to create dirtest2 in dirtest1 within the Linux directory (Linux/dirtest1/dirtest2):
mkdir –p Linux/dirtest1/dirtest2
To view the recursive directory tree, use ls -R.
If one of the folders in the string doesn’t exist, the terminal will return an error without the -p option.
Example 6: How to Create a Directory and Set Permissions
By default, the mkdir command grants the current user read, write, and execute (rwx) rights.
When creating a directory, use the user 777 and the -m option to add all user permissions.
To create a directory named DirM with rwx permissions, run the following command:
mkdir –m777 DirM
Run this to display the permissions sets and a list of all directories:
ls -l
Example 7: How to Check Listings
There is no confirmation when mkdir commands are executed that the operation was accomplished. Add the -v argument to the command to view the mkdir process’s details.
Let’s print the operation status and make a Details directory:
You may confirm that the directory was created without running the ls command by using the feedback obtained from the process.
Final Wordings on Creating Directory in Linux | mkdir Command
To sum up, knowing how to utilize the mkdir command is essential for any Linux user. Understanding how to construct directories efficiently can save you time and effort when managing the structure of your system, organizing your files, or setting up directories for a new project. You have gained an understanding of the fundamentals of using the Linux mkdir command to create directories by following the instructions provided in this book.
You can confidently navigate and administer your system as you gain a deeper understanding of Linux commands via practice and discovery. So explore your Linux environment’s full potential by experimenting with mkdir.