In Linux, the “mkdir” command is used to create new directories (folders) within the file system. It allows you to organize your files and create a directory structure. Here’s how to use the “mkdir” command:
- Open a Terminal:
Launch a terminal emulator on your Linux system.
- Type the “mkdir” command:
Simply type “mkdir” followed by the name or names of the directories you want to create. For example, to create a directory named “mydir”, run:
You can also create multiple directories at once by specifying their names separated by spaces. For example, to create two directories named “dir1” and “dir2”, run:
mkdir dir1 dir2
- Create a Directory Hierarchy:
If you want to create a directory hierarchy with multiple levels, you can use the “-p” option. For example, to create a directory named “parent” that contains a subdirectory named “child”, run:
mkdir -p parent/child
The “-p” option ensures that any intermediate directories that do not exist will be created as well.
- Specify Directory Permissions:
By default, the “mkdir” command creates directories with default permissions. However, you can specify custom permissions using the numeric or symbolic mode with the “chmod” command. For example, to create a directory named “mydir” with specific permissions, run:
mkdir -m 755 mydir
The “-m” option followed by the numeric mode sets the permissions for the newly created directory.
- Create Directories with Parent Directory:
If you want to create a directory within an existing parent directory, you can use the parent directory’s path followed by a forward slash (/) and the name of the new directory. For example, to create a directory named “child” within an existing directory named “parent”, run:
This command will create the “child” directory inside the “parent” directory.
- Verifying the Created Directory:
After running the “mkdir” command, you can verify that the directory has been created by using the “ls” command to list the contents of the current directory. For example:
You should see the newly created directory listed.
- Exiting the Command:
The “mkdir” command doesn’t produce any output unless there is an error. Once you create the desired directories, you can continue executing other commands or exit the terminal as needed.
The “mkdir” command is a fundamental tool for creating directories in Linux. By using different options and specifying directory names, you can easily create single or multiple directories, create directory hierarchies, and organize your files within the file system.