tags:

views:

496

answers:

3

How could one find the 10 largest files in a directory structure?

+8  A: 

Try this script

gci -re -in * |
  ?{ -not $_.PSIsContainer } |
  sort Length -descending |
  select -first 10

Breakdown:

The filter block "?{ -not $_.PSIsContainer }" is meant to filter out directories. The sort command will sort all of the remaining entries by size in descending order. The select clause will only allow the first 10 through so it will be the largest 10.

JaredPar
That's great ---- I figured out how to format the final result using format-table but what I"m not clear on is the syntax ?{ } and the $_ I'm guessing these are shortforms for some statement construct?
Ralph Shillington
The ?{} syntax is short for where-object which is a filter expression. Essentially powershell will pass every object to the ?{} clause in the from of $_. If the return is $true, then the object will continue down the pipeline, otherwise it will be filtered out.
JaredPar
A: 

On linux:

ls -XS | head -n 10

EDIT: Oh, now I see the tag "powershell", sorry for my useless answer ;)

akappa
He asked for a powershell sample
JaredPar
Yes, but or he adds the "powershell" tag only later, or I haven't see it at the time of posting my answer :)
akappa
+3  A: 

This can be simplified a bit because Directories have no length:

gci . -r | sort Length -desc | select fullname -f 10
Keith Hill
+1 I'll use this in the future (less typing).
Ralph Shillington
Plus the '.' isn't even required. Could simplify that front part to just "gci -r | ..." since the default path is the current dir.
Keith Hill