Using du.exe (Sysinternals) is it possible to show folders above a certain size?

42,787

Solution 1

I'm providing a new variant on the previous du / powershell combos, just because neither of them worked for me with du v1.61, and others may face the same issue. I ran into a few problems with the previous variants:

  1. du's -nobanner switch needs to be included (and wasn't) so that ConvertFrom-Csv doesn't choke on the banner at the top (for me, manifesting as mysterious invisible output)
  2. Even then, powershell would return e.g. Cannot convert value "40822284152" to type "System.Int32". Error: "Value was either too large or too small for an Int32." for some lines; presumably very large directories.
  3. Lastly, the output would include very small directories, well below 1GB, despite the presence of Where-Object { [int]$_.DirectorySize -gt 1048576 }

I'm not a powershell expert, but I've managed to adapt the previous two variants to provide a working solution:

du -nobanner -c -l 3 \ | ConvertFrom-Csv | select Path,@{Name="DirectorySize";expression={$_.DirectorySize / 1GB }} | Where-Object { $_.DirectorySize -gt 1 } | Sort-Object { $_.DirectorySize } -descending

And just for giggles, here's a less flexible / less precise solution using du and findstr with a regular expression:

du -l 3 \ | findstr /R /C:",[0-9][0-9][0-9],[0-9][0-9][0-9]  "

This takes advantage of the pattern that directories over 1GB will be printed in. Specifically there will be an extra leading comma before the three digits, and then another comma followed by three more digits. The trailing two spaces are required to filter out the totals that are printed at the bottom. Note: this solution doesn't sort by directory size as with the powershell solution.

Solution 2

As the other answer says, you can't do it with du.exe alone. PowerShell to the rescue!

.\du.exe -c -l 3 C:\ | ConvertFrom-Csv -Header Size,Path | Where-Object { [int]$_.Size -gt 1048576 } | Sort-Object { [int]$_.Size } -descending

Explanation

Breaking that long command down into the individual bits that are each piped into the next one:

.\du.exe -c -l 3 C:\

This is basically what you started with, except the -c parameter tells du to format the output as CSV.

ConvertFrom-Csv -Header Size,Path

This takes the CSV output from du and converts it into a PowerShell hashtable. Since du doesn't provide a header with column names, that has to be done manually.

Where-Object { [int]$_.Size -gt 1048576 }

This filters the data, returning only those rows where the size is greater than 1 GB (du returns sizes in KB, and 1 GB = 1048576 KB). Note the [int] part, to let PowerShell know that it's dealing with numeric data.

Sort-Object { [int]$_.Size } -descending

This sorts the data by size, in descending order (again specifying that the data to sort by is numeric). This is optional, of course.

Solution 3

In the latest du.exe version appending a header with Powershell ConvertFrom-Csv doesn't work out of the box, because du.exe appends it's own header in the listing. What we need to do is select the existing header with select command.

du.exe -c -l 3 C:\ | ConvertFrom-Csv | select Path,DirectorySize | Where-Object { [int]$_.DirectorySize -gt 1048576 }

Solution 4

An up to date answer for cmd.exe if du is in System32:

du -l 3 * | findstr /R /C:",[0-9][0-9][0-9],[0-9][0-9][0-9]  " | sort

Will take more time but will give you what you are looking for I like 1 depth.

du -l 1 * | findstr /R /C:",[0-9][0-9][0-9],[0-9][0-9][0-9]  " | sort
  • STAR - Will retrieve the current dir

You can combine this with an earlier answer from jimadine in order to set a doskey command.

DOSKEY sz=du -l $1 * | findstr /R /C:",[0-9][0-9][0-9],[0-9][0-9][0-9]  " | sort

Usage is

sz <search depth>

Solution 5

That options isn't available with DU 1.4

Usage: du [-c] [-l (levels) | -n | -v] [-u] [-q] (directory)

-c Print output as CSV.

-l Specify subdirectory depth of information (default is all levels).

-n Do not recurse.

-q Quiet (no banner).

-u Count each instance of a hardlinked file.

-v Show size (in KB) of intermediate directories.

So just using DU can't limit the output to something of a specific size.

Share:
42,787

Related videos on Youtube

Iain
Author by

Iain

Iain is a software developer from Wokingham, UK. Has a first-class degree in Computing Science from the University of Manchester. Currently works for a leading financial institution in London on C# and Java projects.

Updated on September 18, 2022

Comments

  • Iain
    Iain over 1 year

    du.exe lets you recursively identify folders that take up a large amount of space. For example, the following will show you the size of all the folders from c:\ 3-levels deep:

    du.exe -l 3 c:\
    

    How can I filter this output to only show me the folders above 1GB?

    Here's an extract of the output from du.exe for reference:

    Du v1.4 - report directory disk usage
    Copyright (C) 2005-2011 Mark Russinovich
    Sysinternals - www.sysinternals.com
    
       6,344,864  c:\Windows\winsxs
      18,268,671  c:\Windows
     483,343,308  c:\
    Files:        412125
    Directories:  42072
    Size:         494,943,548,281 bytes
    Size on disk: 487,560,269,896 bytes
    
  • Iain
    Iain about 12 years
    Happy to combine with other tools available on the command line.
  • Bon Gart
    Bon Gart about 12 years
    Then there you go. Indrek has the answer you want. I gave it a +1.