fsutils

package
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 9 Imported by: 0

README

File System Utilities

  • FormatFileSize: Formats a file size given in bytes into a human-readable string with appropriate units (B, KB, MB, GB, TB).

  • FindFiles: Searches for files with the specified extension in the given root directory and returns a slice of matching file paths.

  • FindFilesWithFilter: Searches for files in the given root directory using a custom filter function, allowing flexible filtering based on file attributes (size, modification time, name, etc.).

  • GetDirectorySize: Calculates the total size (in bytes) of all files within the specified directory.

  • FilesIdentical: Compares two files byte by byte to determine if they are identical.

  • DirsIdentical: Compares two directories to determine if they are identical.

  • GetFileMetadata: Retrieves metadata for a specified file path. Returns a FileMetadata struct that can be marshaled to JSON.

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Index

Constants

View Source
const (
	KB ByteSize = 1024
	MB          = KB * 1024
	GB          = MB * 1024
	TB          = GB * 1024
)

Variables

This section is empty.

Functions

func DirsIdentical

func DirsIdentical(dir1, dir2 string) (bool, error)

DirsIdentical compares two directories to determine if they are identical It returns true if both directories contain the same files with identical content, and false otherwise

func FilesIdentical

func FilesIdentical(file1, file2 string) (bool, error)

FilesIdentical compares two files byte by byte to determine if they are identical

func FindFiles

func FindFiles(root string, extension string) ([]string, error)

FindFiles searches for files with the specified extension in the given root directory and returns a slice of matching file paths.

func FindFilesWithFilter added in v1.15.0

func FindFilesWithFilter(root string, filterFn func(fs.FileInfo) bool) ([]string, error)

FindFilesWithFilter searches for files in the given root directory using a custom filter function. The filterFn parameter allows you to define arbitrary logic for file selection (e.g., size, mod time, type). If filterFn is nil, all non-directory, non-symlink files are returned.

func FormatFileSize

func FormatFileSize(size int64) string

FormatFileSize formats a file size given in bytes into a human-readable string with appropriate units (B, KB, MB, GB, TB).

func GetDirectorySize

func GetDirectorySize(path string) (int64, error)

GetDirectorySize calculates the total size (in bytes) of all files within the specified directory

Types

type ByteSize

type ByteSize int64

type FileMetadata added in v1.3.0

type FileMetadata struct {
	Name    string      `json:"name"`
	Size    int64       `json:"size"`
	IsDir   bool        `json:"is_dir"`
	ModTime time.Time   `json:"mod_time"`
	Mode    os.FileMode `json:"mode"`
	Path    string      `json:"path"`
	Ext     string      `json:"ext"`
	Owner   string      `json:"owner"`
}

func GetFileMetadata added in v1.3.0

func GetFileMetadata(filePath string) (FileMetadata, error)

GetFileMetadata retrieves metadata for the specified file path. It returns a FileMetadata struct containing details about the file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL