Next: , Previous: , Up: Extension Samples   [Contents][Index]


17.7.6 Reading Directories

The readdir extension adds an input parser for directories. The usage is as follows:

@load "readdir"

When this extension is in use, instead of skipping directories named on the command line (or with getline), they are read, with each entry returned as a record.

The record consists of three fields. The first two are the inode number and the file name, separated by a forward slash character. On systems where the directory entry contains the file type, the record has a third field (also separated by a slash), which is a single letter indicating the type of the file. The letters and their corresponding file types are shown in Table 17.4.

LetterFile type
bBlock device
cCharacter device
dDirectory
fRegular file
lSymbolic link
pNamed pipe (FIFO)
sSocket
uAnything else (unknown)

Table 17.4: File types returned by the readdir extension

On systems without the file type information, the third field is always ‘u’.

NOTE: On GNU/Linux systems, there are filesystems that don’t support the d_type entry (see the readdir(3) manual page), and so the file type is always ‘u’. You can use the filefuncs extension to call stat() in order to get correct type information.

Here is an example:

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