【PowerShell】常用命令

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
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值