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:
Section titled “mkdir Syntax:”mkdir [option] [directory_name]
Options:
Section titled “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:
Section titled “Parameters:”Parameter | Description |
---|---|
directory_name | Name of the directory to be created |
mkdir Command Samples:
Section titled “mkdir Command Samples:”Create a New Directory
Section titled “Create a New Directory”mkdir new_directory
Creates a new directory named “new_directory” in the current location.
Create Nested Directories
Section titled “Create Nested Directories”mkdir -p parent_directory/child_directory
Creates a parent directory named “parent_directory” and a nested child directory named “child_directory” within it.
Verbose Mode
Section titled “Verbose Mode”mkdir -v directory1 directory2 directory3
Creates multiple directories (“directory1”, “directory2”, “directory3”) and displays a message for each directory created.
Specify Permissions
Section titled “Specify Permissions”mkdir -m 755 secure_directory
Creates 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
Section titled “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
Section titled “Create Directory with Parent”mkdir -p parent_directory/child_directory/grandchild_directory
Creates directories starting with “parent_directory” followed by “child_directory”, and finally “grandchild_directory” as a nested directory.
Create Directories with Specific Group
Section titled “Create Directories with Specific Group”mkdir -g developers project1 project2
Creates directories for projects “project1” and “project2” with the group ownership set to “developers”.
mkdir FAQ:
Section titled “mkdir FAQ:”How do I use mkdir in Linux?
Section titled “How do I use mkdir in Linux?”To use the mkdir command in Linux, execute the following command:
mkdir directory_name
What is the purpose of the -p option in mkdir?
Section titled “What 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_directory
How can I set permissions for a directory while creating it with mkdir?
Section titled “How 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_directory
How can I create multiple directories at once with mkdir?
Section titled “How 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 directory3
How do I prevent mkdir from displaying error messages if the directory already exists?
Section titled “How 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_directory
Can I use mkdir to create directories with spaces in their names?
Section titled “Can 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
Section titled “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