Search code examples
linuxfile-type

What are the possible values that can be returned by stat "$somepath" --format '%F'


By trial and error I have found that the following values can be returned by stat "$somepath" --format '%F':

  1. regular file

  2. directory

  3. symbolic link

I wish to use stat to perform recursive directory contents operations and I wish to ensure that I can correctly identify every possible file type. Hardlinks appear to behave as I assumed and stat returns the target type.

This information does not appear in my man page; it only says: %F file type

(and it warns that stat may vary between shells; it would be nice if this particular usage were portable but I'm primarily focused on bash)

Is this list complete and is stat allowed to change what text it produces in this case depending on the execution environment or version?

NB: I am not knowledgeable about linux, filesystems or commands so I may have missed something obvious here


Solution

  • Beware: the output of stat is convention based and different versions of stat (from different authors and historical usage) as well as different releases of the same stat can and do return different outputs.

    The file types that I can remember actually seeing when working with files (YMMV) are:

    • regular.* file (regular empty file is an entertainingly frequently overlooked distinction)
    • directory
    • symbolic link
    • fifo
    • .* special file
    • socket
    • semaphore
    • port
    • .* memory object

    In general if you care about what the files are it is much better to use a file handling library in a programming language of your choice which will have had some development to be able to accurately identify a file; using command lines and scripts is not a very robust way of doing things

    Here are some links to mainly the gnu implementation of stat if you want to dig deeper:

    GNU Findutils documentation

    Old GNU Findutils documentation

    Common file types

    The man page is fairly uninformative as you suggest

    GNU stat related source code

    GNU file type related source code