READDIR(3am)

πŸ’– READDIR(3am) β€” the GNU Awk Extension Modules glow-up πŸ’–

okay bestie, pull up a chair because we're talking about readdir, the directory input parser for gawk, from the GNU Awk Extension Modules manual. it's giving "your awk can read folders now" and honestly? iconic. πŸŽ€


✨ NAME ✨

readdir β€” directory input parser for gawk. that's it, that's the whole job, and she does it flawlessly. πŸ’…


🌸 SYNOPSIS 🌸

@load "readdir"

BEGIN { PROCINFO["readdir_override"] = any_value }  # optional

literally two lines, one of them optional. low maintenance queen. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so the readdir extension adds an input parser for directories. yes, directories. the thing awk normally walks past like it's not even there. πŸ™ˆ

when this extension is in use, instead of skipping directories named on the command line (or with getline), they get read, and every entry comes back to you as a record. one entry, one record. no cap.

each record is made of three fields separated by forward slash characters. let's break it down because the details matter, fr fr:

  1. the first field is the inode number
  2. the second field is the filename
  3. the third field is a single letter telling you the type of the file:
  • f β€” for file
  • d β€” for directory
  • b β€” for a block device
  • c β€” for a character device
  • p β€” for a FIFO
  • l β€” for a symbolic link
  • s β€” for a socket

on systems where the directory entry already contains the file type, that third field just gets filled in straight from that info. slay. ✨

on systems without the file type information, the extension falls back to calling stat(2) so it can still hand you the info. so like, the third field should never be u (unknown). never. not once. πŸ’€


ok now the important part, bestie, don't skip this one πŸ‘‡

normally, when you're reading directories, you should set FS equal to "/". however β€” and this is the fun part β€” you may instead choose to create PROCINFO["readdir_override"] (with any value). if that element exists when the directory is opened, then the extension automatically sets the fields in each record for you. set it and forget it, main character energy. 🫢

by default, if a directory cannot be opened (due to permission problems, for example), gawk will exit. 😭 as with regular files, this situation can be handled using a BEGINFILE rule that checks ERRNO and prints an error or otherwise handles the problem. so you can catch it, you just have to actually do it.


πŸŽ€ EXAMPLE πŸŽ€

@load "readdir"
...
BEGIN { FS = "/" }
{ print "file name is", $2 }

see how clean that is? $2 is the filename, FS = "/" splits the record up. the way that just works. πŸ”₯


🌈 SEE ALSO 🌈

go read the whole family, they're all icons. πŸ‘‘


πŸ’œ AUTHOR πŸ’œ

Arnold Robbins, arnold@skeeve.com. thank you for your service, king. 🫑


⭐ COPYING PERMISSIONS ⭐

Copyright Β© 2012, 2013, 2018, 2019, 2023, 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.


ngl that's a lot of legal text but we respect it, the lawyers ate and left no crumbs πŸ’…

and that's the whole page, bestie! brought to you by the Free Software Foundation, dated Feb 25 2026, from the READDIR(3am) page. go read a directory, you've earned it. πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from