{"id":17376,"date":"2026-02-11T11:06:28","date_gmt":"2026-02-11T05:36:28","guid":{"rendered":"https:\/\/www.youstable.com\/blog\/?p=17376"},"modified":"2026-02-11T11:06:41","modified_gmt":"2026-02-11T05:36:41","slug":"mkdir-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.youstable.com\/blog\/mkdir-command-in-linux","title":{"rendered":"Mkdir Command in Linux Explained With Examples in 2026"},"content":{"rendered":"\n<p>The mkdir command in Linux creates new directories. Its basic form is mkdir DIRECTORY, and you can create multiple folders at once, set permissions with -m, build nested paths with -p, and print output with -v. <\/p>\n\n\n\n<p>It respects your umask, returns errors if a path exists without -p, and supports advanced patterns like brace expansion. Creating directories is one of the first things you do on any Linux system. <\/p>\n\n\n\n<p>In this guide, we\u2019ll explain the mkdir command in Linux with clear examples, best practices, and troubleshooting tips. By the end, you\u2019ll confidently create single, multiple, and nested directories with the right permissions, whether locally or on a server at YouStable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"what-is-mkdir-in-linux\">What is mkdir in Linux?<\/h2>\n\n\n\n<p>mkdir (make directory) is a core Linux utility that creates one or more directories. It\u2019s part of GNU coreutils on most distributions and is also available in BSD variants (macOS). It\u2019s often used in deployment scripts, <a href=\"https:\/\/www.youstable.com\/blog\/what-is-ci-cd-on-linux-server\">CI\/CD pipelines<\/a>, and everyday file management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"syntax-and-common-options\">Syntax and Common Options<\/h3>\n\n\n\n<p>Here\u2019s the standard syntax and the options you\u2019ll use most:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir &#91;OPTIONS] DIRECTORY &#91;DIRECTORY...]\n# Common options:\n# -p, --parents        Create parent directories as needed, no error if existing\n# -m, --mode=MODE      Set permissions on the new directory (e.g., 755 or u=rwx,go=rx)\n# -v, --verbose        Print a message for each created directory\n# -Z, --context=CTX    Set SELinux security context (on SELinux-enabled systems)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"basic-usage-examples\">Basic Usage Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"create-a-single-directory\">Create a Single Directory<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir reports<\/code><\/pre>\n\n\n\n<p>If \u201creports\u201d already exists, mkdir returns an error unless you use -p (explained below).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"create-multiple-directories-at-once\">Create Multiple Directories at Once<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir january february march<\/code><\/pre>\n\n\n\n<p>This creates three directories in the current working path. You can also use absolute paths.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"create-nested-directories-with-p\">Create Nested Directories with -p<\/h3>\n\n\n\n<p>Use -p to create parent directories as needed and avoid errors if paths already exist.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p \/var\/www\/project\/{public,logs,backup}<\/code><\/pre>\n\n\n\n<p>Brace expansion ({&#8230;}) is handled by your shell (e.g., bash), not mkdir. It expands into three directories under \/var\/www\/project.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"setting-permissions-with-m-and-understanding-umask\">Setting Permissions with -m (and Understanding umask)<\/h2>\n\n\n\n<p>By default, new directory permissions are influenced by your umask. Typical defaults are 022 or 002 depending on the distro and policy. You can check your current umask with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>umask<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"set-numeric-permissions\">Set Numeric Permissions<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># 755 = owner rwx, group rx, others rx\nmkdir -m 755 app\n\n# 700 = owner rwx only\nmkdir -m 700 secrets<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"set-symbolic-permissions\">Set Symbolic Permissions<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -m u=rwx,go=rx shared\nmkdir -m u=rwx,g=rx,o= private_team<\/code><\/pre>\n\n\n\n<p><strong>Note: <\/strong>The -m value overrides the default umask for that command only. If you need to adjust defaults system wide, modify your shell or PAM configuration, then relogin.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"verbose-output-and-absolute-vs-relative-paths\">Verbose Output and Absolute vs. Relative Paths<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"use-v-to-confirm-whats-created\">Use -v to Confirm What\u2019s Created<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -vp \/opt\/tools\/bin\n# Output: mkdir: created directory '\/opt\/tools'\n#         mkdir: created directory '\/opt\/tools\/bin'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"absolute-vs-relative-paths\">Absolute vs. Relative Paths<\/h3>\n\n\n\n<p>Absolute paths start from \/ and are unambiguous (\/data\/backups). Relative paths depend on your current directory (.\/backups or just backups). For scripts and automation, prefer absolute paths to reduce errors.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"handling-errors-file-exists-permission-denied-and-more\">Handling Errors: File Exists, Permission Denied, and More<\/h2>\n\n\n\n<p>mkdir provides helpful errors. Here are common ones and quick fixes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>File exists: <\/strong>The directory already exists. Use -p to avoid errors when re-running scripts.<\/li>\n\n\n\n<li><strong>Permission denied: <\/strong>You lack rights to the parent path. Switch to a writable directory, adjust ownership\/permissions, or use sudo if appropriate.<\/li>\n\n\n\n<li><strong>No such file or directory:<\/strong> The parent path doesn\u2019t exist. Use -p to create it.<\/li>\n\n\n\n<li><strong>No space left on device:<\/strong> The filesystem is full. Free space or extend storage.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Robust pattern for idempotent script runs\nmkdir -p \/srv\/app\/logs || { echo \"Failed to create logs\"; exit 1; }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"real-world-use-cases-hosting-and-devops\">Real World Use Cases (Hosting and DevOps)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Web roots:<\/strong> mkdir -p \/var\/www\/example.com\/{public_html,logs,tmp}<\/li>\n\n\n\n<li><strong>Deployment pipelines:<\/strong> Prepare release folders, artifacts, and cache directories safely with -p.<\/li>\n\n\n\n<li><strong>Backups:<\/strong> mkdir -p \/backups\/$(date +%F) to snapshot daily backup directories.<\/li>\n\n\n\n<li><strong>Multi tenant hosting:<\/strong> Create per-site directories with correct permissions and ownership.<\/li>\n<\/ul>\n\n\n\n<p>If you\u2019re deploying on <strong><a href=\"https:\/\/www.youstable.com\/vps-hosting\/\">YouStable VPS<\/a><\/strong> or Cloud hosting, you can SSH into your server and use mkdir to prepare docroots, logs, and isolation-friendly paths. Our support can guide you on secure permissions for NGINX\/Apache and PHP-FPM pools.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"advanced-patterns-and-productivity-tips\">Advanced Patterns and Productivity Tips<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"create-many-directories-with-brace-expansion\">Create Many Directories with Brace Expansion<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Monthly data directories\nmkdir -p data\/{2024,2025}\/{01..12}\n\n# App structure scaffold\nmkdir -p app\/{bin,config,lib,log,tmp}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"from-a-file-or-command-stream\">From a File or Command Stream<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Create directories listed in a file\nxargs -I {} mkdir -p \"{}\" &lt;&lt;EOF\n\/var\/www\/site1\/logs\n\/var\/www\/site2\/logs\n\/var\/www\/site3\/logs\nEOF\n\n# With find to mirror a structure\nfind templates -type d -printf '%P\\n' | xargs -I {} mkdir -p \"output\/{}\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"selinux-contexts-selinux-enabled-systems\">SELinux Contexts (SELinux Enabled Systems)<\/h2>\n\n\n\n<p>On SELinux systems, you can apply contexts at creation time in some setups:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Set default context for created dirs (policy-dependent)\nmkdir -Z -p \/var\/www\/example.com\/public_html\n\n# Or specify an explicit context if supported\nmkdir --context=system_u:object_r:httpd_sys_content_t:s0 \/var\/www\/example.com\/public_html<\/code><\/pre>\n\n\n\n<p>Always verify with ls -Z and consult your security policy. In many cases, semanage fcontext and restorecon are preferred for persistent labeling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"security-and-permission-best-practices\">Security and Permission Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Avoid 777: <\/strong>World writable directories invite risk. Prefer 750\/755 for shared reads and 700 for private data.<\/li>\n\n\n\n<li><strong>Use groups: <\/strong>Create a group for a project and set directory group ownership for collaborative write access.<\/li>\n\n\n\n<li><strong>Sticky bit for shared temp:<\/strong> For shared writable dirs (like \/tmp), sticky bit prevents file deletion by others.<\/li>\n\n\n\n<li><strong>Ownership matters:<\/strong> After mkdir, assign correct owner and group with chown to the service or deployment user.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: secure web root with group collaboration\nsudo mkdir -p \/var\/www\/project\/{public,logs}\nsudo chown -R deploy:www-data \/var\/www\/project\nsudo chmod -R 750 \/var\/www\/project\nsudo chmod 2750 \/var\/www\/project # setgid to inherit group<\/code><\/pre>\n\n\n\n<p>On <strong><a href=\"https:\/\/www.youstable.com\/dedicated-servers\/\">YouStable servers<\/a><\/strong>, our team can review your directory layout, permissions, and umask to ensure security and consistent deployments across environments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"troubleshooting-checklist\">Troubleshooting Checklist<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Check path:<\/strong> pwd and ls -ld parent directories to confirm correct location.<\/li>\n\n\n\n<li><strong>Verify permissions:<\/strong> ls -ld parent and identify whether you need sudo or chown.<\/li>\n\n\n\n<li><strong>Confirm umask: <\/strong>umask may be stripping intended permissions; use -m for exact mode.<\/li>\n\n\n\n<li><strong>Re-run safely:<\/strong> Use mkdir -p to prevent errors on existing paths in scripts.<\/li>\n\n\n\n<li><strong>Audit SELinux:<\/strong> On policy-enforced systems, check contexts with ls -Z.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"command-recap-quick-examples\">Command Recap: Quick Examples<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Basic\nmkdir test\n\n# Multiple\nmkdir dir1 dir2 dir3\n\n# Nested parents\nmkdir -p \/data\/app\/releases\/current\n\n# Verbose create\nmkdir -vp \/opt\/tools\/bin\n\n# With permissions\nmkdir -m 755 public\nmkdir -m 700 private\n\n# With brace expansion\nmkdir -p \/var\/www\/site\/{public_html,logs,tmp}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"faqs\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1768200100773\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"what-does-the-mkdir-command-do-in-linux\">What does the mkdir command do in Linux?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>mkdir creates directories. You can create one or multiple at a time, build nested directories with -p, set permissions with -m, and show progress with -v. It respects your system\u2019s umask unless you explicitly set a mode.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768200109219\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-create-nested-directories-in-one-command\">How do I create nested directories in one command?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use mkdir -p. For example: mkdir -p \/var\/www\/project\/public_html creates all missing parent directories without errors if they already exist.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768200117835\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-set-directory-permissions-during-creation\">How do I set directory permissions during creation?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use -m. For numeric: mkdir -m 755 docs. For symbolic: mkdir -m u=rwx,go=rx docs. This overrides the current umask for that command.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768200125595\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-can-i-create-multiple-directories-at-once\">How can I create multiple directories at once?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>List them:<\/strong> mkdir img css js. For patterns, use brace expansion: mkdir -p env\/{dev,stage,prod}\/logs to create nine paths in a single command.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1768200133209\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"why-does-mkdir-say-permission-denied\">Why does mkdir say \u201cPermission denied\u201d?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You don\u2019t have rights to the parent directory. Create paths under your home, change ownership or permissions, or use sudo when appropriate (e.g., system paths like \/var or \/opt).<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p><strong>The mkdir command in Linux is<\/strong> simple yet powerful. With -p, -m, and shell expansions, you can build secure, repeatable directory structures for apps and websites. If you host with <strong><a href=\"https:\/\/www.youstable.com\/\">YouStable<\/a><\/strong>, our engineers can help you standardize directory layouts and permissions across your environments for smoother deployments and better security.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The mkdir command in Linux creates new directories. Its basic form is mkdir DIRECTORY, and you can create multiple folders [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":18605,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[350],"tags":[],"class_list":["post-17376","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledgebase"],"acf":[],"featured_image_src":"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2026\/01\/Mkdir-Command-in-Linux.jpg","author_info":{"display_name":"Sanjeet Chauhan","author_link":"https:\/\/www.youstable.com\/blog\/author\/sanjeet"},"_links":{"self":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/comments?post=17376"}],"version-history":[{"count":6,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17376\/revisions"}],"predecessor-version":[{"id":18607,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/17376\/revisions\/18607"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media\/18605"}],"wp:attachment":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media?parent=17376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/categories?post=17376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/tags?post=17376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}