代码之家  ›  专栏  ›  技术社区  ›  Ralph Shillington

如何查找目录结构中最大的10个文件

  •  27
  • Ralph Shillington  · 技术社区  · 15 年前

    6 回复  |  直到 7 年前
        1
  •  52
  •   Luk    6 年前

    试试这个脚本

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

    “过滤器块” ?{ -not $_.PSIsContainer } “用于筛选出目录。sort命令将按大小降序对所有剩余条目进行排序。select子句将只允许前10到,因此它将是最大的10。

        2
  •  39
  •   Keith Hill    15 年前

    这可以简化一点,因为目录没有长度:

    gci . -r | sort Length -desc | select fullname -f 10
    
        3
  •  6
  •   OutOfThisPlanet EBGreen    4 年前

    blogged about a similar problem 在这里,我想找到一个目录和所有子目录中最大的文件(例如整个C:驱动器),并以易于理解的格式(GB、MB和KB)列出它们的大小。下面是我使用的PowerShell函数,它在一个漂亮的网格视图中列出了按大小排序的所有文件:

    Get-ChildItem -Path 'C:\somefolder' -Recurse -Force -File |
        Select-Object -Property FullName `
            ,@{Name='SizeGB';Expression={$_.Length / 1GB}} `
            ,@{Name='SizeMB';Expression={$_.Length / 1MB}} `
            ,@{Name='SizeKB';Expression={$_.Length / 1KB}} |
        Sort-Object { $_.SizeKB } -Descending |
        Out-GridView
    

    输出到PowerShell GridView很好,因为它允许您轻松筛选结果并滚动浏览。这是更快的PowerShell v3版本,但博客文章也显示了与PowerShell v2兼容的较慢版本。

    -First 10 选择对象调用的参数。

        4
  •  1
  •   Nick Xu    10 年前

    如果您关心结果集的一些格式设置,这里有两个输出外观更好的函数:

    #Function to get the largest N files on a specific computer's drive
    Function Get-LargestFilesOnDrive
    {
    Param([String]$ComputerName = $env:COMPUTERNAME,[Char]$Drive = 'C', [Int]$Top = 10)
    Get-ChildItem -Path \\$ComputerName\$Drive$ -Recurse | Select-Object Name, @{Label='SizeMB'; Expression={"{0:N0}" -f ($_.Length/1MB)}} , DirectoryName,  Length | Sort-Object Length -Descending  | Select-Object Name, DirectoryName, SizeMB -First $Top | Format-Table -AutoSize -Wrap    
    }
    
    #Function to get the largest N files on a specific UNC path and its sub-paths
    Function Get-LargestFilesOnPath
    {
        Param([String]$Path = '.\', [Int]$Top = 10)
        Get-ChildItem -Path $Path -Recurse | Select-Object Name, @{Label='SizeMB'; Expression={"{0:N0}" -f ($_.Length/1MB)}} , DirectoryName,  Length | Sort-Object Length -Descending  | Select-Object Name, DirectoryName, SizeMB -First $Top | Format-Table -AutoSize -Wrap
    }
    
        5
  •  0
  •   Carsten    6 年前

    以下是我所知道的完成此任务的最快方法:

    $folder  = "$env:windir" # forder to be scanned
    $minSize = 1MB
    
    $stopwatch =  [diagnostics.stopwatch]::StartNew()
    
    $ErrorActionPreference = "silentlyContinue"
    $list = New-Object 'Collections.ArrayList'
    $files = &robocopy /l "$folder" /s \\localhost\C$\nul /bytes /njh /njs /np /nc /fp /ndl /min:$minSize
    foreach($file in $files) {
        $data = $file.split("`t")
        $null = $list.add([tuple]::create([uint64]$data[3], $data[4]))
    }
    
    $list.sort() # sort by size ascending
    $result = $list.GetRange($list.count-10,10) 
    $result | select item2, item1 | sort item1 -Descending
    
    $stopwatch.Stop()
    $t = $stopwatch.Elapsed.TotalSeconds
    "done in $t sec."
    
        6
  •  -2
  •   Fouad Djebbar    6 年前

    将前10个最大文件放入本地目录的基本方法是使用h进行人类可读,S按大小排序文件:

    ls -Sh -l |head -n 10
    

    du -ha /home/directory |sort -n -r |head -n 10