How to add ls and other Linux commands into Windows commandline
2022-12-25 | Tutorials
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.
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 beC:\Program Files\Git\usr\bin
). If you installed git somewhere else, find theusr\bin
directory inside that other location. - Open
This PC
orComputer
, right click in the directory and selectProperties
. Control Panel > System and Security > System window will open. - On the left, choose
Advanced system settings
and clickEnvironment Variables
at the bottom.
)
- In the
Environment Variables
window, findPath
variable in theSystem Variables
on the bottom part of the screen. - Click
Edit
and add path to theuser\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
- If git is installed in
- Click
OK
, thenOK
, thenOK
and then open the new command line. Running thels
command now will result in listing the items of a current working directory.
Common issues
- 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 selectProperties
- Select
Advanced system settings
on the left - Select
Environment Variables
on the bottom and look for anything with agit
in the path. There you will eventually find the actual git installation directory.
- Go to
- 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.
- Either press