我不经常使用 Powershell 或 Linux,所以我不确定是什么: ls -al 将在 Powershell“术语”中。我试图找到一些东西,但它说 Powershell 中没有等效的命令。看起来很奇怪,所以我想我会很快问。
等效于 Linux 的 Powershell:ls -al
您正在使用的 ls
Unix实用程序选项:
-
-a
选项包括hidden
项目,在unix样平台上是文件系统项目,其名称以.
开头 -
-l
请求所谓的long format
,该 ```
Get-ChildItem -Force
** powershell等效**是:
[`Get-ChildItem`](https://docs.microsoft.com/powershell/module/microsoft.powershell.management/get-childitem)
* `-Force` 's `hidden` switch包括 `ls` 在输出中 - 尽管与 `.` 不同, `..` , `not` 和 `-A` 条目代表目标目录本身及其父母本身及其父母目录为 `ls` 包括(这使其成为更有用 `long format` `by default` 选项的等效物)。
*使用 `only` 格式的等效 `-Name` ;相反,如果您对文件名(相对路径) `Get-ChildItem` 感兴趣,请使用 `current` switch。
*注意:默认情况下, `-Path` 靶向 `-LiteralPath` location(directory);要明确指定输入路径,请使用 `positional` 参数 - 用于通配符模式 - 或 `-Path` 参数 - 用于文字(verbatim)路径。第一个 ```
Get-ChildItem -Force -LiteralPath $HOME
``` 参数(一个未由参数名称前缀)隐式绑定到 [this answer](https://stackoverflow.com/a/72495988/45375) ;例如,以下命令针对当前用户的主目录:
`fundamental`
*有关更多信息,请参见 `ls` 。
请参阅底部,以获取**缩短此命令**和**定义便利性“别名”(函数)**。
* * *
但是,有一个** `Get-ChildItem` 差异** `ls` 和 `formatted text` 之间有一个差异,但是:
* `formatting` 输出 `data` ,因此,通常不建议解析其输出。
*在PowerShell中, `objects` 与 `strings` 分开,它是 `display formatting` ,而不是输出的 `Get-ChildItem` 。
*也就是说,无论 `objects` 的富 [`System.IO.FileInfo`](https://docs.microsoft.com/en-US/dotnet/api/System.IO.FileInfo) ,输出在终端中呈现,其实际输出为 [`System.IO.DirectoryInfo`](https://docs.microsoft.com/en-US/dotnet/api/System.IO.DirectoryInfo) 的 `programmatic processing` , [pipeline](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines) 和 `(Get-ChildItem -Force).FullName` ,可安全地用于进一步 `full paths` 如果在变量中捕获,则通过 `interactive` 发送到其他PowerShell注释,或在表达式中使用。
*例如,表达式 `**built-in aliases**` 输出当前目录中所有项目的 `**unambiguous parameter-name prefixes**` 。
* * *
**对于 ```
gci -fo
``` 便利**(您不应该在脚本中这样做),您可以使用 `gci` 和 `Get-ChildItem` 来**缩短命令**:
[approved verb](https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands)
`alias` 是 `Get-Verb Get` 的内置别名,其名称遵循PowerShell的命名约定,其中每个所谓的 `Get` 也有一个批准的1-2个字母 `g` 表单;运行 `noun` 表明,批准的 `ci` 动词的别名表格为 `ChildItem` ;尽管 `-fo` 零件未正式调节,但请记住 `-Force` 代表 `-f` 应该很容易。
`-Filter` 是最短的参数名称前缀,明确地指的是 [1])
* Note: Some parameters themselves have actual `aliases`, such as `-h` for `-Hidden` or `-ad` for `-Directory`, but `-Force` does not.
* Unlike unambiguous parameter-name prefixes, which can become ambiguous over time if new parameters are added to a command, parameter aliases are safe to use in scripts, although for readability the full names are preferable.
To see `all` aliases defined for a given command (`Get-ChildItem` in this example):
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`.
* The above shows the output on `macOS and Linux`.
* **On `Windows` you will see that `ls` is `another` built-in alias**.
* This is a **`legacy` alias** from the time PowerShell was a Windows-only shell, and is **best avoided**:
* In general, [aliases take precedence](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Command_Precedence) switch(仅 `shadow` ,也可以指带有相同名称的外部程序,并且它可以指 `ls` `where` `where.exe` `sc` (覆盖)平台的标准命令行实用程序不是一个好主意;虽然这不适用于Windows上的别名 `sc.exe` ,但在其他情况下则是 `ls` (shadows) `Get-ChildItem` )和 `ls` (Windows PowerShell-仅,Shadows `PowerShell` )
*具体而言,鉴于真实的 `gci` Unix Utility和 `Get-ChildItem` 具有巨大不同的语法(和行为),因此将后者暴露为 `gc` 可能会引起混淆。
因此,**建议坚持那些是 `Get-Content` 命令名称的缩写形式的别名,例如 `ls` 的 `dir` ,以及 `cat` for `preset arguments` **。虽然最初没有帮助从熟悉的名称过渡,例如 `function` / `bash` 和 `preset` ,但powerShell cmdlet及其缩写的别名是系统的和基于惯例的异常,这一事实变得更加容易。记住他们前进。
**使用 `alias ll='ls -al'` **:用 [aliases in PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Aliases) 定义便利性“别名”:
`name mappings` 等posix兼容的外壳允许您使用 `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 }
``` 的类似 [this answer](https://stackoverflow.com/a/55539863/45375) 函数( `Conceptually` )表示 `-File` ):
`technically`
有关别名与PowerShell功能的更多信息,请参见 `dynamic` ,包括如何创建更复杂的包装器功能。
* * *
[1] ,它也可以参考 参数,但是 后者是 参数,特定于PowerShell的文件系统提供商,因此在期间没有考虑歧义检查。