The ls
您正在使用的 Unix 实用程序选项:
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.
所以,建议坚持使用那些缩写形式的别名电源外壳命令名称,例如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.