How to Fix “ls command not found” Error?

In Linux, the “ls” command is a handy command that allows users to list the files as well as directories in the working directory, or other directory that users specify. This command can also display various attributes of the files, such as size, permissions, owner, and modification date. But sometimes users may face an error “ls command not found”. 

This guide will demonstrate all possible reasons with solutions to resolve the “ls command not found” error based on the below content:

  1. What is the Basic Usage of the ls Command in Linux?
  2. What are the Causes/Reasons for the “ls command not found” Error?
  3. How to Fix/Resolve the “ls command not found” Error in Linux?

What is the Basic Usage of the ls Command in Linux?

The ls command is used with several options and arguments to customize and filter the results/output. For instance, the “-l” option shows the long format of the file information, the “-a” option shows all files including hidden ones, and the “-h” option shows the file sizes in a human-readable format.

The ls command can also accept a pattern as an argument to match only certain files or directories. For example, ls *.txt will list only files that are complete with the .txt extension format. To explore more about the “ls” command, follow our guide on “ls Command in Linux | List Files and Directories”.

What are the Causes/Reasons for the “ls command not found” Error?

The error “ls command not found” means that the shell cannot find the executable file for the ls command. The reasons behind this error are the PATH environment variable not existing, deleted/renamed/moved ls command, corrupted ls command, changed file permission, and modified shell alias or function. 

All the possible reasons for the “ls command not found” error are mentioned below in detail:

Reason 1: PATH Environment Variable Not Exist

One of the possible reasons for the “ls command not found” error is that the PATH environment variable does not exist or does not include the directory where the ls command is located.

The PATH environment variable is basically a directories list that the shell tracks for an executable file. It is because if the PATH variable is missing or does not contain the correct directories, the shell will not be able to find and execute the command.

Reason 2: Deleted/Renamed/Moved ls Command 

One possible reason for the “ls command not found” error is that the ls command has been deleted, renamed, or moved from its original location. The ls command is found in the “/bin” directory. It gives directions to the shell to search for executable files.

Furthermore, if the ls command is deleted, renamed, or moved from the /bin directory, the shell will not be able to find it and will return an error message. 

Reason 3: Corrupted ls Command 

Another reason for the “ls command not found” error is that the ls command has been corrupted or overwritten by a malicious program. This could happen if the system has been compromised by an attacker who wants to hide the files and directories on the system. 

Reason 4: Changed File Permission

Another reason for causing the error is that the file permission of the ls executable has been changed. The ls command is a built-in utility that lists the files as well as directories in the working directory. The ls executable is usually located in /bin or /usr/bin, and it should have the execute permission for all users. If the execute permission is revoked, then the shell cannot run the ls command and will report an error.

Reason 5: Modified Shell Alias or Function

One possible reason for the “ls command not found” error is that users have modified the shell alias or function for the ls command. An alias or a function is a way to create a shortcut or customize a command in the shell. For example, users can create an alias for “ls -l” as ll, so that users can type ll instead of ls -l. 

However, if users accidentally overwrite the original ls command with an alias or a function that does not exist or has a typo, they will get the error when they try to use ls. 

How to Fix/Resolve the “ls command not found” Error in Linux?

To fix the “ls command not found” error in Linux, users can check the spelling and case letter of the command, check, or add environment PATH variable, check file permission, and reinstall coreutils packages.

These mentioned solutions are implemented in detail as below:

Solution 1: Check the Spelling and Case Letter of the ls Command

One of the most common reasons is Linux commands are case-sensitive and may not work if typed incorrectly. This means that typing LS, Ls, or lS will not work, and will result in the error message below:

To check the spelling and case of the command, make sure that the user types of ls in lowercase letters. Also, users do not have any extra spaces or characters before or after the command. 

Verification

Now, users can verify that the “ls command not found” error has been resolved by typing the “ls” command below:

The output shows the list of files and directories in the current working directory.

Solution 2: Check and Add the PATH Environment Variable

The PATH variable enlists directories where the shell searches for commands. If the directory containing the ls command is not included in the PATH, the shell cannot find it. 

To fix this error, one needs to create or modify the PATH variable and make sure it includes the directory where the ls command is located, which is usually /bin or /usr/bin. The practical implementation of this solution is given below:

Check the PATH Environment Variable

Users can check the PATH variable where the operating system finds executable files by typing the below command:

echo $PATH

The output shows that the /bin directory is included in the return of executing the above command.

Display Full Path

If the /bin directory is not included, users may need to add it to the PATH variable or use the full path of the ls command via the “which” command as below:

which ls

This prints the full path of the ls command which is the usr/bin/ls. 

Add a Directory to the PATH Environment Variable 

Users can also add a directory to the PATH by typing “export PATH=$PATH:/path/to/directory” in the terminal. For instance, add the “bin” directory to the PATH variable via the given script:

export PATH=$PATH:/bin

This will append “/bin” to the end of the existing PATH variable.

Verify Added PATH Environment Variable 

Users can verify the recently added PATH environment variable by typing the following command:

echo $PATH

The output seems that the “/bin” directory is included in the above result. 

Solution 3: Change the Permissions of the ls Command

Another reason that users face with the ls command is due to the file permissions. The ls command should have executable “x” permissions for the user who is trying to run it. Users can display the permissions by executing the below script:

ls -l /bin/ls

The output shows the date, location, and permission of the “ls” directory.

Change Directory Permission 

Users can also change the permissions of the directory “/bin/ls” by mentioning the executable “x” flag with the “chmod” command:

chmod +x /bin/ls

In this way, the directory permission has been changed.

Solution 4: Reinstall the Coreutils Package

The Coreutils package contains the ls command and other essential utilities for Linux. Users can also fix the “ls command not found” error if the package is corrupted or missing by reinstalling the Coreutils package. To reinstall the Coreutils package, follow the below instructions:

Step 1: Update Repository

Before installing any package, the user ensures that the repository is up-to-date. To update the repository, execute the following command with the “sudo” privileges:

sudo apt update    # For Debian, Ubuntu, LinuxMint
sudo yum update    # For CentOS, Oracle Linux, Alma Linux, RHEL

In this way, the system repository has been updated and is ready to install any package.

Step 2: Reinstall the Coreutils Package

To reinstall the coreutils package in Linux based operating system, users can follow the below command with the sudo privileges:

sudo apt install --reinstall coreutils # For Debian, Ubuntu, LinuxMint
sudo yum reinstall coreutils           # For CentOS, Alma Linux, Oracle Linux, RHEL
sudo brew install coreutils            # MacOS X
sudo pacman -S coreutils               # Arch Linux
sudo dnf install coreutils             # Fedora 

The output shows that the coreutils package has been installed in the system.

Solution 5: Reload the Shell Configuration File

To fix this error, users need to check their shell configuration files, e.g. .zshrc or .bashrc, and remove or correct the alias or function that causes the issue. To reload the shell configuration file, users can use the below command which may also fix the error:

source ~/.bashrc

In this way, the shell configuration file is reloaded.

Verification

Now, users can verify that the “ls command not found” error has been resolved by typing the “ls” command as below:

The output shows that the “ls command not found” error has been removed.

Conclusion

To fix the “ls command not found” error, check the spelling and case letter of the command, add, or create an environment PATH variable, and check file permission. Furthermore, users can reinstall Coreutils packages, and reload the shell configuration file. The reasons behind these errors are the PATH environment variable not existing, deleted/renamed/moved ls command, corrupted ls command, changed file permission, and modified shell alias or function. 

This article has explained all reasons and solutions to fix the “ls command not found” error in Linux.