How to Create a Directory in Linux? (A Comprehensive Guide: Create a Folder in Linux)
In the Linux system, a directory is a location in which the user stores files and subdirectories. In contrast, Windows users use the word folder instead of a directory. The directory-oriented file system is used in the Linux/Unix operating system. The beginner or Linux system administrator uses some set of commands to create and manage directories.
Linux is highly favored and utilized for its stability and adaptability in simplifying a variety of tasks. A standout feature of Linux is its terminal, offering the ability to execute commands for tasks ranging from system modifications to downloading required software.
In this tutorial, we will explore different commands and options to create a directory or folder using the terminal. In addition, we will demonstrate how to create a directory in Linux and multiple directories using the āmkdirā command on a Linux system.
Prerequisites
Before using the āmkdirā command on a Linux system, make sure you have the following prerequisites:
1. Running Linux or Unix-like operating systems and having access to the command-line tool āterminalā for running Linux commands.
2. You should have āsudoā and read/write access to create new directories.
Linux āmkdirā Command (Make Folder in Linux)
The Linux create folder command āmkdirā is used to create a directory on a Linux system. This command allows you to create single or multiple directories at the same time on your system. Moreover, it enables you to set permissions or grant access privileges to a specific directory.
It is important to note that each user should have read/write permissions on a parent directory to create subdirectories or folders in their Linux system. Otherwise, the user will receive a āpermission deniedā error.
Basic Syntax of the āmkdirā Command
The basic syntax of Linux make directory using the āmkdirā command is as follows:
$ mkdir [option] [directories]
How to Create a Directory in Linux? (Linux Create Directory)
The āmkdirā command in Linux is used to create a new directory or a folder. To create a directory, execute the āmkdirā command and specify the directory name in the following way:
$ mkdir directory_name
For example, if you want to create a new directory, you will run the āmkdirā command with the directory name:
$ mkdir testdirectory
The above command creates a new directory with that name in the current working directory. You can display the directory path on the terminal using the āpwdā command:
$ pwd
If you want to create a new directory in another directory instead of the current working directory, you will provide the path to create a directory in a specific location.
$ mkdir /path-to-directory directory_name
$ mkdir /home/samreena/Documents/testdirectory
The above command creates a directory named "testdirectory" at the specified directory path in Linux.
Linux āmkdirā Command with Options
The Linux āmkdirā command allows you to create a new folder or directory with different options. This command offers various options to meet specific requirements. In Linux, it allows us to specify the number of directories to be created simultaneously.
In the following table, we have listed some useful āmkdirā commands with various options that will help you create folders in the Linux system:
How to Create Multiple Directories on a Linux System
Using the āmkdirā command, you can create multiple folders in Linux inside a directory. If you want to create multiple directories in the same directory, you can utilize the āmkdir" command to create them individually. However, executing separate commands for each directory can be time-consuming.
To create multiple folders or directories in the same directory, use the āmkdirā command with the directory names separated by commas:
$ mkdir {dir1,dir2,dir3}
The above command creates three different subdirectories in the current working directory.
Display Verbose or Process Information After Creating a Folder in Linux (Linux Make Directory with Option -v)
Once you create a directory in Linux, it doesnāt show any confirmation message on the terminal by default. If you want to display details of the running command that shows whether the directory has been successfully created on your system or not, you can use the option ā-vā with the āmkdirā command.
This option stands for "verbose" and provides more feedback about the operation. You donāt need to run the ālsā command to view the directories or folders. Letās look at an example:
$ mkdir -v test_directory
When you run the above command, you'll see an output displaying information about the creation of the directory. If the operation is successful, you will see the following output:
How to Create a Directory in Linux With a Parent Directory (Make Folder in Linux with Option -p)
Use the option ā-pā with the āmkdirā command if you want to create a parent directory structure with multiple subdirectories. This option includes any missing parent directories during the creation process.
For example, suppose you want to add a directory named "testdir2" within the directory "testdir1" in the āpersonalā directory on the Linux system. In that case, you will need to specify the complete directory path with the āmkdirā command. Use the following command to demonstrate this:
$ mkdir āp /home/samreena dirparent/dirtest2
After executing the above āmkdirā command with the complete path, you can use the "ls" command along with the option "-R" option to verify the directory's creation. This option facilitates the display of a recursive directory tree, showcasing the contents of each directory within the specified path.
How to Set Permissions While Creating a Folder in Linux
When you create a directory, it typically receives default "rwx" (read, write, execute) permissions, but these permissions are initially only granted to the user who created the directory. If you wish to modify the permissions for all users, you can utilize the "-m" option in conjunction with the āmkdirā command.
In the example below, we're granting permissions to the directory as "777," allowing any user to read, write, and execute the directory if necessary.
$ mkdir -m 777 mytestdir
After creating the "mytestdir" directory with the specified permissions, you can use the "ls -l" command to list the contents of the directory with detailed information, including permissions.
$ ls -l
Now, you will get a list of all directories and their permissions.
Create a Folder in Linux Using Shell Script
A shell script generates the executable files through a series of commands. One of these commands is the āmkdirā command that you can use in a shell script.
To illustrate, let's consider a script that takes a specified path as input. Upon execution, this script will generate a folder and include a text file named "script" using the touch command.
#!/bin/bash
mkdir testdir
cd testdirtouch script
Every script you create should begin with the first line (#!/bin/bash). This start line in a file indicates that it is a bash script.
The āmkdirā command is used to generate a folder. Replace "testdir" with your desired directory name. The $@, also known as input parameters, placed at the end of the second and third lines, gets substituted with the value you provide when executing the script.
The ācdā command switches to the specified directory, and ātouchā is used to create an empty file named "script."
To execute the āmkdirā command, use the following:
$ nano mkdir.sh
$ ./mkdir.sh
Thatās all about the āmkdirā command. In Linux, most of the distributions have a built-in GUI interface. Therefore, you can also create a folder or Linux directory using the graphical user interface.
Executing the āmkdirā command with the ā--helpā option will display a detailed list of available options along with their respective descriptions, and subsequently, it will terminate the operation.
$ mkdir --help
As you can see in the screenshot above, all options and āmkdirā command usage are displayed on the terminal after executing the above-mentioned command.
Conclusion
In this article, we demonstrated how to create a folder and multiple directories in Linux using the āmkdirā command. If you are using a Linux VPS server, by utilizing the āmkdirā command with the specified options, youāll be able to make changes to the standard āmkdirā command's functionality and alterations to the output.
So, always use the proper option selection for specific scenarios to achieve accurate results. Given that options are case-sensitive, using the correct option is crucial to avoid errors. To learn more about the usage of the āmkdirā command, visit the āmkdirā main page.