How to fix “command not found: ls” in macOS

One of the most commonly used commands in daily Linux/UNIX operations is ls, which stands for list. As its name implies, it is used to list the contents of a directory and is one of the few commands that beginners should learn how to use right after they start with Linux/UNIX systems.

macOS also have ls command built into the OS. Normally, it works out of the box without any issues. But there are times you may encounter a strange error that says ls: command not found which looks something like below.

command not found ls in macOS

In this short article, we will show you a few possible steps to debug and fix “command not found: ls” error in macOS.

What does command not found: ls mean?

Most operating systems search for executables and binaries in a specific location. The list of all those locations is saved in an environment variable known as PATH.

In macOS, executable binaries are placed in /usr/local/bin by default. This path is also included in PATH environment variable.

If something messed up PATH, the system cannot locate and execute ls.

In most cases, “command not found: ls” error in macOS typically indicates that the PATH environment variable is modified or overwritten, which no longer includes /usr/local/bin path.

Inspect PATH environment variable

In order to check what the current PATH value is, open up a terminal window and run echo $PATH. The default output should look like this.

Check your PATH environment variable on macos

If you don’t see /usr/local/bin present in the output, you would have to put it back in. Run the following command to temporarily set PATH to its default value.

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Code language: JavaScript (javascript)

Now try to run ls again to see whether the “command not found: ls” error goes away.

To make the fix permanent, follow the steps below.

  • Edit .zshrc file using nano: sudo nano ~/.zshrc
  • Put export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin to the very bottom of the file.
  • Press Control + X, followed by Y to save the file, press enter to exit Nano,
  • Now run source ~/.zshrc command to apply the changes to the current shell.

Check shell startup file

One of the common reasons for command not found: ls is  /etc/profile cannot be loaded upon shell startup.

/etc/profile is the global startup file responsible for initializing PATH environment variable.

You can check whether /etc/profile exists, readable and executable at the same time by running the following command.

. /etc/profile

If the file does exists and is executable (and includes some kind of code), it may not be loaded properly, or something may be overriding it.

As a workaround, you can try putting . /etc/profile at the very beginning of your shell startup. In ZSH cases, you should edit /etc/zprofile and /etc/zshrc instead.

Alternatively, you can try recreating /etc/profile with the following default contents :

# System-wide .profile for sh(1) if [ -x /usr/libexec/path_helper ]; then eval `/usr/libexec/path_helper -s` fi if [ "${BASH-no}" != "no" ]; then [ -r /etc/bashrc ] && . /etc/bashrc fi
Code language: PHP (php)

We hope that the information above is useful and helped you successfully fix the “command not found: ls” error.

We’ve also written a few other guides to solve common “command not found” error, both in macOS and Linux, such as brew: command not found – Quick Fixes for Mac M1, fixing zsh: command not found and make: command not found.

If you have any questions, then please feel free to ask in the comments below.

Leave a Comment