How to create ls in windows command prompt?
Asked Answered
C

19

130

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

How can I do that?

Caritta answered 20/2, 2012 at 14:25 Comment(7)
Some of the answers are quite interesting, but what exactly is wrong with the 'dir' command?Uncial
@MattDonnan I'm just used to ls and I make a funny face when I make it on windows. Not anymore ^^Caritta
@aF If you want ls, why don't you just use the real ls?Euraeurasia
@DavidHeffernan because it doesn't exist in windows?Caritta
Sure it does. I use it every day.Euraeurasia
@DavidHeffernan and your windows version is?Caritta
@Caritta I'm using Windows 7, but I run ls on any Windows version. I'm not saying that Windows comes with ls, just that it being open source, and having good Windows ports, it's trivial to run it on Windows. Look at GnuWin32.Euraeurasia
T
122

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.

Tympanitis answered 20/2, 2012 at 14:32 Comment(6)
why not put the ls.bat in system32 folder so you don't have to add the PATHPropound
@David天宇Wong, it is very possible that the user may not have write access to that folder.Tympanitis
does this still work if you use it with args: ls dir ? I think you would need to modify the ls.bat to handle args... or you could try aliases: superuser.com/questions/49170/create-an-alias-in-windows-xpReeta
I like: @echo off\ndir /w %1% to get closer to ls formatPourpoint
excellent answer, it shouldn't be possible to comment on this ;-)Bacillary
You could also install cygwin and use the ls there :>Farmstead
B
310

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

Bossy answered 23/2, 2013 at 17:35 Comment(5)
This is better because you can still use the dir options.Isometric
when i execute it it tells me access denied in windows 10. i gave write access to my user but yet didnt work. any idea?Mistymisunderstand
It works for me in Windows10 (but you have to use a command line that has administrator privilegeMyrtice
while you're at it you may also want to add: echo cls %1 > %systemroot%\system32\clear.batLocker
to use clear comand instead cls echo cls > %systemroot%\system32\clear.batDaley
T
122

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.

Tympanitis answered 20/2, 2012 at 14:32 Comment(6)
why not put the ls.bat in system32 folder so you don't have to add the PATHPropound
@David天宇Wong, it is very possible that the user may not have write access to that folder.Tympanitis
does this still work if you use it with args: ls dir ? I think you would need to modify the ls.bat to handle args... or you could try aliases: superuser.com/questions/49170/create-an-alias-in-windows-xpReeta
I like: @echo off\ndir /w %1% to get closer to ls formatPourpoint
excellent answer, it shouldn't be possible to comment on this ;-)Bacillary
You could also install cygwin and use the ls there :>Farmstead
A
41

Its an old question but for the record:

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

Gives you ls and a whole lot more!

Astronomy answered 25/4, 2013 at 8:35 Comment(3)
+1 ls * and dir * don't do the same thing, so aliasing dir is a bit pants.Brecciate
This is the best answer. Ignore the voting and do this.Reinaldo
don't forget to add C:\Program Files (x86)\GnuWin32\bin to your pathMayor
H
29

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.
Haldi answered 28/11, 2014 at 13:30 Comment(3)
Great suggestion that gives access to full functionality of ls and all it's optional arguments. Just a slight update, I have installed the latest version of git for windows and the ls.exe can now be found in C:\Program Files\Git\ usr \binUnrig
Yes, or just start bash.exe in git folder (instead of cmd) and enjoy a ~complete shell (with ls).Ance
git bash is hideous, i like the path optionMayor
C
18

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

Camarillo answered 20/2, 2012 at 14:32 Comment(0)
C
9

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
Colville answered 27/2, 2016 at 2:58 Comment(2)
best solution ever for linux user on windows, npm install cash-global -g . Would be great to add ls -l as ll (LL) also.Willemstad
Its a saviour mateCathodoluminescence
B
7

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.

Bottomless answered 20/2, 2012 at 14:34 Comment(1)
+1, I forgot to say that was windows xp but that is good for windows vista/7 :)Caritta
P
5

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.

Precept answered 20/2, 2012 at 14:34 Comment(0)
O
5

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 :)

Olsewski answered 12/12, 2013 at 4:1 Comment(0)
P
5

+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

Phung answered 18/2, 2015 at 5:53 Comment(0)
M
3

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.

Mariettemarigold answered 20/2, 2012 at 14:43 Comment(0)
D
3

The most easiest way is

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

now you can use ls

Decretive answered 14/9, 2020 at 13:15 Comment(0)
T
2

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.

Thong answered 10/1, 2014 at 9:57 Comment(0)
E
2

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.

Erysipelas answered 19/12, 2017 at 17:40 Comment(0)
H
1

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)

Holle answered 22/7, 2019 at 12:5 Comment(0)
H
0

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!

Heifetz answered 30/6, 2017 at 15:24 Comment(0)
B
0

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 =)

Baelbeer answered 25/11, 2020 at 21:7 Comment(0)
F
-1

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...

Fabric answered 17/12, 2013 at 16:10 Comment(0)
E
-3

Here is my C# source code and binary.

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

Ethno answered 2/6, 2017 at 18:48 Comment(2)
A binary is unnecessary. Just use a symlink or bat file.Sari
!! What a complicated way to go with .. "I need to kill that dumb fly that's been bothering me."-- "OK Frank, lock the doors and seal the windows, I've got a bug bomb, it'll take 8 hours"Atrip

© 2022 - 2024 — McMap. All rights reserved.