Powershell相当于Linux:ls -al [关闭]

2024-03-29

我不经常使用 Powershell 或 Linux,所以我不确定: ls -al 在 Powershell“术语”中是什么。我试图找到一些东西,但它说 Powershell 中没有等效的命令。看起来很奇怪,所以我想我应该尽快问一下。


The ls您正在使用的 Unix 实用程序选项:

  • -a选项包括hiddenitems,在类 Unix 平台上是文件系统项,其名称以.

  • -l要求所谓的长格式,它生成表格输出,其中包括文件系统项的模式、用户和组所有权、上次写入时间戳、项大小及其名称。

The PowerShell 等效项 is:

Get-ChildItem -Force
  • Get-ChildItem https://learn.microsoft.com/powershell/module/microsoft.powershell.management/get-childitem's -Force开关包括hidden输出中的项目 - 尽管与ls, the . and ..代表目标目录本身及其父目录的条目是not包含(这使得它相当于更有用的-A ls选项)。

  • 相当于长格式使用格式化默认情况下;相反,如果您对文件名(相对路径)感兴趣only, 使用-Name switch.

  • 注意:默认情况下,Get-ChildItem目标是current位置(目录);要显式指定输入路径,请使用-Path参数 - 对于通配符模式 - 或-LiteralPath参数 - 用于文字(逐字)路径。首先位置性的参数(没有参数名称前缀的参数)隐式绑定到-Path;例如,以下命令针对当前用户的主目录:

    Get-ChildItem -Force -LiteralPath $HOME
    
    • See 这个答案 https://stackoverflow.com/a/72495988/45375了解更多信息。

请参阅底部部分缩短此命令 and 定义一个方便的“别名”(函数).


有一个基本的不同之处之间ls and Get-ChildItem, 然而:

  • ls输出格式化文本,因此通常不鼓励解析其输出。

  • 在 PowerShell 中,格式化与 分开data它是objects, not strings是输出。

    • 也就是说,无论有钱人显示格式 that Get-ChildItem输出出现在终端中,其实际输出是objects类型的System.IO.FileInfo https://learn.microsoft.com/en-US/dotnet/api/System.IO.FileInfo and System.IO.DirectoryInfo https://learn.microsoft.com/en-US/dotnet/api/System.IO.DirectoryInfo,可以安全地用于进一步程序化处理如果在变量中捕获,则通过发送到其他 PowerShell 注释pipeline https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines,或用在表达式中。
  • 例如,表达式(Get-ChildItem -Force).FullName输出完整路径当前目录中的所有项目。


For 交互的方便(你不应该在脚本中这样做),你可以使用内置别名 and 明确的参数名称前缀 to 缩短命令:

gci -fo

gci是内置别名Get-ChildItem,其名称遵循 PowerShell 的命名约定,其中每个所谓的批准的动词 https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands还有一封已批准的 1-2 封信alias形式;跑步Get-Verb Get透露,已获批准Get动词的别名形式是g;而noun部分没有受到正式监管,请记住ci代表ChildItem应该很容易。

-fo is the shortest parameter-name prefix that unambiguously refers to the -Force switch (just -f alone could also refer to -Filter[1])

  • 注:部分参数本身有实际aliases, 例如-h for -Hidden or -ad for -Directory, but -Force才不是。
  • 与明确的参数名称前缀不同,如果将新参数添加到命令中,随着时间的推移,参数名称前缀可能会变得模糊,参数别名可以安全地在脚本中使用,尽管为了可读性,最好使用全名。

To see all为给定命令定义的别名(Get-ChildItem在此示例中):

PS> Get-Alias -Definition Get-ChildItem

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           dir -> Get-ChildItem                                          
Alias           gci -> Get-ChildItem                                          

To limit output to built-in aliases, i.e. to exclude aliases defined by other modules or $PROFILE scripts, run pwsh -noprofile { Get-Alias -Definition Get-ChildItem } (in Windows PowerShell, use powershell instead of pwsh.

  • 上面显示了输出macOS 和 Linux.
  • On Windows you will see that ls is another built-in alias.
    • 这是一个legacy alias从 PowerShell 是仅限 Windows 的 shell 开始,最好避免:

      • 一般来说,别名优先 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Command_Precedence超过同名的外部程序,这不是一个好主意shadow(覆盖)平台的标准命令行实用程序;虽然这不适用于别名ls在 Windows 上,在其他情况下也是如此,即where(阴影where.exe) and sc(仅限 Windows PowerShell,阴影sc.exe)

      • 具体来说,考虑到真实的lsUnix 实用程序和Get-ChildItem具有如此截然不同的语法(和行为),将后者暴露为ls很可能会造成混乱。

所以,建议坚持使用那些缩写形式的别名电源外壳命令名称,例如gci for Get-ChildItem, and gc for Get-Content。虽然这最初并不能帮助从熟悉的名称(例如ls / dir and cat事实上,PowerShell cmdlet 名称及其缩写别名是系统化且基于约定的,这使得以后更容易记住它们。

定义一个方便的“别名”预设参数, 用一个function:

POSIX 兼容的 shell,例如bash允许您定义别名preset参数,例如alias ll='ls -al'

相比之下,PowerShell 中的别名 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Aliases仅仅是名称映射并且你需要声明一个function相反(您可以将其添加到您的$PROFILE文件以供将来会议使用):

# Equivalent of bash alias `alias ll='ls -al'`
function ll { ls -al @args }

或者,类似的gcih功能 (h表示hidden) 基于Get-ChildItem:

function gcih { Get-ChildItem -Force @args }

See 这个答案 https://stackoverflow.com/a/55539863/45375有关 PowerShell 中别名与函数的更多信息,包括如何创建更复杂的包装函数。


[1] Conceptually, it could also refer to the -File parameter, but technically the latter is a dynamic parameter, specific to PowerShell's file-system provider, and is therefore not being considered during the ambiguity check.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell相当于Linux:ls -al [关闭] 的相关文章

随机推荐