Skip to content

Instantly share code, notes, and snippets.

@srishanbhattarai
Last active March 29, 2018 07:30
Show Gist options
  • Save srishanbhattarai/8e745b1a7909cfd2433803f0fee5c78d to your computer and use it in GitHub Desktop.
Save srishanbhattarai/8e745b1a7909cfd2433803f0fee5c78d to your computer and use it in GitHub Desktop.
Prune files matching a pattern in the current working dir
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/fatih/color"
)
func start() {
verbose := flag.Bool("v", false, "verbose output")
flag.Parse()
// stats
var allFiles uint64
var removedFiles uint64
now := time.Now()
var wg sync.WaitGroup
f, _ := ioutil.ReadDir("./")
fmt.Println()
for _, file := range f {
n := file.Name()
if strings.HasPrefix(n, "mssql_") {
wg.Add(1)
go func(filename string) {
defer func() {
wg.Done()
atomic.AddUint64(&allFiles, 1)
}()
err := os.Remove(filename)
if err == nil {
atomic.AddUint64(&removedFiles, 1)
if *verbose {
fmt.Printf("%s: %s\n", color.RedString("Deleted"), color.CyanString(filename))
}
}
}(file.Name())
}
}
wg.Wait()
elapsed := time.Since(now).Round(time.Millisecond).String()
fmt.Println("Time elapsed: ", color.GreenString(elapsed))
fmt.Println("Files matched: ", color.GreenString(strconv.FormatUint(allFiles, 10)))
fmt.Println("Files removed: ", color.GreenString(strconv.FormatUint(removedFiles, 10)))
}
func main() {
start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment