How to add ls and other Linux commands into Windows command line
Tags: windows, commandline, linuxcommands, git
Published: 2022-12-25 18:41:34

Whenever I happen to be using the command line on Windows, I always end up running `ls` command and getting the annoying error `'ls' is not recognized as an internal or external command, operable program or batch file`. This command is sometimes available in powershell, but never by default in cmd. Turns out there is an easy way to make it available, and in this post I will explain how.

The trick relies on `git` being installed on Windows system already. My assumption here is that you, the reader, is someone who works in tech and therefore chances are, you already have git on your machine. If that is not the case, you can [install git by following this guide](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). 

TL;DR - **what's the trick** - `git` is a tool that allows you to manage versions of your code projects and happens to bring the Windows executables for a lot of linux commands with the installation. You can add the directory where these executables are stored into the path and make them available in the CMD. Steps how to achieve this are bellow:

- Find the `usr\bin` subfolder of the git installation folder (by default, it will be `C:\Program Files\Git\usr\bin`). If you installed git somewhere else, find the `usr\bin` directory inside that other location.
- Open `This PC` or `Computer`, right click in the directory and select `Properties`. Control Panel > System and Security > System window will open.
- On the left, choose `Advanced system settings` and click `Environment Variables` at the bottom.

![Image of System and Security / System in Control Panel, Advanced system settings option in the left menu is highlighted](https://danieldusek.com/images/008_system_and_security_advanced_system_settings_highlighted.PNG)

- In the `Environment Variables` window, find `Path` variable in the `System Variables` on the bottom part of the screen.
- Click `Edit` and add path to the `user\bin` subdirectory of git installation folder in there:
    - If git is installed in `C:\Program Files\Git`
    - Path to add is: `C:\Program Files\Git\usr\bin`
- Click `OK`, then `OK`, then `OK` and then **open the new command line**. Running the `ls` command now will result in listing the items of a current working directory. 

## Common issues

1. **I can't find my git installation folder, how do I look it up?** If you can run the `git` command from commandline:
    - Go to `This PC`, right click and select `Properties`
    - Select `Advanced system settings` on the left
    - Select `Environment Variables` on the bottom and look for anything with a `git` in the path. There you will eventually find the actual git installation directory.
2. **I followed the tutorial and still can't use `ls` in my CMD, what should I do?** Start a **new** commandline window:
    - Either press `Windows Key` + `R` and type in CMD, or just find it in the start menu.
    -  Run `ls` again and it should work now. 
    - Why? You were probably trying to run the command from previously opened CMD, which read the old value of Path variable when it was starting. It doesn't know about the changes you have made into the path yet.