Fix 'No Such File or Directory' for Filenames with Spaces
In this article
If you've ever typed a command like cat My File.txt and gotten No such file or directory, the problem is almost always that the shell treats spaces as separators between arguments. This guide shows you how to properly reference filenames with spaces in Linux, macOS, and Windows terminals so your commands work the first time.
What You'll Need
- A terminal or command prompt (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows)
- A file whose name contains at least one space
- Basic familiarity with typing commands
Step-by-Step
- Identify the exact filename. Use
ls(Linux/macOS) ordir(Windows) to see the file's precise name. Note the spaces and any other special characters. - Choose your quoting method. The most reliable way is to wrap the entire filename in quotes. For example, if the file is
My Report.txt, type:
This works in bash, zsh, PowerShell, and Command Prompt.cat "My Report.txt" - Use the backslash escape (Linux/macOS). In bash or zsh, you can escape each space with a backslash. The same command becomes:
The backslash tells the shell that the following space is part of the filename, not a separator.cat My\ Report.txt - Use tab completion. Type the first few characters of the filename, then press Tab. The shell will auto-complete the name with proper escaping or quoting. For example, type
cat Myand press Tab—it becomescat My\ Report.txt(or with quotes, depending on your shell). - For Windows Command Prompt, use double quotes. The same rule applies:
type "My Report.txt"orcopy "My Report.txt" "My Report Copy.txt". Single quotes do not work in Command Prompt, so always use double quotes. - For PowerShell, use double quotes or the backtick. Double quotes work as expected:
Get-Content "My Report.txt". If you prefer not to use quotes, you can escape spaces with a backtick (`), but double quotes are simpler and less error-prone. - If you're writing a script, quote variables too. When a filename is stored in a variable, always quote it. In bash:
Without the quotes, the variable expands to two separate words, causing the same error.file="My Report.txt" cat "$file"
Common Mistakes
- Using single quotes in Windows Command Prompt. Single quotes are not recognized as quoting characters there; they are treated literally.
- Forgetting to quote the entire path. If the path includes directories with spaces, quote the whole path:
cat "/home/user/My Documents/Report.txt", not just the filename. - Putting quotes around only part of the name. For example,
cat "My" Report.txtwill still fail becauseReport.txtis treated as a separate argument. - Using a forward slash in Windows paths. Windows accepts both
\and/, but when quoting, keep the backslashes as they are. In PowerShell, backslashes are not escape characters, so"My Report.txt"is fine.
Tips
- Rename the file if you can. If the filename is under your control, replacing spaces with underscores or hyphens eliminates the problem entirely. Use
mv "My Report.txt" My_Report.txt(Linux/macOS) orren "My Report.txt" My_Report.txt(Windows). - Use tab completion consistently. It not only saves typing but also avoids quoting errors because the shell inserts the correct syntax automatically.
- In bash, you can mix quotes and escapes. For example,
cat My\ Report.txtandcat "My Report.txt"are equivalent. Choose whichever is easier to read. - When using find with spaces, quote the search pattern. For example:
find . -name "My Report.txt". The quotes prevent the shell from splitting the pattern.
Troubleshooting
If you still get No such file or directory after quoting, try these steps:
- Check for hidden characters. The filename might contain a non-breaking space or other Unicode space. Use
ls -b(Linux/macOS) to show escaped characters, ordir /x(Windows) to see short names. If you see\240or similar, you have a special space. - Verify the path. Run
pwd(Linux/macOS) orcd(Windows) to confirm you're in the correct directory. If the file is elsewhere, include the full path. - Check for trailing spaces. A filename like
Report.txt(with a space at the end) is valid but easy to miss. Usels | cat -A(Linux/macOS) to see trailing spaces as$at the end of the line. - Try the short name (Windows). In Command Prompt, use
dir /xto find the 8.3 short name, then use that. For example,type REPORT~1.TXT. - If you're using a GUI application, drag and drop. Drag the file into the terminal window—most terminals will insert the filename with proper escaping automatically.
Frequently Asked Questions
Why does the error happen even when I quote the filename?
Quoting should work, so if it doesn't, the filename likely contains a different character than you think—like a non-breaking space or a similar-looking letter. Use ls -b to inspect the exact bytes.
Do single quotes work in Windows Command Prompt?
No. Command Prompt only recognizes double quotes for grouping. Single quotes are treated as literal characters, so type 'My Report.txt' looks for a file named 'My and then Report.txt'.
Can I use spaces in filenames at all?
Yes, spaces are perfectly valid in filenames on all major operating systems. They just require quoting or escaping when used in command-line interfaces.
What if the filename has multiple spaces or spaces at the beginning?
Quote the entire name as-is. For example, cat " My File.txt" works. Tab completion is especially helpful here because it's easy to miscount spaces.
Is there a way to avoid this in scripts?
Yes, always quote variable expansions. In bash: cat "$file". In PowerShell: Get-Content "$file". This handles spaces and other special characters correctly.
Does this affect wildcards like * or ?
Yes. If you use a wildcard that matches a file with spaces, the shell expands it correctly. For example, cat *.txt will include My Report.txt without issues. But if you combine a wildcard with a literal space, you need to quote the literal part: cat "My "*.txt.