Created
July 29, 2017 15:27
-
-
Save m-x-k/54981590d0ff21b780473d9ca7d972f8 to your computer and use it in GitHub Desktop.
Output file size and check if a duplicate exists for all files in a specific directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "os" | |
| "fmt" | |
| "log" | |
| "crypto/sha512" | |
| "io/ioutil" | |
| "path/filepath" | |
| ) | |
| var files = make(map[[sha512.Size]byte]string) | |
| func printFile(path string, info os.FileInfo, err error) error { | |
| if err != nil { | |
| log.Print(err) | |
| return nil | |
| } | |
| du(path, info) | |
| checkDuplicate(path, info) | |
| fmt.Println("") | |
| return nil | |
| } | |
| func du(path string, info os.FileInfo) { | |
| size := info.Size() | |
| if !info.IsDir() { | |
| fmt.Printf("%s: %d Bytes\n", path, size) | |
| } | |
| } | |
| func checkDuplicate(path string, info os.FileInfo) error { | |
| if info.IsDir() { | |
| return nil | |
| } | |
| data, err := ioutil.ReadFile(path) | |
| if err != nil { | |
| log.Print(err) | |
| return nil | |
| } | |
| digest := sha512.Sum512(data) | |
| if v, ok := files[digest]; ok { | |
| fmt.Printf("%q is a duplicate of %q\n", path, v) | |
| } else { | |
| files[digest] = path | |
| } | |
| return nil | |
| } | |
| func main() { | |
| log.SetFlags(log.Lshortfile) | |
| dir := os.Args[1] | |
| err := filepath.Walk(dir, printFile) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment