前置要求
文件名与别名冲突
原因是PS的别名移除命令只能移除当前会话的别名,重写打开PS别名还会存在
- 打开PowerShell的配置文件:notepad $profile
- 添加移除别名的语句:Remove-Alias ls
-
ls
```bash function Get-List {
param (
[bool]$isLs
)
$regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled) $compressed = New-Object System.Text.RegularExpressions.Regex(‘.(zip|tar|gz|rar|jar|war|7z)$’, $regex_opts) $executable = New-Object System.Text.RegularExpressions.Regex(‘.(exe|bat|cmd|py|ps1|psm1|vbs|rb|reg|sh|zsh)$’, $regex_opts) $code_files = New-Object System.Text.RegularExpressions.Regex(‘.(ini|csv|log|xml|yml|json|java|c|cpp|css|sass|js|ts|jsx|tsx|vue)$’, $regex_opts)
$itemList = @() Get-ChildItem | ForEach-Object {
$color = ""$length = $_.Lengthif ($_.GetType().Name -eq 'DirectoryInfo') {$color = "`e[34m" # 目录名称蓝色$length = ""}elseif ($compressed.IsMatch($_.Name)) {$color = "`e[31m" # 压缩文件红色}elseif ($executable.IsMatch($_.Name)) {$color = "`e[32m" # 可执行文件青色}elseif ($code_files.IsMatch($_.Name)) {$color = "`e[33m" # 代码文件黄色}else {$color = "`e[37m" # 其他文件默认白色}$itemList += [PSCustomObject]@{Mode = $color + $_.ModeLastWriteTime = $color + $_.LastWriteTimeLength = $color + $lengthName = $color + $_.Name}
} if ($isLs) {
Write-Output $itemList | Format-Wide -AutoSize
} else {
Write-Output $itemList | Format-Table
} }
function ls { Get-List $true }
<a name="SzMB1"></a>### ll```bashfunction Get-List {param ([bool]$isLs)$regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)$compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar|jar|war|7z)$', $regex_opts)$executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|py|ps1|psm1|vbs|rb|reg|sh|zsh)$', $regex_opts)$code_files = New-Object System.Text.RegularExpressions.Regex('\.(ini|csv|log|xml|yml|json|java|c|cpp|css|sass|js|ts|jsx|tsx|vue)$', $regex_opts)$itemList = @()Get-ChildItem | ForEach-Object {$color = ""$length = $_.Lengthif ($_.GetType().Name -eq 'DirectoryInfo') {$color = "`e[34m" # 目录名称蓝色$length = ""}elseif ($compressed.IsMatch($_.Name)) {$color = "`e[31m" # 压缩文件红色}elseif ($executable.IsMatch($_.Name)) {$color = "`e[32m" # 可执行文件青色}elseif ($code_files.IsMatch($_.Name)) {$color = "`e[33m" # 代码文件黄色}else {$color = "`e[37m" # 其他文件默认白色}$itemList += [PSCustomObject]@{Mode = $color + $_.ModeLastWriteTime = $color + $_.LastWriteTimeLength = $color + $lengthName = $color + $_.Name}}if ($isLs) {Write-Output $itemList | Format-Wide -AutoSize}else {Write-Output $itemList | Format-Table}}function ll {Get-List $false}
参考:
https://www.toutiao.com/a7020409939063521822/?log_from=526675665ae6b_1637408373531
