Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a new directory in Linux using the terminal?
The mkdir command is used to create a new directory in the Linux/Unix operating system. It can create single or multiple directories at once and set permissions during creation, similar to the chmod command.
Syntax
The general syntax of the mkdir command is as follows −
$ mkdir [OPTION]... [DIRECTORIES]...
Command Options
| Option | Description |
|---|---|
| -m, --mode=MODE | Set file permissions at the time of directory creation (like chmod) |
| -p, --parents | Create parent directories as needed; no error if directory exists |
| -v, --verbose | Display messages showing what is being created |
| -Z | Set the SELinux security context of each created directory |
| --help | Display help message and exit |
| --version | Show version information and exit |
Creating a Single Directory
To create a new directory in the current location, use the basic mkdir command −
$ mkdir newdir
This creates a directory named newdir in the current working directory.
Creating Multiple Directories
You can create multiple directories at once by specifying multiple names −
$ mkdir dir1 dir2 dir3 dir4
Creating Parent and Child Directories
To create nested directory structures, use the -p option with -v for verbose output −
$ mkdir --parent --verbose parent/child
mkdir: created directory 'parent' mkdir: created directory 'parent/child'
This creates both the parent directory and child directory in one command.
Setting Permissions During Creation
To create a directory with specific permissions, use the -m option −
$ mkdir -m 755 newdir
This creates a directory with read, write, and execute permissions for the owner, and read and execute permissions for group and others.
Checking Version and Help
To view version information −
$ mkdir --version
To display detailed help information −
$ mkdir --help
Common Examples
Here are practical examples combining different options −
# Create directory with 744 permissions and verbose output $ mkdir -m 744 -v myproject # Create nested structure with intermediate directories $ mkdir -p project/src/main/java # Create multiple directories with permissions $ mkdir -m 755 docs images scripts
Important Notes
You must have write permissions in the parent directory to create new directories
Directory names are case-sensitive in Linux
Avoid spaces in directory names or use quotes if necessary
Conclusion
The mkdir command is essential for directory management in Linux systems. It provides flexible options for creating single directories, multiple directories, nested structures, and setting permissions during creation. Understanding these options helps organize files efficiently in the Linux filesystem.
