@@ -3,7 +3,6 @@ package main
33import (
44 "flag"
55 "fmt"
6- "io/ioutil"
76 "log"
87 "os"
98 "path/filepath"
@@ -18,7 +17,9 @@ To find all files under a path recursively which are above a minimum size limit.
1817var (
1918 pathsAboveRedSizeMBLimit = flag .Int ("minsize" , 500 , "minimum size to display in MBs" )
2019 targetDir = flag .String ("path" , "/tmp" , "path to scan" )
20+ listDir = flag .Bool ("dir" , false , "list big size directories also" )
2121 version = flag .Bool ("version" , false , "show me version" )
22+ debug = flag .Bool ("debug" , false , "show extra information" )
2223)
2324
2425func readDir (dirname string ) ([]os.FileInfo , error ) {
@@ -52,39 +53,54 @@ func sizeInHuman(size int64) (float64, string, string) {
5253 return gbSize , fmt .Sprintf ("about %.2f GBs" , gbSize ), "gb"
5354}
5455
55- func calculateDirSize (dirpath string ) (dirsize int64 , err error ) {
56- err = os .Chdir (dirpath )
57- if err != nil {
58- return
59- }
60- files , err := ioutil .ReadDir ("." )
61- if err != nil {
62- return
56+ func changeSizeToMB (size float64 , sizeType string ) float64 {
57+ if sizeType == "b" {
58+ return (size / (1024 * 1024 ))
59+ } else if sizeType == "kb" {
60+ return (size / (1024 ))
61+ } else if sizeType == "mb" {
62+ return size
63+ } else if sizeType == "gb" {
64+ return size * 1024
6365 }
66+ return size
67+ }
6468
65- for _ , file := range files {
66- if file .Mode ().IsRegular () {
67- dirsize += file .Size ()
69+ func calculateDirSize (dirPath string ) (dirSize int64 , fullDirSize float64 , err error ) {
70+ err = filepath .Walk (dirPath , func (path string , info os.FileInfo , err error ) error {
71+ if info .Mode ().IsRegular () {
72+ dirSize += info .Size ()
6873 }
69- }
74+ return nil
75+ })
76+
77+ size , _ , sizeType := sizeInHuman (dirSize )
78+ fullDirSize = changeSizeToMB (size , sizeType )
7079 return
7180}
7281
82+ func analyzeSubDir (dirPath string , dirName string , sizeMBLimit float64 ) {
83+ _ , dirSize , err := calculateDirSize (dirPath )
84+ if err != nil && * debug == true {
85+ log .Printf ("not able to get dir size for: %s" , dirPath )
86+ return
87+ } else if float64 (dirSize ) >= sizeMBLimit {
88+ if * listDir {
89+ fmt .Printf ("\n 📁Path: %s\n \t Name: %s\n \t Size: %.2f MB\n " , dirPath , dirName , dirSize )
90+ }
91+ analyzeDir (dirPath )
92+ }
93+ }
94+
7395func analyzeDir (pathToScan string ) {
7496 sizeMBLimit := float64 (* pathsAboveRedSizeMBLimit )
7597 if entries , err := readDir (pathToScan ); err == nil {
7698 for _ , entry := range entries {
7799 filepath := filepath .Join (pathToScan , entry .Name ())
78100 size , humanSize , sizeType := sizeInHuman (entry .Size ())
79- isDir := entry .IsDir ()
80- if isDir {
81- dirsize , err := calculateDirSize (filepath )
82- if err != nil {
83- log .Fatalf ("not able to get dir size for: %s" , filepath )
84- continue
85- } else if float64 (dirsize ) >= sizeMBLimit {
86- analyzeDir (filepath )
87- }
101+
102+ if entry .IsDir () {
103+ analyzeSubDir (filepath , entry .Name (), sizeMBLimit )
88104 continue
89105 }
90106 if sizeType == "b" || sizeType == "kb" {
@@ -97,7 +113,7 @@ func analyzeDir(pathToScan string) {
97113 continue
98114 }
99115 }
100- fmt .Printf ("\n 📁 Path: %s\n \t Name: %s\n \t Size: %s\n " , filepath , entry .Name (), humanSize )
116+ fmt .Printf ("\n 📄 Path: %s\n \t Name: %s\n \t Size: %s\n " , filepath , entry .Name (), humanSize )
101117 }
102118 } else {
103119 log .Fatalf ("error reading dir %s" , pathToScan )
0 commit comments