package main import ( "fmt" "io/fs" "path/filepath" "sort" "syscall" "time" ) type fileStats struct { path string size int64 access time.Time } // Converts a given number of bytes into a more human-readable string. func humanBytes(size float64) string { units := []string{"B", "KiB", "MiB", "GiB", "TiB", "EiB", "ZiB"} i := 0 if size >= 1024 { for i < len(units) && size >= 1024 { i++ size = size / 1024 } } return fmt.Sprintf("%.01f %s", size, units[i]) } func main() { var fileList []fileStats filepath.Walk("/usr", func(path string, file fs.FileInfo, err error) error { if err != nil { // Will panic if we don't handle this. // Ignores files we can't access (permission denied). return nil } if file.Mode().IsRegular() { stat := file.Sys().(*syscall.Stat_t) atime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) fs := fileStats{path, file.Size(), atime} fileList = append(fileList, fs) } return nil }) sort.Slice(fileList, func(i, j int) bool { return fileList[i].size < fileList[j].size }) for _, file := range fileList { file.access.Format("%b %d %Y") fmt.Printf("%s %s %s\n", file.path, humanBytes(float64(file.size)), file.access.Format("02 Jan 2006")) } }