FILEFUNCS(3am)
π FILEFUNCS π
okay bestie, gather round, because this little page comes straight out of the GNU Awk Extension Modules manual (section 3am, iykyk) and it's giving file functionality for your gawk scripts. let's go π«Ά
πΈ NAME πΈ
filefuncs β it provides some file related functionality to gawk. that's the whole vibe. β¨
π¦ SYNOPSIS π¦
@load "filefuncs"
result = chdir("/some/directory")
result = stat("/some/path", statdata [, follow])
flags = or(FTS_PHYSICAL, ...)
result = fts(pathlist, flags, filedata)
result = statvfs("/some/path", fsdata)
β¨ DESCRIPTION β¨
The filefuncs extension adds several functions that give you access to file-related facilities. she's useful, she's iconic, she's loaded with @load. π
π chdir() π
The chdir() function is a direct hook into the chdir(2) system call, and its entire job is changing the current directory.
It returns zero on success, or less than zero on error. In the error case, it updates ERRNO. no cap. π
π¦ stat() π¦
The stat() function is a hook into the stat(2) system call.
- returns zero on success
- returns less than zero on error, and in that case it updates
ERRNO
By default, it uses lstat(2). However, if you pass it a third argument, it uses stat(2) instead. slay.
In all cases, it clears the statdata array. When the call succeeds, stat() fills statdata with information retrieved from the filesystem, like this:
statdata["name"]β the name of the file, equal to the first argument passed tostat().statdata["dev"]β corresponds to thest_devfield in thestruct stat.statdata["ino"]β corresponds to thest_inofield in thestruct stat.statdata["mode"]β corresponds to thest_modefield in thestruct stat.statdata["nlink"]β corresponds to thest_nlinkfield in thestruct stat.statdata["uid"]β corresponds to thest_uidfield in thestruct stat.statdata["gid"]β corresponds to thest_gidfield in thestruct stat.statdata["size"]β corresponds to thest_sizefield in thestruct stat.statdata["atime"]β corresponds to thest_atimefield in thestruct stat.statdata["mtime"]β corresponds to thest_mtimefield in thestruct stat.statdata["ctime"]β corresponds to thest_ctimefield in thestruct stat.statdata["rdev"]β corresponds to thest_rdevfield in thestruct stat. π¨ this element is only present for device files.statdata["major"]β corresponds to thest_majorfield in thestruct stat. π¨ also only present for device files.statdata["minor"]β corresponds to thest_minorfield in thestruct stat. π¨ again, only present for device files.statdata["blksize"]β corresponds to thest_blksizefield in thestruct stat, if that field is present on your system. (it is present on all modern systems that we know of. π)statdata["pmode"]β a human-readable version of the mode value, such as printed byls(1). for example,"-rwxr-xr-x".statdata["linkval"]β if the named file is a symbolic link, this element will exist, and its value is the value of the symbolic link (where the symbolic link points to). πͺstatdata["type"]β the type of the file as a string. one of"file","blockdev","chardev","directory","socket","fifo","symlink","door", or"unknown". not all systems support all file types, so vibe check your platform first. π
π³ fts() π³
The fts() function hooks into the fts(3) set of routines for traversing file hierarchies. instead of handing you data about one file at a time in a stream, it fills in a multi-dimensional array with data about every file and directory it encounters in the requested hierarchies. it's giving main character energy, honestly.
The arguments are as follows:
pathlistβ an array of filenames. the element values are used; the index values are ignored. πflagsβ this should be the bitwise OR of one or more of the following predefined flag values. at least one ofFTS_LOGICALorFTS_PHYSICALmust be provided; otherwisefts()returns an error value and setsERRNO. β οΈFTS_LOGICALβ do a βlogicalβ file traversal, where the information returned for a symbolic link refers to the linked-to file, and not to the symbolic link itself. this flag is mutually exclusive withFTS_PHYSICAL.FTS_PHYSICALβ do a βphysicalβ file traversal, where the information returned for a symbolic link refers to the symbolic link itself. this flag is mutually exclusive withFTS_LOGICAL.FTS_NOCHDIRβ as a performance optimization, thefts(3)routines change directory as they traverse a file hierarchy. this flag disables that optimization.FTS_COMFOLLOWβ immediately follow a symbolic link named inpathlist, whether or notFTS_LOGICALis set.FTS_SEEDOTβ by default, thefts(3)routines do not return entries for.and... this option causes entries for..to also be included. (the AWK extension always includes an entry for., see below. β¨)FTS_XDEVβ during a traversal, do not cross onto a different mounted filesystem.FTS_SKIPβ when set, causes top level directories to not be descended into.
filedataβ thefiledataarray is first cleared. thenfts()creates an element infiledatafor every element inpathlist. the index is the name of the directory or file given inpathlist. the element for this index is itself an array. there are two cases. π- the path is a file. in this case, the array contains two or three elements:
"path"β the full path to this file, starting from the βrootβ that was given in thepathlistarray."stat"β this element is itself an array, containing the same information as provided by thestat()function described earlier for itsstatdataargument. π¨ the element may not be present ifstat(2)for the file failed."error"β if some kind of error was encountered, the array will also contain an element named"error", which is a string describing the error.
- the path is a directory. in this case, the array contains one element for each entry in the directory. if an entry is a file, that element is as for files, just described. if the entry is a directory, that element is (recursively) an array describing the subdirectory. if
FTS_SEEDOTwas provided in the flags, then there will also be an element named"..". this element will be an array containing the data as provided bystat().
- the path is a file. in this case, the array contains two or three elements:
In addition, there will be an element whose index is ".". this element is an array containing the same two or three elements as for a file: "path", "stat", and "error".
The fts() function returns 0 if there were no errors. otherwise it returns -1. π
πΎ statvfs() πΎ
The statvfs() function provides a hook into the statvfs(2) system call β on systems that supply this system call, anyway. she's exclusive. π
It returns zero on success, or less than zero on error. in the error case, it updates ERRNO.
When the call is successful, statvfs() fills the fsdata array with information retrieved about the filesystem, as follows:
fsdata["bsize"]β corresponds to thebsizemember in thestruct statvfs.fsdata["frsize"]β corresponds to thef_frsizemember in thestruct statvfs.fsdata["blocks"]β corresponds to thef_blocksmember in thestruct statvfs.fsdata["bfree"]β corresponds to thef_bfreemember in thestruct statvfs.fsdata["bavail"]β corresponds to thef_bavailmember in thestruct statvfs.fsdata["files"]β corresponds to thef_filesmember in thestruct statvfs.fsdata["ffree"]β corresponds to thef_ffreemember in thestruct statvfs.fsdata["favail"]β corresponds to thef_favailmember in thestruct statvfs.fsdata["fsid"]β corresponds to thef_fsidmember in thestruct statvfs. π¨ this member is not available on all systems.fsdata["flag"]β corresponds to thef_flagmember in thestruct statvfs.fsdata["namemax"]β corresponds to thef_namemaxmember in thestruct statvfs.
π NOTES π
The AWK fts() extension does not exactly mimic the interface of the fts(3) routines. instead it chose to provide an interface based on associative arrays, which should be more comfortable to use from an AWK program. this includes the lack of a comparison function, since gawk already provides powerful array sorting facilities. π
and while an fts_read()-like interface could have been provided, that felt less natural than simply creating a multi-dimensional array to represent the file hierarchy and its information. the vibes said: array. β¨
Nothing prevents AWK code from changing the predefined FTS_xx values β but doing so may cause strange results when the changed values are passed to fts(). π¨ so like, don't.
π BUGS π
- There are many more file-related functions for which AWK interfaces would be desirable. so many. π
- It's not clear why I thought adding
FTS_SKIPwas a good idea.
π EXAMPLE π
See test/fts.awk in the gawk distribution for an example. go look, it's cute. π«Ά
π SEE ALSO π
- GAWK: Effective AWK Programming, and the man pages for the other extensions shipped with gawk.
chdir(2),fts(3),stat(2),statvfs(2).
π AUTHOR π
Arnold Robbins, arnold@skeeve.com.
π COPYING PERMISSIONS π
Copyright Β© 2012, 2013, 2018, 2019, 2026, Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual page under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual page into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.
and that's the whole page, bestie!! πβ¨π brought to you by the Free Software Foundation, dated Feb 25 2026, living in section FILEFUNCS(3am). read it, love it, @load it. mwah. π
π¦πΈ
π¦ where this page comes from
- arch rolling: gawk 5.4.1-1 Β· GPL-3.0-or-later
- nixos 26.05: gawk-interactive 5.4.1
- nixpkgs unstable: gawk 5.4.1
- nixpkgs unstable: gawk-interactive 5.4.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms