What characters are illegal in a Unix filename?

What characters are illegal in a Unix file name? Only two, the nul character or zero byte, and the slash character /. If you want to test it on your system, the following C program demonstrates:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>

int main ()
{
    int c;
    char filename[0x100];

    for (c = 0; c < 0x100; c++) {
        int status;
        snprintf (filename, 0xff, "%c-test\0", c);
        status = creat (filename, 0);
        if (status == -1) {
            warn ("creat: %d", c);
        }
        else {
            status = unlink (filename);
            if (status != 0) {
                warn ("unlink: %d", c);
            }
        }
    }
    return 0;
}

A single dot . or double dot .. are illegal file names too, but you can create a file called ...; try

touch ...

if you don't believe it.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer