# Packages
No description provided by the author
# README
fileutils

Package fileutils
provides useful, high-level file operations.
Details
IsFile
&IsDir
checks if file/directory existsCopyFile
copies a file from source to destination, preserving modeCopyDir
copies all files recursively from the source to destination directoryMoveFile
moves a file, using atomic rename when possible with copy+delete fallbackListFiles
returns sorted slice of file paths in directoryTempFileName
returns a new temporary file name using secure random generationSanitizePath
cleans file pathTouchFile
creates an empty file or updates timestamps of existing oneChecksum
calculates file checksum using various hash algorithms (MD5, SHA1, SHA256, etc.)FileWatcher
watches files or directories for changesWatchRecursive
watches a directory recursively for changes
Usage Examples
File Operations
// 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)
File Checksum
// 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)
File Watcher
// 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")
Install and update
go get -u github.com/go-pkgz/fileutils