How to Fix “Command Not Found” Errors in Mac Command Line
Advanced Mac users that utilize the command line may occasionally encounter a “command not found” error message when attempting to run something in the command line. The “command not found” error in the Terminal can surface for several reasons in the command line of MacOS and Mac OS X, as we’ll discuss here, and of course we’ll offer solutions to these issues.
Why you see “command not found” error messages at the command line
The four most common reasons why you may see the “command not found” message in the Mac command line are as follows:
- the command syntax was entered incorrectly
- the command you are attempting to run is not installed
- the command was deleted, or, worse, the system directory was deleted or modified
- the users $PATH is incomplete, or $PATH has been erroneously set, reset, or cleared – this is the most common reason to see a ‘command not found’ message
Fortunately you can solve all of these issues and get the common working again as expected. If you simply entered the syntax wrong, entering it correctly resolves that, easy! Beyond that, we’ll start off with the most common reason, which is that the users $PATH is not set properly, or was reset somehow.
Fixing “Command Not Found” Terminal Messages in Mac OS with $PATH Setting
The most likely reason why Mac users may unexpectedly see the command not found message in the command line is something went awry with the users $PATH, or the path where the command is located is not set. You can check the $PATH with “echo $PATH” if you feel like it, otherwise you can just run the following commands to set the standard default path that Mac OS uses in the command line:
Hit return and run your command again, it should work fine.
By the way, even though we’re focusing on Mac OS here, this same idea applies to other unix and linux varieties as well.
Note if the intended command you’re attempting to use is located in a nonstandard directory or in another location (/usr/local/sbin/ etc), you can always add that new $PATH at the command line to specify where to look if needed.
Before, where the “command not found” message shows up running simple commands line ls and cd:
After, with those commands working successfully as expected:
How does this happen? Sometimes it can be running an incomplete or incorrect export $PATH command, a failure of adjusting environment variables, amongst other reasons.
You may need to refresh the command line shell for the change to take effect. If you relaunch the Terminal and get the “command not found” error again, then add the export $PATH commands to the users .bash_profile, .profile, or relevant shell profile if using an alternative shell in the Terminal app.
“Command not found” because command isn’t installed? Use HomeBrew
If the command just isn’t installed on the Mac, for common examples like wget, htop, or the many other useful unix commands available as Homebrew packages that are not otherwise preinstalled in Mac OS, then the simplest solution is to install and use Homebrew on the Mac to gain access to those command line utilities. Homebrew is a great tool anyway, so if you’re going to be spending time in the Terminal you’ll probably want it.
“Command not found” because a system directory is missing? Restore the missing system files
Every once in a while, Mac users may find themselves in a situation where they accidentally or inadvertently deleted system files from Mac OS. Usually this happens when someone is experimenting with the rm/srm commands and a wildcard, or maybe they became overly zealous with the Trash can when logged in as root. In any event, you can read here how to restore deleted or missing system files to Mac OS and Mac OS X – it usually involves restoring from a backup or reinstalling the system software itself.
Do you know of another reason why you may see the “command not found” error message in Mac OS Terminal? Maybe you have a better solution than what is offered above? Share with us in the comments below!
Thanks a lot it worked!!!
I’ve done the export PATH command and then check echo PATH$ and it is as you suggested but I still get “-sh: rm: Command not found”
Thank you! These instructions (below) helped point to where my path was and how to fix it!
You can check the $PATH with “echo $PATH” if you feel like it, otherwise you can just run the following commands to set the standard default path that Mac OS uses in the command line:
Hit return and run your command again, it should work fine.
Last login: Mon Feb 28 23:17:06 on ttys000
/etc/zshrc:7: command not found: locale
/etc/zshrc_Apple_Terminal:104: command not found: mkdir
sajeethsamar@sajeeths-MacBook-Air ~ %
how to solve this problem
Thanks A lot..!!!
The most useful piece of information Ive come across. My vim is back!!!
I have this issue on literally every command with a newly bought M1… Terrible dev. system, big waste of money and Apple support is not able to help properly…
Be sure to install command line tools
Reinstalling Homebrew worked for me. Not sure why this was necessary, but glad to finally have this problem resolved.
yena@Yenas-MacBook-Pro ~ % echo $PATH
my terminal is still having a problem of “zsh: command not found:”.. Is my location is wrong? Please help me.
The question to me is why you have a user named ‘osxdaily’ on your Mac
zsh is the default shell on the Mac now
thank you!!! this worked for me!
my dir comman is yet not working
(base) shadabahmed@shadabs-MacBook-Pro desktop % dir
zsh: command not found: dir
(base) shadabahmed@shadabs-MacBook-Pro desktop %
MacOS is Unix based, not DOS like Windows
the ‘dir’ equivalent is ‘ls’
3hrs looking for solution.
yass ITS WORKED! THANKS
Yes its work all commond show notfound but after this it’s work fine.
Thanks the first command worked for me.
Dammmmm! This Works… Thanks a lot!!!!
How can I fix command laptop macbook5
WOW man, i was desperate because i tried to do something really simple, and messed up a lot.
Thanks a lot, my terminal its working again, and really well explained.
The first copy paste i did in my url reset my path and worked perfectly! thank you so much!
Hi,I had also incorrectly configured PATH variable & hence was getting that issue..Thanks a lot :)
Thank you guy’s!!!!!!!!!
this article was a life saver. I was so frustrated about mining guess solution on the internet for hours and with the right command it was gone just like that. You guy’s rock!!!!
I wanted to say thank you for this document because it allowed to reconfigure my terminal and return all orders and since it works very well. Thanks again. See you.
My usr bin looks like a mess. What to do with it?
Finally! This worked great. Thanks
One other ‘command not found’ occurs when trying to use one of your own programs/scripts in a directory that is not in your path:
~/dbapp 513 % db_test
-bash: db_test: command not found
To fix it, prefix the command with ‘./’
~/dbapp 514 % ./db_test
Not tested on Mojave
I would definitely recommend installing Homebrew for adding those third party tools that don’t come with the Mac.
But aside from that I don’t see how someone could mess up their path, maybe I am just not versed enough to get it.