如何在Windows命令提示符下创建ls?
我想在 Windows 命令提示符中使用 ls 并使其运行 dir 命令。
我怎样才能做到这一点?
I want to use ls in windows command prompt and make it run the dir command.
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
当然 ls 不能作为批处理的 unix 命令。如果您检查 %1 中的 -l 或 -a 等以及它们的所有组合,它将起作用......
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...
这是我的 C# 源代码和二进制文件。
只需在某处添加 ls.exe 并将路径添加到 path 环境变量即可。
Here is my C# source code and binary.
Just add ls.exe somewhere and add the path to the path environment variable.
您可以使用一个简单的命令来解决这个问题:
如果您使用的是 vista 及更高版本,请确保首先以管理员身份运行 cmd.exe
You can solve this question with one simple command:
Make sure you run
cmd.exe
as admin first if you are on vista and up您可以:
ls.bat
的批处理文件,并让它包含dir
命令,仅ls.bat
文件所在的目录添加到您的PATH
环境变量,然后您可以从命令提示符执行
ls
。You could:
ls.bat
and have it contain thedir
command onlyls.bat
file exists to yourPATH
environment variableYou could then execute
ls
from a command prompt.这是一个老问题,但为了记录:
http://gnuwin32.sourceforge.net/packages/coreutils。 htm
为您提供 ls 以及更多内容!
Its an old question but for the record:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Gives you ls and a whole lot more!
我发现的最简单的方法是:
bin
Git 目录添加到您的 Path 变量中。我的位于C:\Program Files\Git\usr\bin
。Easiest way I have found is:
bin
directory of Git to your Path variable. Mine was located inC:\Program Files\Git\usr\bin
.ls
in all its glory.我有一个解决方案,但它很脏:
创建一个名为 ls.bat 的文件,仅包含“dir”。
将其放在 C:\windows\system32 (或 PATH env var 中的任何目录)中。
那(应该)有效!
编辑:更一致的东西: https://superuser.com/questions/49170/create- Windows XP 中的别名
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
如果您的系统上安装了 Node.js,则可以从 Cash,我为 Windows 上的 Linux 命令编写的库:
If you have Node.js installed on your system, you can install it from Cash, a library I wrote for Linux commands on Windows:
Vista/7 的 Windows 命令提示符将允许 NTFS 符号链接,以管理员身份运行 cmd.exe,然后:
然后设置 PATH 环境变量以包含刚刚创建的链接的位置。
如果您想要的不仅仅是“ls”命令,您应该查看 cygwin。
编辑-刚刚意识到
dir.exe
不是一个单独的程序,所以这实际上不起作用。但是mklink
和 cygwin 是值得了解的好东西。Windows command prompt for Vista/7 will allow NTFS symbolic links, run cmd.exe as administrator then:
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. Butmklink
and cygwin are good things to know about.如果您只想让 cmd 将
ls
识别为dir
的别名,则可以使用doskey
命令(来自 这个关于超级用户的答案)。这不会改变 dir 命令的原始命令行参数处理。
If you just want to have cmd recognize
ls
as an alias fordir
, you can use thedoskey
command (from this answer on superuser).This does not change the original command line parameter handling of the dir command.
我的 ls.bat 位于下面
,可以传输 cli args
将其放入 %windir% 或 %PATH% 变量中的任何目录中。
只需确保使用 ANSI 编码保存文件即可:)
my ls.bat was below
that can transfer cli args
put it in %windir% or any directory in your %PATH% variable.
Just make sure you save the file with ANSI encoding :)
上面的帖子建议为 Windows 安装 git 并将目录 bin 添加到路径变量中+1。
另一种方式是我在 Windows 8 和 Windows 7 计算机上的 cmd.exe 中使用 touch、ls 和许多其他 UNIX 命令。
转到以下站点安装 Cygwin
https://www.cygwin.com/install.html
为您的系统安装 32 或 64 位版本。默认设置和包应包含您需要的内容,因此进入包屏幕后无需更改任何内容。
安装后,将 Cygwin 文件夹路径复制到环境路径变量中。例如;如果您将 cygwin 安装到 C:\Cygwin,则将以下内容添加到您的环境系统路径变量中:
;C:\Cygwin\bin
在我的系统上,我安装了 64 位版本,默认文件夹名称和路径为 C:\cygwin64。因此,我将以下内容添加到系统环境路径变量中:
;C:\cygwin64\bin
如果终端已打开,请重新启动终端。然后输入 ls,您将看到一个目录列表。
如果您不熟悉设置 PATH 环境变量,请参阅以下内容:
超级用户链接 1
超级用户链接 2
+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
您还可以使用 cygwin 并直接使用 ls 命令以及您可能使用的所有其他 unix 命令行工具习惯了。
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.
最简单的方法是
C:\Program Files\Git\usr\bin
安装到你的路径变量中,现在你可以使用
ls
The most easiest way is
C:\Program Files\Git\usr\bin
to your path variablenow you can use
ls
我推荐以下食谱。
DOSKEY
和$*
创建ls
命令。例如,您的命令可能类似于
DOSKEY ls=dir
DOSKEY sublime="C:\Program Files\Sublime Text 2\sublime_text" $*
$*
对于接受参数的命令很有用。例如,在这里我希望能够执行sublime my_code.c
。cmd 的注册表位于 HKEY_CURRENT_USER ->软件->微软->命令处理器。使用文件(不是包含文件夹)的完整路径创建一个名为 AutoRun 的字符串值条目,例如
%USERPROFILE%\custom_command.cmd
。然后每次运行 cmd 时,您的命令都会被加载!您也可以向批处理文件添加更多有用的内容。有关示例模板,请参阅此处。
I recommend the following recipe.
DOSKEY
and$*
to create yourls
command.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 dosublime 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.
对我有用的另一个解决方案是使用 UnxUtils,它从可执行文件(包括
ls
、sed
和grep
)。使用方法:下载源代码。解压缩。将
UnxUtils\usr\local\wbin
路径添加到 Windows PATH 变量。启动一个新的 CMD 实例。Another solution that worked for me is to use UnxUtils, which adds multiple utilities from executable files (including
ls
,sed
, andgrep
).To use: download source code. Unzip. Add the
UnxUtils\usr\local\wbin
path to the Windows PATH variable. Start a new CMD instance.使用 Windows 的 Linux 子系统的人可以从 Linux bash 调用
ls
。以下命令在 System32 中创建 ls 命令:(必须首先启用/安装 Linux 子系统功能)
Someone who uses Linux Subsystem for Windows could call
ls
from the Linux bash. The following Command creates thels
Command in System32:(The Linux Subsystem feature must be enabled/installed first)
您可以遵循本指南:
https://gist.github.com/vladikoff/38307908088d58af206b
TL;DR< /strong>:将
/K path/to/custom/init_cmd.bat
传递给你的“shell启动”命令。我使用 ConsoleZ 作为我的 shell 包装器,因此在我的情况下,我可以在“选项卡”中找到设置选项,然后将 shell 路径设置为
“C:\Windows\System32\cmd.exe”/KC: \cmd_init.bat""
像这样。
其中
C:\cmd_init.bat
是包含我的宏的批处理脚本,这就是我想要的:抱歉格式和其他错误,这是我第一次在这里回答。
我希望它有帮助!
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:Sorry for formatting and other mistakes, this is my first time answering here.
I hope it helps!
使用
doskey
键在 .bat 或 .cmd 文件中创建别名:Enjoy =)
Create an alias in .bat or .cmd file using
doskey
key:Enjoy =)
我记得旧 Unix 时代的一些内容
A few that I remember from the old Unix days