files

package module
v1.10.8 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: BSD-3-Clause Imports: 14 Imported by: 13

README

files

Functions for dealing with files, paths and processes.

Function signatures

func Exists(path string) bool
func File(path string) bool
func Symlink(path string) bool
func FileOrSymlink(path string) bool
func Dir(path string) bool
func Which(executable string) string
func WhichCached(executable string) string
func PathHas(executable string) bool
func PathHasCached(executable string) bool
func BinDirectory(filename string) bool
func DataReadyOnStdin() bool
func Binary(filename string) bool
func FilterOutBinaryFiles(filenames []string) []string
func TimestampedFilename(filename string) string
func ShortPath(path string) string
func FileHas(path, what string) bool
func ReadString(filename string) string
func CanRead(filename string) bool
func Relative(path string) string
func Touch(filename string) error
func ExistsCached(path string) bool
func ClearCache()
func RemoveFile(path string) error
func DirectoryWithFiles(path string) (bool, error)
func Executable(path string) bool
func ExecutableCached(path string) bool
func Empty(path string) bool
func RealPath(path string) bool

Running commands

// Run a command without using a shell, only return nil if it went well
func Run(command string) error
// Run a command with /bin/sh and return the combined and trimmed output
func Shell(command string) (string, error)
// Run a command with /bin/bash (or bash from the PATH) and return the combined and trimmed output
func Bash(command string) (string, error)
// Run a command with /usr/bin/fish (or fish from the PATH) and return the combined and trimmed output
func Fish(command string) (string, error)

Examining and stopping processes

// Try to find the PID given a process name (similar to pgrep)
func GetPID(name string) (int64, error)
// Return true if a valid PID for the given process name is found in /proc (similar to pgrep)
func HasProcess(name string) bool
// Find and kill all processes that match the given name, returns the number of processes killed.
func Pkill(name string) (int, error)
// Resolve and returns the specified path (e.g., "exe", "cwd") for the process identified by pid.
func GetProcPath(pid int, suffix string) (string, error)

General info

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Binary = IsBinary
View Source
var BinaryAccurate = IsBinaryAccurate
View Source
var Dir = IsDir
View Source
var DirAndNotSymlink = IsDirAndNotSymlink
View Source
var Empty = IsEmpty

Empty checks if the given path is an empty file. Also returns false if something went wrong.

View Source
var Executable = IsExecutable
View Source
var ExecutableCached = IsExecutableCached

ExecutableCached checks if the given path exists and is an executable file, with cache support. Assumes that the filesystem permissions have not changed since the last check.

View Source
var File = IsFile
View Source
var FileOrSymlink = IsFileOrSymlink
View Source
var RealPath = IsRealPath

RealPath checks if the given path is the same if symlinks are not followed (like "pwd -P" / the "real" path) Also returns false if something went wrong.

View Source
var Relative = IsRelative
View Source
var Symlink = IsSymlink

Functions

func Bash added in v1.9.3

func Bash(bashCommand string) (string, error)

Bash runs the given command with /bin/bash, or "bash" in the $PATH, and returns the combined output.

func BinDirectory

func BinDirectory(filename string) bool

BinDirectory will check if the given filename is in one of these directories: /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin, ~/.bin, ~/bin, ~/.local/bin

func CanRead added in v1.1.0

func CanRead(filename string) bool

CanRead checks if 1 byte can actually be read from the given filename

func ClearCache added in v1.5.0

func ClearCache()

ClearCache clears all cache

func DataReadyOnStdin

func DataReadyOnStdin() bool

DataReadyOnStdin checks if data is ready on stdin

func DirectoryWithFiles added in v1.8.0

func DirectoryWithFiles(path string) (bool, error)

DirectoryWithFiles checks if the given path is a directory and contains at least one file

func Exists

func Exists(path string) bool

Exists checks if the given path exists

func ExistsCached added in v1.5.0

func ExistsCached(path string) bool

ExistsCached checks if the given path exists, using a cache for faster access. Assumes that the filesystem has not changed since the last check.

func FileHas

func FileHas(path, what string) bool

FileHas checks if the given file exists and contains the given string

func FilterOutBinaryFiles

func FilterOutBinaryFiles(filenames []string) []string

FilterOutBinaryFiles filters out files that are either binary or can not be read

func FilterOutBinaryFilesAccurate added in v1.10.5

func FilterOutBinaryFilesAccurate(filenames []string) []string

FilterOutBinaryFilesAccurate filters out files that are either binary or can not be read

func FindFile added in v1.10.1

func FindFile(substring string) ([]string, error)

FindFile searches for files that contain the given substring in their filename starting from the current directory. It returns all matches as absolute paths or an error if the search fails.

func Fish added in v1.9.3

func Fish(fishCommand string) (string, error)

Fish runs the given command with /usr/bin/fish, or "fish" in the $PATH, and returns the combined output.

func GetPID added in v1.10.3

func GetPID(name string) (int64, error)

getPID tries to find the PID, given a process name, similar to pgrep

func GetProcPath added in v1.10.3

func GetProcPath(pid int, suffix string) (string, error)

GetProcPath resolves and returns the specified path (e.g., "exe", "cwd") for the process identified by pid. It returns an error if the path cannot be resolved.

func HasProcess added in v1.10.3

func HasProcess(name string) bool

HasProcess returns true if a valid PID for the given process name is found in /proc, similar to how pgrep works

func HeaderSearch added in v1.10.1

func HeaderSearch(absCppFilename string, headerExtensions []string, maxTime time.Duration) (string, error)

HeaderSearch will search for a corresponding file, given a slice of extensions. This is useful for ie. finding a corresponding .h file for a .c file. The search starts in the current directory, then searches every parent directory in depth. TODO: Search sibling and parent directories named "include" first, then search the rest.

func IsBinary added in v1.4.0

func IsBinary(filename string) bool

IsBinary returns true if the given filename can be read and is a binary file. It checks if the given filename is a regular file first, to avoid hangs when reading named pipes.

func IsBinaryAccurate added in v1.10.5

func IsBinaryAccurate(filename string) bool

IsBinaryAccurate returns true if the given filename can be read and is a binary file. It checks if the given filename is a regular file first, to avoid hangs when reading named pipes.

func IsDir

func IsDir(path string) bool

IsDir checks if the given path exists and is a directory

func IsDirAndNotSymlink(path string) bool

IsDirAndNotSymlink checks if the given path exists and is a directory that is not a symlink

func IsEmpty added in v1.10.0

func IsEmpty(path string) bool

IsEmpty checks if the given path is an empty file. Also returns false if something went wrong.

func IsExecutable added in v1.9.0

func IsExecutable(path string) bool

IsExecutable checks if the given path exists and is executable

func IsExecutableCached added in v1.9.0

func IsExecutableCached(path string) bool

IsExecutableCached checks if the given path exists and is an executable file, with cache support. Assumes that the filesystem permissions have not changed since the last check.

func IsFile

func IsFile(path string) bool

IsFile checks if the given path exists and is a regular file

func IsFileOrSymlink(path string) bool

IsFileOrSymlink checks if the given path exists and is a regular file or a symbolic link

func IsRealPath added in v1.10.2

func IsRealPath(path string) bool

IsRealPath checks if the given path is the same if symlinks are not followed (like "pwd -P" / the "real" path) Also returns false if something went wrong.

func IsRelative added in v1.10.0

func IsRelative(path string) string

IsRelative takes an absolute or relative path and attempts to return it relative to the current directory. If there are errors, it simply returns the given path.

func IsSymlink(path string) bool

IsSymlink checks if the given path exists and is a symbolic link

func PathHas added in v1.7.0

func PathHas(executable string) bool

PathHas checks if the given executable is in $PATH

func PathHasCached added in v1.7.0

func PathHasCached(executable string) bool

PathHasCached checks if the given executable is in $PATH (looks in the cache first and then caches the result)

func Pkill added in v1.10.3

func Pkill(name string) (int, error)

Pkill tries to find and kill all processes that match the given name. Returns the number of processes killed, and an error, if anything went wrong.

func ReadString added in v1.1.0

func ReadString(filename string) string

ReadString returns the contents of the given filename as a string. Returns an empty string if there were errors.

func RemoveFile added in v1.5.0

func RemoveFile(path string) error

RemoveFile deletes a file, but it only returns an error if the file both exists and also could not be removed

func Run added in v1.9.2

func Run(commandString string) error

Run tries to run the given command, without using a shell. No output is returned, only an error, if something went wrong.

func Shell added in v1.9.3

func Shell(shellCommand string) (string, error)

Shell runs the given command with /bin/sh and returns the combined output

func ShortPath

func ShortPath(path string) string

ShortPath replaces the home directory with ~ in a given path. The given path is expected to contain the home directory path either 0 or 1 times, and if it contains the path to the home directory, it is expected to be at the start of the given string.

func TimestampedFilename

func TimestampedFilename(filename string) string

TimestampedFilename prefixes the given filename with a timestamp

func Touch added in v1.5.0

func Touch(filename string) error

Touch behaves like "touch" on the command line, and creates a file or updates the timestamp

func Which

func Which(executable string) string

Which tries to find the given executable name in the $PATH Returns an empty string if not found.

func WhichCached added in v1.6.0

func WhichCached(executable string) string

WhichCached tries to find the given executable name in the $PATH, using a cache for faster access. Assumes that the $PATH environment variable has not changed since the last check.

Types

This section is empty.

Jump to

Keyboard shortcuts

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