How to create ls in windows command prompt?

WindowsWindows XpCmdLsDir

Windows Problem Overview


I want to use ls in windows command prompt and make it run the dir command.

How can I do that?

Windows Solutions


Solution 1 - Windows

You can solve this question with one simple command:

echo @dir %* > %systemroot%\system32\ls.bat

Make sure you run cmd.exe as admin first if you are on vista and up

Solution 2 - Windows

You could:

  • create a batch file called ls.bat and have it contain the dir command only
  • add the directory where the ls.bat file exists to your PATH environment variable

You could then execute ls from a command prompt.

Solution 3 - Windows

Its an old question but for the record:

http://gnuwin32.sourceforge.net/packages/coreutils.htm

Gives you ls and a whole lot more!

Solution 4 - Windows

Easiest way I have found is:

  1. Install Git for Windows
  2. Add the bin directory of Git to your Path variable. Mine was located in C:\Program Files\Git\usr\bin.
  3. Start a command prompt and enjoy ls in all its glory.

Solution 5 - Windows

I have a solution but it's dirty:

Create a file named ls.bat containing only "dir".

Put it in C:\windows\system32 (or any directory in PATH env var).

That (should) works!

Edit: Something more consistent: https://superuser.com/questions/49170/create-an-alias-in-windows-xp

Solution 6 - Windows

If you have Node.js installed on your system, you can install it from Cash, a library I wrote for Linux commands on Windows:

npm install cash-ls -g

Solution 7 - Windows

Windows command prompt for Vista/7 will allow NTFS symbolic links, run cmd.exe as administrator then:

mklink ls %System%\dir.exe

Then set up your PATH environment variable to include the location of the link you just created.

If you want more than just the 'ls' command, you should look into cygwin.

EDIT- Just realized dir.exe is not a separate program, so this doesn't really work. But mklink and cygwin are good things to know about.

Solution 8 - Windows

If you just want to have cmd recognize ls as an alias for dir, you can use the doskey command (from this answer on superuser).

This does not change the original command line parameter handling of the dir command.

Solution 9 - Windows

my ls.bat was below

@dir %*

that can transfer cli args

ls /b
ls /w

put it in %windir% or any directory in your %PATH% variable.

Just make sure you save the file with ANSI encoding :)

Solution 10 - Windows

+1 on the post above suggesting to install git for windows and add the directory bin to your path variables.

Another way I got touch, ls, and a lot of other UNIX commands working in cmd.exe on my Windows 8 and Windows 7 machines.

Go to the following site to install Cygwin

https://www.cygwin.com/install.html

Install the 32 or 64 bit version for your system. The default settings and packages should include what you need so you don't have to change anything once you get to the packages screen.

After installation, copy the Cygwin folder path to your environment path variables. For example; if you installed cygwin to C:\Cygwin, you will add the following to your environment system path variables:

;C:\Cygwin\bin

On my system I installed the 64bit version and the default folder name and path was C:\cygwin64. So i added the following to my system environment path variables:

;C:\cygwin64\bin

Restart your terminal if it's open. Then type ls and you'll see a directory listing.

See the following if you are not familiar with setting PATH environment variables:

Superuser Link 1

Superuser Link 2

Solution 11 - Windows

you could also use cygwin and just use the ls command directly along with all the other unix command line tools you might be used to.

Solution 12 - Windows

I recommend the following recipe.

  1. Use DOSKEY and $* to create your ls command.
  2. Make the command persistent by recording it in a .bat/.cmd file and add the path of the file to registry.

For example, your command may look like
DOSKEY ls=dir
DOSKEY sublime="C:\Program Files\Sublime Text 2\sublime_text" $*
$* is useful for commands that take on arguments. For example, here I like to be able to do sublime my_code.c.

The registry for cmd is at HKEY_CURRENT_USER -> Software -> Microsoft -> Command Processor. Create a string valued entry called AutoRun with the full path of the file (not the containing folder) such as %USERPROFILE%\custom_command.cmd. Then each time cmd is run, your command will be loaded!

You can add more useful stuffs to the batch file too. See here for an example template.

Solution 13 - Windows

Another solution that worked for me is to use UnxUtils, which adds multiple utilities from executable files (including ls, sed, and grep).

To use: download source code. Unzip. Add the UnxUtils\usr\local\wbin path to the Windows PATH variable. Start a new CMD instance.

Solution 14 - Windows

The most easiest way is

  • install git
  • add C:\Program Files\Git\usr\bin to your path variable

now you can use ls

Solution 15 - Windows

Surely ls would not work as a unix command for the batches. If you check %1 for -l or -a etc. and all combinations of them, it would work...

Solution 16 - Windows

You could follow this guide: https://gist.github.com/vladikoff/38307908088d58af206b

TL;DR: pass /K path/to/custom/init_cmd.bat to your "shell startup" command.

I'm using ConsoleZ as my shell wrapper, so in my case I can find the setup option in "tabs", then I set the shell path to "C:\Windows\System32\cmd.exe "/K C:\cmd_init.bat"" like this.

Where C:\cmd_init.bat is the batch script containing my macros, here's what I would go for:

@echo off

doskey ls=dir /b
rem other macro stuff..

Sorry for formatting and other mistakes, this is my first time answering here. I hope it helps!

Solution 17 - Windows

Someone who uses Linux Subsystem for Windows could call ls from the Linux bash. The following Command creates the ls Command in System32:

echo @bash -c "ls %*" > %systemroot%\system32\ls.bat

(The Linux Subsystem feature must be enabled/installed first)

Solution 18 - Windows

Create an alias in .bat or .cmd file using doskey key:

@echo off
title "ls command cmd bar"
doskey ls=echo off $T dir $* $T echo on

Enjoy =)

Solution 19 - Windows

Here is my C# source code and binary.

Just add ls.exe somewhere and add the path to the path environment variable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaF.View Question on Stackoverflow
Solution 1 - WindowssecghostView Answer on Stackoverflow
Solution 2 - WindowshmjdView Answer on Stackoverflow
Solution 3 - WindowssipicklesView Answer on Stackoverflow
Solution 4 - WindowsvaragrawalView Answer on Stackoverflow
Solution 5 - WindowsSTMView Answer on Stackoverflow
Solution 6 - WindowsdthreeView Answer on Stackoverflow
Solution 7 - WindowsbusterView Answer on Stackoverflow
Solution 8 - WindowsdevioView Answer on Stackoverflow
Solution 9 - WindowsIlPADlIView Answer on Stackoverflow
Solution 10 - WindowsjtlindseyView Answer on Stackoverflow
Solution 11 - WindowsNerdtronView Answer on Stackoverflow
Solution 12 - WindowsArgyllView Answer on Stackoverflow
Solution 13 - WindowsEvanView Answer on Stackoverflow
Solution 14 - Windowsuser14233213View Answer on Stackoverflow
Solution 15 - WindowsdocowhocoolView Answer on Stackoverflow
Solution 16 - Windowsjred_kaiView Answer on Stackoverflow
Solution 17 - WindowsA. MarkóczyView Answer on Stackoverflow
Solution 18 - WindowsBk01View Answer on Stackoverflow
Solution 19 - Windowsuser3880586View Answer on Stackoverflow