Using Epoch to Create a Unique Path File Name

Using the UNIX Epoch is a far more useful way to uniquely name a file whether it be new or a copy of an existing file. This epochify utility is an elegant way to do it in Go, but it can also be easily done with nothing but Bash as well.

💢 I have always hated (1) and (2) and (copy) when the designers of such things threw away an amazing opportunity to include the exact second the new file name was created.

package main

import (
    "fmt"
    "strings"
  "os"
    "time"
)

func Epochify(path string) string {
    i := strings.LastIndex(path, ".")
    if i >= 0 {
        return fmt.Sprintf("%v-%v%v", path[0:i], time.Now().Unix(), path[i:])
    }
    return fmt.Sprintf("%v-%v", path, time.Now().Unix())
}

func TextArgs() string { return strings.Join(os.Args[1:], " ") } 

func main() {
  fmt.Println(Epochify(TextArgs()))
}

Here are some ways you could use it.

epochify some file
epochify "somefile.md"
epochify "/a/path/to/somefile.md"
cp somefile.md $(epochify somefile.md)
ls some*
some file-1558288794
somefile-1558288794.md
/a/path/to/somefile-1558288794.md
somefile.md somefile-1558288794.md