Package fileutils provides useful, high-level file operations.
IsFile&IsDirchecks if file/directory existsCopyFilecopies a file from source to destination, preserving modeCopyDircopies all files recursively from the source to destination directoryMoveFilemoves a file, using atomic rename when possible with copy+delete fallbackListFilesreturns sorted slice of file paths in directoryTempFileNamereturns a new temporary file name using secure random generationSanitizePathcleans file pathTouchFilecreates an empty file or updates timestamps of existing oneChecksumcalculates file checksum using various hash algorithms (MD5, SHA1, SHA256, etc.)FileWatcherwatches files or directories for changesWatchRecursivewatches a directory recursively for changes
// Copy a file
err := fileutils.CopyFile("source.txt", "destination.txt")
if err != nil {
log.Fatalf("Failed to copy file: %v", err)
}
// Move a file
err = fileutils.MoveFile("source.txt", "destination.txt")
if err != nil {
log.Fatalf("Failed to move file: %v", err)
}
// Check if a file or directory exists
if fileutils.IsFile("file.txt") {
fmt.Println("File exists")
}
if fileutils.IsDir("directory") {
fmt.Println("Directory exists")
}
// Generate a temporary file name
tempName, err := fileutils.TempFileName("/tmp", "prefix-*.ext")
if err != nil {
log.Fatalf("Failed to generate temp file name: %v", err)
}
fmt.Println("Temp file:", tempName)// Calculate MD5 checksum
md5sum, err := fileutils.Checksum("path/to/file", enum.HashAlgMD5)
if err != nil {
log.Fatalf("Failed to calculate MD5: %v", err)
}
fmt.Printf("MD5: %s\n", md5sum)
// Calculate SHA256 checksum
sha256sum, err := fileutils.Checksum("path/to/file", enum.HashAlgSHA256)
if err != nil {
log.Fatalf("Failed to calculate SHA256: %v", err)
}
fmt.Printf("SHA256: %s\n", sha256sum)// Create a simple file watcher
watcher, err := fileutils.NewFileWatcher("/path/to/file", func(event FileEvent) {
fmt.Printf("Event: %s, Path: %s\n", event.Type, event.Path)
})
if err != nil {
log.Fatalf("Failed to create watcher: %v", err)
}
defer watcher.Close()
// Watch a directory recursively
watcher, err := fileutils.WatchRecursive("/path/to/dir", func(event FileEvent) {
fmt.Printf("Event: %s, Path: %s\n", event.Type, event.Path)
})
if err != nil {
log.Fatalf("Failed to create watcher: %v", err)
}
defer watcher.Close()
// Add another path to an existing watcher
err = watcher.AddPath("/path/to/another/file")
// Remove a path from the watcher
err = watcher.RemovePath("/path/to/file")go get -u github.com/go-pkgz/fileutils