[Golang] List Directory Size in Ascending Order (Excluding Sub-Directories)


List folder size in ascending orfer, excluding sub-folders, in Go. We use ioutil.ReadDir to read information of files/directories in current directory, excluding sub-directories. If you want to include sub-directories, use filepath.Walk instead. After calculating directory size, use sort.Slice to sort the size in ascending order.

package main

import (
      "fmt"
      "io/ioutil"
      "os"
      "sort"
)

type nameSize struct {
      name string
      size int64
}

func calculateDirSize() (dirsize int64) {
      files, err := ioutil.ReadDir(".")
      if err != nil {
              fmt.Println(err)
              return
      }

      for _, file := range files {
              if file.Mode().IsRegular() {
                      dirsize += file.Size()
              }
      }
      return
}

func main() {
      // read all files/directories in current directory
      dirs, err := ioutil.ReadDir(".")
      if err != nil {
              fmt.Println(err)
              return
      }

      var nameSizeArray []nameSize
      for _, dir := range dirs {
              // if it is not dir, continue to next dir
              if !dir.IsDir() {
                      continue
              }

              err = os.Chdir(dir.Name())
              if err != nil {
                      fmt.Println(err)
                      return
              }

              size := calculateDirSize()
              ns := nameSize{dir.Name(), size}
              nameSizeArray = append(nameSizeArray, ns)

              err = os.Chdir("..")
              if err != nil {
                      fmt.Println(err)
                      return
              }
      }

      // sort all directories in current directory according to size
      sort.Slice(nameSizeArray, func(i, j int) bool {
              return nameSizeArray[i].size < nameSizeArray[j].size
      })

      // print sorted result
      for _, ns := range nameSizeArray {
              fmt.Println(ns)
      }
}

If you want to use shell command to list directory size, see [2].

Tested on: Ubuntu Linux 17.10, Go 1.10.1


References:

[1][Golang] Calculate Directory Size Excluding Sub-Directories
[2][Bash] List Directory Size in Descending and Ascending Order