PowerShell 类似于“dir /a:d” (Win) 或“ls -d */” (重击)
我只想使用 PowerShell 列出当前工作目录下的所有目录。这在 Bash shell
ls -d */
或 Windows 中的 cmd.exe
dir /a:d
中很容易实现:使用 PowerShell,但是我似乎无法使用单个命令来完成此操作。相反,我发现唯一有效的是:
ls | ? {$_Mode -like "d*"}
这似乎太冗长和复杂,而且我怀疑我不需要一个单独的Where子句。不过,Get-ChildItem 的帮助并没有明确说明如何过滤模式。谁能启发我吗?
I simply want to list all of the directories under my current working directory, using PowerShell. This is easy from a Bash shell:
ls -d */
or cmd.exe in Windows:
dir /a:d
Using PowerShell however I cannot seem to be able to do it with a single command. Instead the only the I've found that works is:
ls | ? {$_Mode -like "d*"}
That seems way too wordy and involved, and I suspect that I don't need a separate Where clause there. The help for Get-ChildItem doesn't make it clear how to filter on Mode though. Can anyone enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这也有效:
毫无疑问,它比 bash 或 cmd.exe 更冗长一些。如果您想减少冗长,您当然可以在您的配置文件中添加一个函数和一个别名。我会看看是否能找到一种使用 -filter 的方法。
经过进一步调查,我认为除了创建自己的函数和别名之外,没有更简洁的方法可以做到这一点。您可以将其放入您的个人资料中:
然后将 ls 文件夹中的目录:
此解决方案会受到一些限制,因为它无法处理任何花哨的内容,例如 -Include、-Exclude、-Filter、-Recurse 等,但您可以轻松地将其添加到函数中。
实际上,这是一个相当幼稚的解决方案,但希望如果您决定追求它,它将引导您走向正确的方向。老实说,我不会打扰你。在我个人看来,powershell 总体上更大的灵活性足以克服这种情况下的额外冗长。
This works too:
There is no doubt that it is a little more wordy than bash or cmd.exe. You could certainly put a function and an alias in your profile if you wanted to reduce the verbosity. I'll see if I can find a way to use -filter too.
On further investigation, I don't believe there is a more terse way to do this short of creating your own function and alias. You could put this in your profile:
Then to ls the directories in a folder:
This solution would be a little limited since it would not handle any fanciness like -Include, -Exclude, -Filter, -Recurse, etc. but you could easily add that to the function.
Actually, this is a rather naive solution, but hopefully it will head you in the right direction if you decide to pursue it. To be honest with you though I wouldn't bother. The extra verbosity in this one case is more than overcome by the overall greater flexibility of powershell in general in my personal opinion.
尝试:
Try:
我发现这比 Plus 更容易记住
,输入速度更快,因为您可以使用 -ex 而不是 -exclude 或使用 tab 来展开它。
I find this easier to remember than
Plus, it is faster to type, as you can do -ex instead of -exclude or use tab to expand it.
您现在可以使用
Get-ChildItem -Directory
或简称ls -dir
。根据 微软的文档。You can now use
Get-ChildItem -Directory
orls -dir
for short. This has existed at least since PowerShell 3.0 according to Microsoft's documentation.您可以查看 PowerShell 团队博客上的旧帖子: http: //blogs.msdn.com/powershell/archive/2009/03/13/dir-ad.aspx
You can check old post on PowerShell team blog: http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx
我来到这个帖子是因为我想弄清楚“FOR /D”是如何工作的。实际上如何使用带有 /D 选项的批处理命令转义(%)。
我满怀希望地阅读了上述内容,说实话,它们都比 FOR 命令选项复杂得多——当然,如果它有效的话。
使用 for 的其他形式
仅限目录
I came to this thread because I'm trying to figure out how "FOR /D" works. Well actually how to use the batch-command escape(%) with the /D option.
I read the above items with hope, to be honest they're all a lot more complex than the FOR command option -- If it worked of course.
Using additional forms of for
Directories only
好吧,很快我们就必须创建一个类来简化事情并输入类似的命令
dir -options {122b312aa3132-1313-131112f8111111} 这样我们就可以执行与
dir/ad/od相同的操作
Well, soon we will have to a make a class to simplify things and be typing commands like
dir -options {122b312aa3132-1313-131112f8111111} just so we can do the same as
dir/ad/od