/* Copyright (C) 2014 by Alexandru Cojocaru */ /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "fmt" "log" "os" ) func du(currentPath string, info os.FileInfo) int64 { size := info.Size() if !info.IsDir() { return size } dir, err := os.Open(currentPath) if err != nil { log.Print(err) return size } defer dir.Close() fis, err := dir.Readdir(-1) if err != nil { log.Fatal(err) } for _, fi := range fis { if fi.Name() == "." || fi.Name() == ".." { continue } size += du(currentPath+"/"+fi.Name(), fi) } fmt.Printf("%d %s\n", size, currentPath) return size } func main() { log.SetFlags(log.Lshortfile) dir := os.Args[1] info, err := os.Lstat(dir) if err != nil { log.Fatal(err) } du(dir, info) }