Comment Re:The philosophy behind textual data (Score 1) 442
Powershell doesn't have a du command as standard - however I knocked together this script:
function global:Measure-Directory
{
param (
[string]$Path = (write-error `
"Usage: Measure-Directory [-Path] path [-Recurse]"),
[switch]$Recurse = $false
)
process {
gci $Path -Rec:$Recurse |
? { $_.psiscontainer } |
% { $dir=$_; gci $_.fullname |
? { -not $_.psiscontainer } |
measure -sum -average -minimum -maximum length |
% {
add-member -name Length -value $_.Sum -membertype NoteProperty -pass -inp $dir |
add-member -name Count -value $_.Count -membertype NoteProperty -pass |
add-member -name Average -value $_.Average -membertype NoteProperty -pass |
add-member -name Minimum -value $_.Minimum -membertype NoteProperty -pass |
add-member -name Maximum -value $_.Maximum -membertype NoteProperty -pass
}
}
}
}
set-alias du Measure-Directory
that would allow me to issue:
du -r / |sort length -desc > mem.txt
But it would also allow me to issue:
du / -rec |? {$_.count -gt 10}|? {$_.LastWriteTime -gt "2007-01"} |sort length |ft Length,Count,Minimum,Maximum,Average,LastWriteTime ,fullname -auto -wrap
to find all directories with more than 10 files which have had files added since the beginning of the year, sort them by directory size in ascending order and report the directory size, number of files, smallest, largest and average file size along with the last modification time and the full directory path in a sensibly formatted table.
Because PSH passes along the directory object (onto which Measure-Directory has grafted a set of metrics) the pipeline can refine the search using any of the directory properties (timestamps, attributes, etc) and output as many or as few as desired.
PSH has its problems, not least is the fact that all those familiar utilities are no longer the best solutions when working in an object shell, yet in most cases replacements have not been written by MS or the community. It should be remembered that the only complete toolchain developed for PSH so far targeted MS Exchange.
It also has its (lets be kind) idiosyncrasies - compare the slow performance of gci *.exe versus the cmd.exe like performance of gci . *.exe - that make you wonder whether it is overly developed for manipulating system configurations at the expense of care and feeding of a directory hierarchy.
Despite some reservations though, I'd have to say it is an immensely logical shell and while I wouldn't go without the gnu toolchain at the moment, I am curious as to see how PSH compares when it (eventually) gets its own complete toolchain.
function global:Measure-Directory
{
param (
[string]$Path = (write-error `
"Usage: Measure-Directory [-Path] path [-Recurse]"),
[switch]$Recurse = $false
)
process {
gci $Path -Rec:$Recurse |
? { $_.psiscontainer } |
% { $dir=$_; gci $_.fullname |
? { -not $_.psiscontainer } |
measure -sum -average -minimum -maximum length |
% {
add-member -name Length -value $_.Sum -membertype NoteProperty -pass -inp $dir |
add-member -name Count -value $_.Count -membertype NoteProperty -pass |
add-member -name Average -value $_.Average -membertype NoteProperty -pass |
add-member -name Minimum -value $_.Minimum -membertype NoteProperty -pass |
add-member -name Maximum -value $_.Maximum -membertype NoteProperty -pass
}
}
}
}
set-alias du Measure-Directory
that would allow me to issue:
du -r / |sort length -desc > mem.txt
But it would also allow me to issue:
du / -rec |? {$_.count -gt 10}|? {$_.LastWriteTime -gt "2007-01"} |sort length |ft Length,Count,Minimum,Maximum,Average,LastWriteTim
to find all directories with more than 10 files which have had files added since the beginning of the year, sort them by directory size in ascending order and report the directory size, number of files, smallest, largest and average file size along with the last modification time and the full directory path in a sensibly formatted table.
Because PSH passes along the directory object (onto which Measure-Directory has grafted a set of metrics) the pipeline can refine the search using any of the directory properties (timestamps, attributes, etc) and output as many or as few as desired.
PSH has its problems, not least is the fact that all those familiar utilities are no longer the best solutions when working in an object shell, yet in most cases replacements have not been written by MS or the community. It should be remembered that the only complete toolchain developed for PSH so far targeted MS Exchange.
It also has its (lets be kind) idiosyncrasies - compare the slow performance of gci *.exe versus the cmd.exe like performance of gci . *.exe - that make you wonder whether it is overly developed for manipulating system configurations at the expense of care and feeding of a directory hierarchy.
Despite some reservations though, I'd have to say it is an immensely logical shell and while I wouldn't go without the gnu toolchain at the moment, I am curious as to see how PSH compares when it (eventually) gets its own complete toolchain.