你在 PowerShell 中吗?
如何使用 PowerShell 获得 du 式分析? 我想定期检查磁盘上目录的大小。
以下给出了当前目录中每个文件的大小:
foreach ($o in gci)
{
Write-output $o.Length
}
但我真正想要的是目录中所有文件(包括子目录)的总大小。 另外,我希望能够根据大小对其进行排序(可选)。
How can I get a du-ish analysis using PowerShell? I'd like to periodically check the size of directories on my disk.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
“探索美丽的语言”博客中有一个实现:
“Powershell 中“du -s *”的实现”
(博客所有者的代码:Luis Diego Fallas)
输出:
There is an implementation available at the "Exploring Beautiful Languages" blog:
"An implementation of 'du -s *' in Powershell"
(Code by the blog owner: Luis Diego Fallas)
Output:
我稍微修改了答案中的命令,以按大小降序排序,并包含以 MB 为单位的大小:
输出:
也许这不是最有效的方法,但它有效。
I modified the command in the answer slightly to sort descending by size and include size in MB:
Output:
Maybe it is not the most efficient method, but it works.
如果您只需要该路径的总大小,可以使用一个简化版本,
If you only need the total size of that path, one simplified version can be,
我的有点不同; 我将目录名上的所有文件分组,然后遍历每个目录的构建总数列表(包括子目录)。
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).
基于之前的答案,这适用于那些想要以 KB、MB、GB 等显示大小,并且仍然能够按大小排序的人。 要更改单位,只需将“Name=”和“Expression=”中的“MB”更改为所需单位即可。 您还可以通过更改“2”来更改要显示的小数位数(四舍五入)。
这将大小作为数字而不是字符串(如另一个答案中所示)提供,因此可以按大小排序。 例如:
Building on previous answers, this will work for those that want to show sizes in KB, MB, GB, etc., and still be able to sort by size. To change units, just change "MB" to desired units in both "Name=" and "Expression=". You can also change the number of decimal places to show (rounding), by changing the "2".
This gives the size as a number not a string (as seen in another answer), therefore one can sort by size. For example:
我自己使用之前的答案的看法:
My own take using the previous answers:
使用 Get-Chiltree 并没有那么慢:
Using Get-Chiltree, is not that slow :