How to fix "ls: command not found" error in Linux

Oops!
Linux command "ls" doesn't work!
What can I do about this ?

When we use linux server like XSERVER through SSH connection, we usually use these commands like ls or vi.

But one day your server would refuse your command, and you may see the error below.

bash: ls: command not found

What is root cause of this error.

How to find root cause without commands like ls.

So today I will introduce about how to fix "ls: command not found" error in Linux.

TOC

The reason of "ls: command not found" error

Reason of "ls: command not found" error is that PATH settings is broken.

Because of broken PATH settings, server can not find program of input command. So server said "command not found".

Then can I run command with full path ?

Yes. If server has specific program, you can run the command.

So you can run command with full path.

Full path is different with its environment.

For example, you can run ls command with /usr/bin/ls in XSERVER.

How to fix "ls: command not found" error in Linux

idea, solution

The cause of "ls: command not found" error is broken path settings.

Then how can we fix the error ?

In case of bash, path settings is in ~/.bash_profile .

So you should fix the content.

In order to open file, you can use following command.

/usr/bin/vi ~/.bash_profile

In my case, content of ~/.bash_profile was like below.

Wrong point is below.

PATH=$PATH:$HOME/bin
export PATHeval $(/home/xxxx/.linuxbrew/bin/brew shellenv)

This text should have line break between export PATH and eval.
(Maybe I did mistake when I installed linuxbrew.)

So in my case, I modified ~/.bash_profile like below.

PATH=$PATH:$HOME/bin
export PATH
eval $(/home/xxxx/.linuxbrew/bin/brew shellenv)

Then "command not found" error was solved.

Linux – "command not found" teratail

Conclusion

Today I explained about how to fix "ls: command not found" error in Linux.

Important points are following.

  • The cause of "ls: command not found" error is broken path settings.
  • In this case, you can use full path like /usr/bin/ls .
  • In order to solve this error, you should open  ~/.bash_profile with /usr/bin/vi command.

Even if you face "command not found" error, you can solve it.

If you like this article, please
Like or Follow !

If you like this article, please share !

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

TOC