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
2009-04-28 13:55:11
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
2009-04-28 14:20:26
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
2009-04-28 14:28:13
A:
On linux:
ls -XS | head -n 10
EDIT: Oh, now I see the tag "powershell", sorry for my useless answer ;)
akappa
2009-04-28 14:01:05
+3
A:
This can be simplified a bit because Directories have no length:
gci . -r | sort Length -desc | select fullname -f 10
Keith Hill
2009-05-01 18:25:24
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
2009-05-02 06:02:30