What mkdir does
Creates one or more directories. By default it creates them in the current path unless you give a full/relative path.
mkdir lab # make a folder named 'lab'mkdir dir1 dir2 dir3 # make several at oncemkdir "My Projects" # names with spaces → quote them1) The flags you’ll actually use
-p
— parents (most important)
· Creates parent directories as needed.
· No error if the dir already exists (idempotent → great for scripts).
mkdir -p projects/sem1/os/notesmkdir -p /data/backups # won’t fail if /data/backups already exists-v
— verbose
Shows what it created.
mkdir -vp project/{src,bin,docs}-m
— mode (permissions on creation)
Sets permissions at creation time
(before umask applies).
mkdir -m 755 public_html # rwx r-x r-xmkdir -m u=rwx,go=rx labs # same as 755 using symbolsNote: default
perms come from your umask (see it with umask). -m lets you override.
2) Power patterns (you’ll use these a lot)
Create a whole tree quickly (brace expansion)
mkdir -p project/{src,tests,docs,build}mkdir -p uni/{sem1/{os,cn,dbms},sem2/{daa,oslab}}Ensure a directory exists before writing files (script-safe)
target="$HOME/data/runs/$(date +%F)"mkdir -p "$target"cp results.csv "$target/"Make parent folder of a file path
f="$HOME/out/reports/summary.txt"mkdir -p "$(dirname "$f")"Shared team folder where new files keep the group
mkdir -p /shared/devchgrp devs /shared/devchmod 2775 /shared/dev # setgid bit → new files inherit group 'devs'3) Checking results & permissions
ls -ld lab # show the directory entryls -l # directories show with 'd' at the start: drwxr-xr-x4) Common errors (and quick fixes)
·
File
exists → the directory is
already there. Use -p to make it a no-op.
·
No
such file or directory →
parent path missing. Use -p.
·
Permission
denied → you don’t have rights
in the parent. Choose a path you own or use sudo
(only when appropriate).
5) Mini-lab (10–15 min)
# 1) simple & multiplemkdir -v ~/lab && mkdir -v ~/lab/{alpha,beta} # 2) nested with parentsmkdir -vp ~/lab/alpha/src/utils # 3) custom permissionsmkdir -m 700 ~/lab/secret # only you can accessls -ld ~/lab/secret # 4) project skeleton via bracesmkdir -vp ~/lab/app/{src,tests,docs,build}ls -l ~/lab/app # 5) script-safe parent creationfile=~/lab/output/run1/results.txtmkdir -p "$(dirname "$file")" && echo "OK" > "$file" && ls -l "$file"Exam-ready bullets
· Purpose: create directories.
·
Syntax: mkdir
[-p] [-m MODE] [-v] DIR...
·
-p creates parents and succeeds if existing
(idempotent).
·
-m sets permissions at creation (e.g., -m 755).
·
-v prints what’s created.
·
Use brace expansion
to create many dirs at once: mkdir -p proj/{src,docs,tests}.
·
Check with ls -ld DIR; remember permissions are affected by umask.
Want this bundled
with ls, cd, cp, mv, rm as a printable 1-pager for your class? I can format
it.