How to cd into a folder with spaces

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To navigate into a folder with spaces using the cd command, you must either quote the path ('cd "folder name"') or escape the spaces ('cd folder\ name'). The quotation method is most common: cd "My Documents" will successfully change to that directory without errors.

Key Facts

What It Is

The cd (change directory) command is used to navigate between folders in a command-line terminal or shell environment. When a folder name contains spaces, the shell interprets each word as a separate argument unless you properly format the path. This is a fundamental concept in command-line computing that affects not just cd, but any command that interacts with file paths. Understanding how to handle spaces is essential for anyone using terminal interfaces on Linux, macOS, or Windows (PowerShell/Command Prompt).

The space-handling problem emerged with early Unix systems in the 1970s, where spaces were chosen as default argument delimiters for efficiency. When personal computers began using graphical interfaces in the 1980s-1990s, operating systems like Windows and macOS allowed spaces in file and folder names for user readability. This created a persistent incompatibility between user-friendly folder naming and command-line functionality. The solution—quoting and escaping—has remained the standard since the original Unix shell design.

There are three primary methods to handle spaces: quoting the entire path, escaping individual spaces, and using tab completion. Double-quote method: cd "Folder Name". Single-quote method: cd 'Folder Name'. Backslash escape method: cd Folder\ Name. Each method has slightly different behavior with special characters and variables. The quoting methods are most reliable and widely used across different operating systems and shell environments.

How It Works

The shell interprets the command line by splitting it on whitespace characters (spaces, tabs, newlines) unless those characters are protected by quoting or escaping. When you type 'cd My Documents', the shell sees three separate tokens: 'cd', 'My', and 'Documents'. It then passes 'My' as the argument to cd, which fails because no folder named 'My' exists. Quoting or escaping tells the shell to treat the space as a literal character rather than a separator, so the entire string is passed as one argument.

For a real example, consider navigating to a Windows user's 'My Documents' folder on a MacBook: typing 'cd My Documents' fails with 'cd: My: No such file or directory', but 'cd "My Documents"' succeeds. On Linux systems managing a project folder called 'My Project Files', you would use 'cd "My Project Files"' from any parent directory. Within PowerShell on Windows, 'cd "C:\Users\John Smith\Documents"' handles both the backslashes and spaces correctly. AWS cloud engineers working with serverless folders use quotes when navigating: 'cd "Lambda Functions"'.

The step-by-step process is: (1) type cd followed by a space, (2) add an opening quote (single or double), (3) type or paste the folder path with spaces, (4) add the closing quote, (5) press Enter. Alternatively, after typing 'cd "M', you can press Tab to auto-complete the remaining path, which automatically handles spaces. In most modern terminals, pasting a path automatically quotes it if needed. For nested folders with multiple spaces, quote the entire path once: 'cd "Parent Folder/Child Folder"'.

Why It Matters

This matters because terminal usage is essential in software development, system administration, data science, and DevOps—fields employing approximately 25 million professionals globally. Being unable to navigate directories properly blocks all further command-line operations, making this a fundamental blocker for anyone learning Unix/Linux systems. According to Stack Overflow's 2024 survey, over 20% of programming questions involve basic file system navigation issues, with space handling being the most common. Network administrators managing cloud infrastructure (AWS, Azure, GCP) encounter server paths with spaces regularly.

In DevOps and cloud computing, scripts and automated deployments frequently navigate directories with spaces in production environments. Companies like Netflix, Spotify, and Microsoft all use command-line tools with folder names containing spaces in their build pipelines. Incorrect handling of spaces in scripts causes silent failures or sends commands to wrong directories. Security vulnerabilities can arise from improper space escaping, where attackers craft folder names with spaces to inject malicious commands (path traversal attacks).

Future trends show increased complexity as artificial intelligence training datasets and machine learning projects use increasingly human-readable folder structures with multiple spaces and special characters. Container orchestration tools like Kubernetes and Docker increasingly encounter volume mount paths with spaces. The shift toward Infrastructure-as-Code means developers must confidently script directory navigation in multiple environments. As remote work increases, users managing local folders with client names or project descriptions containing spaces will need this knowledge.

Common Misconceptions

Misconception 1: 'I should avoid using spaces in folder names to solve this problem.' While technically true, this is impractical advice that ignores user preferences and operating system conventions. Windows and macOS actively encourage spaces in folder names ('My Documents', 'My Pictures') through their default folder names. Telling users to never use spaces in folder names is like telling them to accept poor usability—the real solution is learning the command-line syntax. Modern software should be designed to handle spaces, not force users into awkward naming conventions.

Misconception 2: 'Escaping spaces with backslashes is the best method.' While backslash escaping works, it's actually more error-prone than quoting because each space requires an individual backslash. 'cd My\ Documents\ Folder' is harder to read and easier to get wrong than 'cd "My Documents Folder"'. Single quotes are better for avoiding variable expansion, but double quotes are more intuitive for most users. The misconception comes from seeing backslash examples in tutorials without context about why quoting is often superior.

Misconception 3: 'Different operating systems require different quoting syntax.' This is partially false—while Windows PowerShell and Linux bash handle some characters differently, both support the standard double-quote method for spaces. 'cd "Folder Name"' works on Windows PowerShell, Linux bash, and macOS zsh. Some shells offer additional quoting methods, but the double-quote syntax is universal across all major platforms. This misconception discourages cross-platform development because developers think their skills don't transfer between systems.

Related Questions

Related Questions

What's the difference between single and double quotes?

Single quotes treat everything literally, preventing variable expansion and special character interpretation. Double quotes allow variable expansion (e.g., $HOME) but still protect spaces from being treated as separators. For simple folder names with spaces, both work identically, but double quotes are more versatile for advanced use cases.

Can I use wildcards with folder names containing spaces?

Yes, but the wildcard must be inside quotes: cd "My Do*" might match 'My Documents' if only one folder starts that way. Wildcards like * and ? work normally within quoted paths. However, be careful because 'cd My Do*' (unquoted) interprets the * as a separator and fails.

Why does tab completion work better than typing the path manually?

Tab completion automatically quotes or escapes spaces correctly without requiring you to type them yourself. The shell's autocomplete feature understands your filesystem and formats paths properly. This is why experienced developers rely on tab completion to navigate—it eliminates manual quoting errors.

Sources

  1. Command-line Interface - WikipediaCC-BY-SA-4.0
  2. Bash Unix Shell - WikipediaCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.