Last active
March 1, 2018 00:00
-
-
Save akerouanton/29d386f9962b17be7ec26ade69b43022 to your computer and use it in GitHub Desktop.
ls and mv (partially) rewritten in go (without perf issues on directory with hundreds of thousands of files)
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" | |
| "log" | |
| "fmt" | |
| ) | |
| func main() { | |
| dir, err := os.Getwd() | |
| if err != nil { | |
| log.Fatalf(err.Error()) | |
| } | |
| if len(os.Args) > 1 { | |
| dir = os.Args[1] | |
| } | |
| f, err := os.Open(dir) | |
| if err != nil { | |
| log.Fatalf("Failed to open %s (err %d)", dir, err) | |
| } | |
| files, err := f.Readdirnames(-1) | |
| if err != nil { | |
| log.Fatalf("Failed to read entries in %s (err %d)", dir, err) | |
| } | |
| for _, file := range files { | |
| fmt.Println(file) | |
| } | |
| } |
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" | |
| "log" | |
| "fmt" | |
| ) | |
| func main() { | |
| if len(os.Args) != 3 { | |
| log.Fatalf("Usage: %s <src> <dst>", os.Args[0]) | |
| } | |
| src := os.Args[1] | |
| dst := os.Args[2] | |
| f, err := os.Open(src) | |
| if err != nil { | |
| log.Fatalf("Failed to open %s (err %d)", src, err) | |
| } | |
| files, err := f.Readdirnames(-1) | |
| if err != nil { | |
| log.Fatalf("Failed to read entries in %s (err %d)", src, err) | |
| } | |
| for _, file := range files { | |
| os.Rename(fmt.Sprintf("%s/%s", src, file), fmt.Sprintf("%s/%s", dst, file)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment