The mkdir command in Unix and Linux is used to create directories (folders) within the file system. It is a straightforward command for creating new directories at specified locations.
The basic syntax for the mkdir command is:
mkdir [options] directory...options: Command-line options to control the behavior ofmkdir.directory: The name(s) of the directory(ies) to be created.
To create a single directory:
mkdir mydirThis command creates a directory named mydir in the current working directory.
To create multiple directories at once:
mkdir dir1 dir2 dir3This command creates directories dir1, dir2, and dir3 in the current working directory.
To create nested directories (directories within directories):
mkdir -p parentdir/subdirThis command creates a directory named parentdir if it doesn't exist, and within it, creates a directory named subdir.
To create parent directories as needed:
mkdir -p path/to/parent/dir/newdirThis command creates the entire directory structure path/to/parent/dir/ if it doesn't exist, and then creates newdir within it.
To set permissions for the newly created directory:
mkdir -m 755 newdirThis command creates newdir with permissions set to 755 (read, write, execute for owner, read and execute for group and others).
To create directories for organizing project files:
mkdir project
cd project
mkdir src docs testsThis sequence of commands creates a project directory and within it, creates src, docs, and tests directories.
To create temporary workspaces for specific tasks:
mkdir workspaces/task1
mkdir workspaces/task2This command creates separate directories for different tasks within a workspaces directory.
The mkdir command is a simple yet essential tool for creating directories in Unix and Linux environments. Its ability to create nested directories (-p option) and set permissions (-m option) provides flexibility for various use cases, from organizing files to creating temporary workspaces. Understanding these options and practical use cases can help you efficiently manage directory structures in your file system.
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit