cat/Get-Content:
带行号
PS> $ cat <filePath> | ForEach-Object { "{0} {1}" -f $_.ReadCount,$_ }
# OR
PS> $ forEach ($i in (cat <filePath>)) { "{0} {1}" -f $i.ReadCount,$i }
ren/rename:
重命名目录下的文件:
PS> $ dir ./ | %{ren $_.Name -NewName ("Scenery{0}.png" -f $i++)}
#
PS> $ get-childitem .\packages\ | %{ ren $_ -NewName $_.Name.Replace("winston","") }
Measure-Object:
统计目录下的文件数:
PS> $ (get-childitem .\node_modules\ -File -Recurse | Measure-Object).Count
Clipboard/粘贴板
# 复制当前路径到粘贴板
PS> $ Set-Clipboard $PWD
创建空白文件:
PS> $ echo $Null >> <fileName>
#
PS> $ New-Item <Path> [-Name <FileName>]
Clear Windows Tmp:
PS> $ Get-ChildItem $env:temp | `
Where-Object { $_.Length -ne $null } | `
Where-Object { $_.LastWriteTime -lt $cutoff } | `
Remove-Item -Force -ErrorAction SilentlyContinue -WhatIf #移除WhatIf 会真正删除文件
查看 PowerShell 设置:
PS> $ Get-PSReadlineOption
PS:
在 PowerShell
中,没有 { }
这个通配符 要想实现如下:
LinuxShell:
# 在 src/routes 目录下,同时创建 warn.js 和 error.js
$ mkdir -p ./src/routes/{warn.js,error.js}
上下是否完全等价,这个不能保证,但是效果都是在 ./src/rotues 目录下同时创建 warn.js 和 error.js
PowerShell:
PS> $ New-Item $(@("warn.js", "error.js") | %{ "./src/routes/$_" })
foreach 版本
Powershell:
PS> $ foreach ($i in @("controller", "entity", "repository") ) { mkdir .\user-service\src\main\java\com\example\demo\$i }
下面是错误:
PS> project$ New-Item ./src/routes/warn.js,error.js
# 这个命令 会在 project/src/routes 目录下创建一个 warn.js 文件
# 而 error.js 会被创建在 project 目录下
Get-ChildItem
递归删除指定目录:
$ 递归删除 packages 目录下的所有 dist
PS> $ Get-ChildItem -Path ./packages -Recurse dist | %{ (Test-Path $_) ? (rm -Recurse $_) : (echo "文件夹不存在") }
Others:
PS> $ (cat ./test.log) `
>> -replace "<1匹配的被替换字符串>","1替换字符串" `
>> -replace "<2匹配的被替换字符串>","2替换字符串" |
>> Out-File ./test.log
移除跨行内容:
文件内容:
{
"name": "04__format",
"version": "1.0.0",
"description": "> TODO: description",
"author": "xk ",
"homepage": "",
"license": "ISC",
"main": "src/App.js",
"directories": {
"src": "src",
"test": "__tests__"
},
"files": [
"src"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
}
}
执行:
PS> $ ((Get-Content ./package.json) -join "`n") `
>> -replace ",`n.*__tests__.*",$Null |
>> Out-File ./package.json
移除后的结果:
{
"name": "04__format",
"version": "1.0.0",
"description": "> TODO: description",
"author": "xk ",
"homepage": "",
"license": "ISC",
"main": "src/App.js",
"directories": {
"src": "src"
},
"files": [
"src"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
}
}
删除指定行:
文件内容:
{
"name": "04__format",
"version": "1.0.0",
"description": "> TODO: description",
"author": "xk <771838298@qq.com>",
"homepage": "",
"license": "ISC",
"main": "src/App.js",
"directories": {
"src": "src",
"test": "__tests__"
},
"files": [
"src"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
}
}
PS> $ ((Get-Content ./package.json) |
>> ForEach-Object { ($_.ReadCount -eq 6) ? $NULL : $_ } ) |
>> Out-File ./package.json
删除Windows垃圾:
$cutoff = (Get-Date).AddDays(-30)
$before = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum
Get-ChildItem $env:temp | `
Where-Object { $_.Length -ne $null } | `
Where-Object { $_.LastWriteTime -lt $cutoff } | `
Remove-Item -Force -ErrorAction SilentlyContinue -WhatIf #移除WhatIf 会真正删除文件
获取 powershell 配置:
PS> $ Get-PSReadLineOption
# HistorySavePath: 这个文件记录了所有的 powershell history