windows du 在 PowerShell 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/868264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
du in PowerShell?
提问by Cheeso
How can I get a du-ish analysis using PowerShell? I'd like to periodically check the size of directories on my disk.
如何使用 PowerShell进行du-ish 分析?我想定期检查磁盘上目录的大小。
The following gives me the size of each file in the current directory:
下面给出了当前目录中每个文件的大小:
foreach ($o in gci)
{
Write-output $o.Length
}
But what I really want is the aggregate size of all files in the directory, including subdirectories. Also I'd like to be able to sort it by size, optionally.
但我真正想要的是目录中所有文件的总大小,包括子目录。此外,我希望能够按大小对其进行排序,可选。
回答by Tomalak
There is an implementation available at the "Exploring Beautiful Languages" blog:
在“探索美丽的语言”博客中有一个可用的实现:
"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}
}
(Code by the blog owner: Luis Diego Fallas)
(博客所有者的代码:Luis Diego Fallas)
Output:
输出:
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
回答by hollie317704
I modified the command in the answer slightly to sort descending by size and include size in MB:
我稍微修改了答案中的命令以按大小降序排序并包含以 MB 为单位的大小:
gci . |
%{$f=$_; gci -r $_.FullName |
measure-object -property length -sum |
select @{Name="Name"; Expression={$f}},
@{Name="Sum (MB)";
Expression={"{0:N3}" -f ($_.sum / 1MB) }}, Sum } |
sort Sum -desc |
format-table -Property Name,"Sum (MB)", Sum -autosize
Output:
输出:
PS C:\scripts> du
Name Sum (MB) Sum
---- -------- ---
results 101.297 106217913
SysinternalsSuite 56.081 58805079
ALUC 25.473 26710018
dir 11.812 12385690
dir2 3.168 3322298
Maybe it is not the most efficient method, but it works.
也许这不是最有效的方法,但它有效。
回答by Damien Gregory
function Get-DiskUsage ([string]$path=".") {
$groupedList = Get-ChildItem -Recurse -File $path | Group-Object directoryName | select name,@{name='length'; expression={($_.group | Measure-Object -sum length).sum } }
foreach ($dn in $groupedList) {
New-Object psobject -Property @{ directoryName=$dn.name; length=($groupedList | where { $_.name -like "$($dn.name)*" } | Measure-Object -Sum length).sum }
}
}
Mine is a bit different; I group all of the files on directoryname, then walk through that list building totals for each directory (to include the subdirectories).
我的有点不同;我将目录名上的所有文件分组,然后遍历每个目录(包括子目录)的列表构建总数。