@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"flag"
5
5
"fmt"
6
- "io/ioutil"
7
6
"log"
8
7
"os"
9
8
"path/filepath"
@@ -18,7 +17,9 @@ To find all files under a path recursively which are above a minimum size limit.
18
17
var (
19
18
pathsAboveRedSizeMBLimit = flag .Int ("minsize" , 500 , "minimum size to display in MBs" )
20
19
targetDir = flag .String ("path" , "/tmp" , "path to scan" )
20
+ listDir = flag .Bool ("dir" , false , "list big size directories also" )
21
21
version = flag .Bool ("version" , false , "show me version" )
22
+ debug = flag .Bool ("debug" , false , "show extra information" )
22
23
)
23
24
24
25
func readDir (dirname string ) ([]os.FileInfo , error ) {
@@ -52,39 +53,54 @@ func sizeInHuman(size int64) (float64, string, string) {
52
53
return gbSize , fmt .Sprintf ("about %.2f GBs" , gbSize ), "gb"
53
54
}
54
55
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
63
65
}
66
+ return size
67
+ }
64
68
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 ()
68
73
}
69
- }
74
+ return nil
75
+ })
76
+
77
+ size , _ , sizeType := sizeInHuman (dirSize )
78
+ fullDirSize = changeSizeToMB (size , sizeType )
70
79
return
71
80
}
72
81
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
+
73
95
func analyzeDir (pathToScan string ) {
74
96
sizeMBLimit := float64 (* pathsAboveRedSizeMBLimit )
75
97
if entries , err := readDir (pathToScan ); err == nil {
76
98
for _ , entry := range entries {
77
99
filepath := filepath .Join (pathToScan , entry .Name ())
78
100
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 )
88
104
continue
89
105
}
90
106
if sizeType == "b" || sizeType == "kb" {
@@ -97,7 +113,7 @@ func analyzeDir(pathToScan string) {
97
113
continue
98
114
}
99
115
}
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 )
101
117
}
102
118
} else {
103
119
log .Fatalf ("error reading dir %s" , pathToScan )
0 commit comments