A modern, high-performance, zero-dependency Go filesystem utility library. Designed for production services, CLI applications, data pipelines, and distributed tools requiring reliable, secure, and cross-platform file operations.
FileLock) and auto-cleaning temporary directories (WithTempDir).DetectMimeType and determine text vs binary files (IsBinary, IsText).CompareFiles) and atomic string replacement (ReplaceInFile).SplitFile) and reassemble without data loss (JoinFiles).HeadLines), bottom N lines (TailLines) via reverse chunk seek, and rapid line counts (CountLines).EncryptFile) and decrypt (DecryptFile) files using authenticated AES-GCM cryptography.ReadJSON, WriteJSON) and CSV (ReadCSV, WriteCSV) persistence.DiskUsage) with total, used, free, available, and percentage.RotateFile) or size thresholds (RotateBySize).BatchRename) and recursive empty directory pruning (PruneEmptyDirs).SyncDir) with checksum-based or timestamp-based comparison, and orphan pruning (SyncOptions.Delete).WatchPath) with real-time CREATED, MODIFIED, and DELETED change events.IsReadable, IsWritable, IsExecutable, SetReadOnly).HumanBytes.go get github.com/Techwasti-Infotech/go-file-utils
package main
import (
"fmt"
"github.com/Techwasti-Infotech/go-file-utils"
)
func main() {
// Check existence
if fileutils.Exists("configs/settings.json") {
fmt.Println("Config file exists!")
}
// Ensure parent directory exists and touch a file
_ = fileutils.EnsureDir("logs/2026/03", 0o755)
_ = fileutils.Touch("logs/2026/03/app.log")
// Safe copy with timestamps preserved
_ = fileutils.CopyFile("logs/2026/03/app.log", "logs/backup.log", fileutils.CopyFileOptions{
Overwrite: true,
PreserveTimestamps: true,
})
// Retrieve clean file metadata
info, _ := fileutils.GetInfo("logs/backup.log")
fmt.Printf("File: %s, Size: %d bytes\n", info.Name, info.Size)
}
// Atomically write data without risking partial writes during crashes
data := []byte(`{"status": "ok"}`)
err := fileutils.WriteFileAtomic("configs/app.json", data, 0o644)
// Stream large log files line by line
err = fileutils.ReadLines("logs/large.log", func(line string) (bool, error) {
if strings.Contains(line, "FATAL") {
fmt.Println("Alert:", line)
return false, nil // Stop reading early
}
return true, nil // Continue reading
})
// Find all Go files larger than 1KB modified in the last 24 hours
files, err := fileutils.Find(".", fileutils.SearchFilter{
Extensions: []string{".go"},
MinSize: 1024,
ModifiedAfter: time.Now().Add(-24 * time.Hour),
SkipHidden: true,
OnlyFiles: true,
})
// Compress an entire directory tree
_ = fileutils.Zip("data/reports", "data/reports.zip")
// Extract with automatic Zip Slip security defense
_ = fileutils.Unzip("data/reports.zip", "data/extracted")
// Calculate streaming SHA256 checksum
hash, _ := fileutils.FileSHA256("data/reports.zip")
// Verify integrity against expected checksum
match, _ := fileutils.VerifyFileChecksum("data/reports.zip", fileutils.HashSHA256, hash)
// Hold exclusive atomic lock during critical section
err := fileutils.WithLock("database.sqlite", 5*time.Second, func() error {
// Perform isolated write
return nil
})
We provide a complete suite of runnable standalone example programs inside the examples/ directory:
DiskUsage), permissions inspection, regex batch rename, and empty folder pruning.Run any example directly with:
go run ./examples/01_basic_fs
Browse the interactive online documentation hosted on GitHub Pages: ๐ https://techwasti-infotech.github.io/go-file-utils/
Additional reference documents:
Run all unit tests with data race detector and coverage report:
go test -v -race -cover ./...
This library is distributed under the terms of the MIT License. See LICENSE for details.