How To Create Directory In Terminal

7 min read

How to Create a Directory in Terminal

Navigating and organizing files is a fundamental skill for anyone working with computers, especially in development, system administration, or data science. The terminal provides a powerful, efficient way to manage your filesystem without relying on graphical interfaces. On the flip side, among the most basic yet essential commands is the one used to create a new folder, or directory, in your system. Understanding how to create directory in terminal not only saves time but also deepens your comprehension of how operating systems structure data. This article walks you through the process across different platforms, explains key concepts, and offers tips to avoid common pitfalls.

Understanding the Terminal and Directory Concepts

Before typing commands, it helps to understand what the terminal actually is and what a directory represents. The terminal, also known as the command line or shell, is a text-based interface that allows you to interact with your operating system by typing commands. Instead of clicking through menus, you type instructions that the system executes immediately Not complicated — just consistent..

A directory is simply a folder that organizes files and other directories. In a filesystem, directories form a hierarchical tree structure. The root directory sits at the top, and every other folder branches out from it. When you create a new directory, you're adding a new node to this tree, which helps keep your files structured and easily retrievable.

Different shells—such as Bash, Zsh, or Fish—may have slight variations in how they handle commands, but the core functionality remains consistent across most Unix-like systems and even Windows Terminal with WSL or Git Bash. Knowing your shell can help you customize your experience, but the basic command for creating a directory is universal.

Real talk — this step gets skipped all the time Not complicated — just consistent..

Creating a Directory on Linux and macOS

On most Linux distributions and macOS, the default shell is Bash or Zsh, and the command to create a new directory is mkdir, short for "make directory". The basic syntax is straightforward:

mkdir new_folder

Running this command in your terminal will create a folder named new_folder in your current working directory. If you want to create a directory with spaces in the name, you can either quote the name or use backslashes:

mkdir "my project"
# or
mkdir my\ project

Creating Nested Directories

One of the most powerful features of mkdir is the ability to create nested directories in a single command. By using the -p (parents) flag, you can specify a full path, and the system will create all intermediate directories that don't already exist. This is incredibly useful when setting up project structures:

The official docs gloss over this. That's a mistake.

mkdir -p project/src/components/utils

This command creates project, then project/src, then project/src/components, and finally project/src/components/utils, regardless of whether any of those folders already exist. Without the -p flag, attempting to create a nested path would result in an error if any parent directory was missing.

Using Absolute and Relative Paths

Understanding paths is crucial when creating directories. A relative path is based on your current location in the filesystem. Take this: if you're in your home directory and type mkdir documents/work, you're creating a work folder inside a documents folder relative to where you are.

An absolute path starts from the root of the filesystem and always begins with a forward slash (/). That said, on macOS and Linux, an absolute path might look like /Users/username/projects/new_folder. Using absolute paths eliminates ambiguity, especially when scripting or working across different directories.

Creating a Directory on Windows

Windows users have a few options depending on the terminal they use. The traditional Command Prompt uses md or mkdir as well, but the behavior and path syntax differ slightly from Unix systems.

mkdir C:\Users\YourName\Documents\NewProject

If you're using Windows Terminal with WSL (Windows Subsystem for Linux) or Git Bash, the mkdir command works exactly as it does on Linux or macOS. This compatibility makes it easier for developers who switch between Windows and Unix environments daily Practical, not theoretical..

For those using PowerShell, the command is slightly different:

New-Item -ItemType Directory -Path "C:\Users\YourName\Documents\NewProject"

While mkdir is often accepted as an alias, using New-Item ensures you're using the native PowerShell cmdlet and can take advantage of additional parameters like -Force to overwrite existing directories or -Path for flexible path handling It's one of those things that adds up..

Absolute vs

Absolute vs Relative Paths

When you specify a path to mkdir, you have two fundamental options: absolute and relative. Understanding the distinction helps you avoid common pitfalls, especially when writing scripts that need to run from different working directories And that's really what it comes down to..

Relative Paths

A relative path is interpreted based on your current working directory (CWD). If you run:

mkdir logs/2025

and your CWD is /home/user/project, the command creates /home/user/project/logs/2025. and run the same command, the new folder appears in a completely different location. If you later change directories withcd ..This flexibility is useful for building structures that are anchored to a project root, but it also means that scripts must be executed from the intended context Still holds up..

Tip: Many developers place a mkdir -p command inside a script that first runs cd "$(dirname "$0")" to guarantee the script operates relative to its own location.

Absolute Paths

An absolute path starts at the filesystem root and uniquely identifies a location regardless of where you are. For example:

mkdir /var/www/html/public

Even if you cd /tmp first, the folder will appear at /var/www/html/public. Absolute paths are unambiguous, making them ideal for system‑wide directories, configuration files, or when you need to guarantee that a folder ends up exactly where you expect it.

Choosing the Right Approach

Scenario Recommended Path Type Reason
Project‑local structure (e.Which means g. , src/, tests/) Relative Keeps the layout portable; you can run the same command from any directory inside the repo. Still,
System directories (e. g., /usr/local/bin, /etc/app) Absolute Prevents accidental creation in the wrong location and aligns with OS conventions. On top of that,
Cross‑platform scripts (run on macOS, Linux, Windows) Absolute (or use pwd to compute it) Eliminates dependency on the caller’s CWD, ensuring consistent behavior.
User‑specific data (e.g., ~/Documents/Notes) Tilde‑expanded relative (~/Documents/Notes) Works on Unix‑like systems and is easy to read; expands to the user’s home directory.

Handling Spaces and Special Characters

Both absolute and relative paths can contain spaces, brackets, or other special characters. The safest way to pass them to mkdir is by quoting the entire argument:

mkdir "my documents/2025 reports"

On Windows Command Prompt, quoting works similarly:

mkdir "C:\Program Files\My App\Data"

PowerShell also supports quoting, but it additionally allows you to use single quotes for literal strings:

New-Item -ItemType Directory -Path 'C:\Users\Public\Test Folder'

Practical Example: Creating a Cross‑Platform Project Layout

Below is a small Bash snippet that builds a typical Node.js project structure, using relative paths so it can be run from any directory inside the repo:

#!/usr/bin/env bash
set -euo pipefail

# Define the project root (the directory containing this script)
PROJECT_ROOT="$(cd "$(dirname "$0")" && pwd)"

# Create the layout
mkdir -p "$PROJECT_ROOT/src"
mkdir -p "$PROJECT_ROOT/tests"
mkdir -p "$PROJECT_ROOT/docs"
mkdir -p "$PROJECT_ROOT/assets/images"
mkdir -p "$PROJECT_ROOT/assets/fonts"

If you prefer a Windows PowerShell version, you can achieve the same result with:

$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
@('src', 'tests', 'docs', 'assets\images', 'assets\fonts') |
    ForEach-Object { New-Item -ItemType Directory -Path (Join-Path $ProjectRoot $_) -Force }

Both scripts rely on absolute paths internally ($ProjectRoot) while presenting a clean, relative description of the directories you want.

Conclusion

Creating directories is a foundational task that underpins everything from simple file organization to complex CI/CD pipelines. By mastering the nuances of mkdir on Unix‑like systems and its Windows equivalents, and by understanding when to use absolute versus relative paths, you can write solid, portable scripts that work consistently across platforms. Whether you’re building a nested project hierarchy with a single -p flag or handling paths that contain spaces, the key is to

Out This Week

Out This Week

Dig Deeper Here

Readers Also Enjoyed

Thank you for reading about How To Create Directory In Terminal. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home