How to Fix ‘Zsh: No Such File or Directory’ Error in Terminal

Launching your command-line interface or executing a shell script only to be stopped by the immediate error message: “zsh: no such file or directory” brings command-line productivity to a sudden halt. Whether you are working on Linux or macOS (where Z shell has been the default shell since macOS Catalina), this error is one of the most frequently misunderstood messages in Unix computing. While it intuitively seems to suggest that a file you typed does not exist, the error frequently appears when running commands that do exist, or immediately upon opening a brand-new Terminal tab without typing a single word. Understanding how Zsh resolves command paths, executes initialisation scripts, and parses shebang interpreters allows you to systematically resolve the issue. Follow this guide to troubleshoot and eliminate the error.

Why Does Zsh Report ‘No Such File or Directory’?

The Z shell returns this exact error string under three distinct operational scenarios:

  • Broken Environment PATH Variables: The system $PATH string in your configuration file has been overwritten rather than appended, preventing Zsh from finding standard binaries like ls, mkdir, or node.
  • Syntax Errors in Shell Initialisation Scripts (.zshrc / .zprofile): Attempting to source non-existent files or plugins (such as uninstalled Oh My Zsh themes, NVM directories, or Homebrew paths).
  • Invalid Script Shebang Line (#!): When running a bash or python script where the interpreter path at the very top (e.g. #!/bin/bash^M) contains Windows carriage returns (CRLF) or points to a non-existent binary location.
  • Unquoted File Paths with Spaces: Executing commands on paths containing whitespace or special characters without enclosing them in quotation marks or escaping spaces with backslashes.

Method 1: Inspect and Repair Shell Initialisation Files (.zshrc)

If the error greets you the instant you launch your Terminal, a line inside your ~/.zshrc or ~/.zprofile file is attempting to execute or source a missing file.

  1. Open your .zshrc file using the nano terminal editor:
    nano ~/.zshrc
  2. Look through the file for lines starting with source or . (e.g. source ~/.nvm/nvm.sh or source ~/custom-theme.zsh).
  3. Check whether those paths actually exist on your storage drive. If an uninstalled plugin or old configuration is referenced, delete that line or comment it out by placing a # at the start of the line.
  4. Press Ctrl + O and Enter to save changes, then press Ctrl + X to exit.
  5. Reload your configuration without closing the terminal:
    source ~/.zshrc

Method 2: Restore the Default System PATH Variable

If standard Unix commands suddenly return “no such file or directory”, your PATH variable was accidentally corrupted by a poorly formatted export PATH=... statement.

  1. Check your current PATH string:
    echo $PATH
  2. If the output does not contain /usr/bin, /bin, and /usr/sbin, standard tools are inaccessible. Temporarily restore standard paths in your active session by running:
    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
  3. Now open your ~/.zshrc file:
    nano ~/.zshrc
  4. Locate any lines setting the PATH. Ensure you are appending to the existing path using :$PATH at the end. For example, use:
    export PATH="/opt/homebrew/bin:$PATH"
    rather than omitting :$PATH. Save and exit.

Method 3: Fix Script Line Endings (CRLF to LF)

Scripts created or edited on Windows computers use DOS/Windows carriage return line endings ( ), which break Unix shell interpreters with misleading “no such file” errors.

  1. If the error occurs when executing a script file (e.g. ./deploy.sh), check its line endings.
  2. Convert Windows CRLF line endings to Unix LF using the dos2unix utility:
    dos2unix script.sh
  3. Alternatively, if dos2unix is not installed, use sed:
    sed -i 's/\r$//' script.sh
  4. Make the script executable and run it again:
    chmod +x script.sh && ./script.sh

Method 4: Properly Escape Whitespace in File and Folder Names

Unix shells treat spaces as argument delimiters. Unquoted spaces cause the shell to truncate the path.

  1. If navigating to or opening a directory containing spaces, always wrap the path in double quotation marks:
    cd "My Project Folder"
  2. Alternatively, use a backslash to escape each space character:
    cd My\ Project\ Folder

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.