mkdir Linux Command Guide
The Linux mkdir command is used to create directories in the file system. It allows users to specify the names of the directories they want to create and can also create multiple directories at once. By using the mkdir command, users can organize their files and data efficiently. Overall, the mkdir command is a simple yet powerful tool for managing directories in a Linux environment.
mkdir Syntax:
mkdir [option] [directory_name]Options:
| Option | Description |
|---|---|
| -m | Set file mode (permissions) of the created directory |
| -p | Create parent directories as needed |
| -v | Print a message for each created directory |
Parameters:
| Parameter | Description |
|---|---|
| directory_name | Name of the directory to be created |
mkdir Command Samples:
Create a New Directory
mkdir new_directoryCreates a new directory named “new_directory” in the current location.
Create Nested Directories
mkdir -p parent_directory/child_directoryCreates a parent directory named “parent_directory” and a nested child directory named “child_directory” within it.
Verbose Mode
mkdir -v directory1 directory2 directory3Creates multiple directories (“directory1”, “directory2”, “directory3”) and displays a message for each directory created.
Specify Permissions
mkdir -m 755 secure_directoryCreates a directory named “secure_directory” with permissions set to 755 (read, write, execute for owner; read and execute for group and others).
Create Directory with Spaces
mkdir "directory with spaces"Creates a directory with a name containing spaces by encapsulating the directory name within double quotes.
Create Directory with Parent
mkdir -p parent_directory/child_directory/grandchild_directoryCreates directories starting with “parent_directory” followed by “child_directory”, and finally “grandchild_directory” as a nested directory.
Create Directories with Specific Group
mkdir -g developers project1 project2Creates directories for projects “project1” and “project2” with the group ownership set to “developers”.
mkdir FAQ:
How do I use mkdir in Linux?
To use the mkdir command in Linux, execute the following command:
mkdir directory_nameWhat is the purpose of the -p option in mkdir?
The -p option in mkdir is used to create parent directories along with the specified directory. This is useful when you want to create a directory and its parent directories if they do not already exist.
mkdir -p path/to/parent_directory/new_directoryHow can I set permissions for a directory while creating it with mkdir?
You can set permissions for a directory while creating it by using the chmod command in combination with the mkdir command. Here is an example:
mkdir new_directory && chmod 755 new_directoryHow can I create multiple directories at once with mkdir?
To create multiple directories at once with mkdir, you can specify the directory names separated by spaces in a single command. Here is an example:
mkdir directory1 directory2 directory3How do I prevent mkdir from displaying error messages if the directory already exists?
You can prevent mkdir from displaying error messages if the directory already exists by using the -p option. This option ensures that no error message is shown if the directory is already present.
mkdir -p existing_directoryCan I use mkdir to create directories with spaces in their names?
Yes, you can create directories with spaces in their names by enclosing the directory name in quotes. Here is an example:
mkdir "directory with spaces"Applications of the mkdir command
- Create a new directory
- Create multiple directories at once
- Create directories with specific permissions
- Create parent directories as needed with the -p option
- Create directories with symbolic links or absolute paths
- Create nested directories efficiently