如何使用PowerShell进行
du-ish分析?我想定期检查磁盘上目录的大小。
以下给出了当前目录中每个文件的大小:
foreach ($o in gci)
{
Write-output $o.Length
}
但我真正想要的是目录中所有文件的聚合大小,包括子目录。此外,我也希望能够根据大小进行排序。
博客中的“探索美丽语言”有一个实现:
“An implementation of ‘du -s *’ in Powershell”
function directory-summary($dir=".") {
get-childitem $dir |
% { $f = $_ ;
get-childitem -r $_.FullName |
measure-object -property length -sum |
select @{Name="Name";Expression={$f}},Sum}
}
(代码由博主:Luis Diego Fallas)
输出:
PS C:\Python25> directory-summary Name Sum ---- --- DLLs 4794012 Doc 4160038 include 382592 Lib 13752327 libs 948600 tcl 3248808 Tools 547784 LICENSE.txt 13817 NEWS.txt 88573 python.exe 24064 pythonw.exe 24576 README.txt 56691 w9xpopen.exe 4608