mkdir Command to Create Directory in Linux with Examples
Overview
Creating directories in the Linux operating system is a fundamental task that allows you to organize your files and folders efficiently. A directory, also known as a folder, serves as a container for grouping related files and subdirectories. In this article, we will explore the various methods to create directories in Linux using the mkdir command.
How to Create a Directory in Linux?
To create a directory in Linux, you can make use of the mkdir command. This command stands for "make directory" and is specifically designed for creating new directories. The mkdir command is easy to use and offers several options to customize the directory creation process.
What is mkdir Command in Linux?
The mkdir command is a standard utility in Linux that allows users to create directories. It is a command-line tool that is available on most Linux distributions. The basic syntax for using the mkdir command is as follows:
Syntax
- mkdir:
The command itself. - options:
Additional flags that modify the command's behaviour. - directory_name:
The name of the directory you want to create.
Options
The mkdir command offers various options that enhance its functionality. Some commonly used options include:
- -p or --parents:
This option allows you to create parent directories as needed. If any of the intermediate directories don't exist, they will be created. - -m or --mode:
This option lets you set the permissions for the newly created directory. You can specify the permissions using either symbolic or numeric notation. - -v or --verbose:
This option enables verbose output, displaying a message for each directory that is created. - -h or --help:
This option provides a help message that explains the usage and available options of the mkdir command.
Examples
Let's look at some examples to understand how the mkdir command works:
- To create a directory named "documents" in the current working directory, you can use the following command:
This command creates a directory named "documents" within the current directory.
- If you want to create a directory with a specific set of permissions, you can use the -m option followed by the permission mode. For example, to create a directory named "public" with read and write permissions for the owner, read permissions for the group, and no permissions for others, you can use the following command:
This command creates a directory named "public" with the specified permissions.
- To create a directory with multiple levels of parent directories, you can use the -p option. For instance, suppose you want to create a directory structure like "photos/2023/summer". You can use the following command:
This command creates the directories "photos", "2023", and "summer" if they don't already exist.
Creating Parent Directories
By default, the mkdir command only creates the specified directory. However, if you need to create a directory along with its parent directories, you can use the -p or --parents option. This option ensures that all the intermediate directories are created if they don't already exist.
For example, consider the following command:
If the directories "/home/user/documents" don't exist, the mkdir command will create them along with the "reports" directory. This is useful when you want to create a complex directory structure without having to manually create each parent directory.
Setting Permissions
When creating a directory using the mkdir command, you can also specify the permissions for the directory using the -m or --mode option. Permissions determine who can access, modify, or execute files and directories. There are two common ways to specify permissions: symbolic notation and numeric notation.
Symbolic Notation
In symbolic notation, you can use a combination of letters and symbols to represent the permissions. The basic symbols used are:
- r: Read permission
- w: Write permission
- x: Execute permission
- -: No permission
To set the permissions using symbolic notation, you can use the following format:
For example, to create a directory named "private" with read and write permissions for the owner, read permissions for the group, and no permissions for others, you can use the following command:
Numeric Notation
In numeric notation, you use three digits to represent the permissions. Each digit corresponds to a specific user category: owner, group, and others. The digits can have values from 0 to 7, representing different combinations of read, write, and execute permissions.
- 0: No permissions
- 1: Execute permission
- 2: Write permission
- 3: Write and execute permissions
- 4: Read permission
- 5: Read and execute permissions
- 6: Read and write permissions
- 7: Read, write, and execute permissions
To set the permissions using numeric notation, you can use the following format:
For example, to create a directory named "shared" with read, write, and execute permissions for the owner, read and execute permissions for the group, and read permissions for others, you can use the following command:
How to Create Multiple Directories?
The mkdir command also allows you to create multiple directories simultaneously. You can specify multiple directory names separated by spaces, and the command will create all the specified directories.
For example, to create three directories named "folder1", "folder2", and "folder3", you can use the following command:
This command will create all three directories within the current working directory.
Additional Info related to Directories
Removing Directories
In addition to creating directories, it's important to understand how to remove them when they are no longer needed. The rmdir command is used to delete empty directories in Linux. However, it's important to note that the directory must be empty for the rmdir command to successfully remove it.
To remove a directory using the rmdir command, use the following syntax:
For example, to remove a directory named "folder1", you would execute the following command:
If the directory contains any files or subdirectories, the rmdir command will display an error message and refuse to remove the directory. In such cases, you can use the rm command with the -r or -rf option to remove directories recursively, along with their contents.
Caution should be exercised when using the rm command with the -r or -rf option, as it permanently deletes the directory and all its contents, without any confirmation prompts. Ensure that you double-check the command before executing it to prevent accidental data loss.
Navigating and Accessing Directories
Once you have created directories in Linux, it is essential to know how to navigate and access them. The cd command (Change Directory) is used to move between directories in the command-line interface.
To navigate to a specific directory, use the following syntax:
For example, to navigate to a directory named "documents" within the current directory, you would execute the following command:
If the directory is located in a different location, you can provide the absolute or relative path to access it. Here are a few examples:
- Absolute path:
cd /home/user/documents (navigates to the "documents" directory in the user's home directory) - Relative path:
cd ../parent_directory (moves up one level in the directory structure and then navigates to the "parent_directory")
To navigate to the parent directory, you can use the following command:
This command moves up one level in the directory structure.
To navigate to the home directory of the current user, use the tilde (~) symbol:
Once you are in a directory, you can use other commands to perform operations on files or subdirectories within that directory.
Renaming Directories
In addition to creating and removing directories, Linux provides the ability to rename directories. The mv command (move) can be used to rename directories by moving them to a new location with a different name.
To rename a directory using the mv command, use the following syntax:
For example, let's say you have a directory named "folder1" and you want to rename it to "new_folder". You would execute the following command:
The mv command will rename the directory from "folder1" to "new_folder" within the same location.
If you want to move and rename the directory simultaneously, you can provide the destination path along with the new name. Here's an example:
For instance, to rename a directory named "folder2" and move it to a different location, you would use the following command:
This command renames the "folder2" directory to "new_folder2" and moves it to the "/home/user/documents" directory.
Keep in mind that the mv command can also be used to move directories to different locations without renaming them. This flexibility allows for efficient management of directory names and locations within the Linux system.
Conclusion
- Creating directories in Linux is a straightforward process using the mkdir command.
- By understanding the options and syntax of the mkdir command, you can efficiently create directories and organize your files and folders.
- Whether it's creating a single directory, multiple directories, or setting specific permissions, the mkdir command provides the flexibility you need to manage your directory structure effectively.
- We also covered some important topics like removing directories, changing directories and renaming directories.