Signup/Sign In

How to create a directory in Linux with mkdir command?

mkdir (make directories) - In Linux/Unix operating system mkdir command is used to make new directories if they do not already exist.

  • mkdir stands for make directories.
  • mkdir is also used in IBM OS, Microsoft Windows, DOS for the same purpose.
  • It is also available in EFI (Extensible Firmware Interface) shell and PHP scripting language.
  • While executing the mkdir command must have permission to create a directory in the parent directory.
  • mkdir command can create multiple directories and set permission for the directories.

The general syntax of mkdir command.

mkdir [OPTION]... DIRECTORY...

Brief description of options available with the 'mkdir' command.

Options Description
-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
--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

Create a directory using mkdir command in Linux/Unix with mkdir in Linux.

for creating a directory in Linux using terminal we need to run this command mkdir <directory name>

Create a directory using mkdir command in Linux/Unix

Change the permission mode of directories using -m, --mode=MODE with mkdir in Linux.

In this example, using mkdir -m a=r newfolder a directory is created and only allows users to read from the directory.

Change the permission mode of directories using -m, --mode=MODE.

Create a directory with no error if the directory already existed with mkdir in Linux.

In this example, a directory already exists with 'hope' name and no error is showing.

Create a directory with no error if the directory already existed.

Conclusion

In this tutorial, we covered how we can create a directory or create multiple directories in Linux. We also learned how we can create parent directories along with creating a directory if the parent directories are not already present.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.