Why do directory listings contain the current (.) and parent (..) directory?

后端 未结 8 1165
面向向阳花
面向向阳花 2020-12-11 03:11

Whenever I list the contents of a directory with a function like readdir, the returned file names also include \".\" and \"..\". I have the suspicion that these are just nor

相关标签:
8条回答
  • 2020-12-11 03:37

    They are reported because they are stored in the directory listing. That's the way unices have always worked.

    0 讨论(0)
  • 2020-12-11 03:38

    These are normal directories, they are "hard links" to the current directory and directory above. They are present in all directories (even at the root level, where .. is exactly the same as .).

    When using ls, you can filter out . and .. with ls -A (note the capital -A).

    When applying a command to all dot-files, but not . or .., I often use .??* which matches only dot-file with a name of three characters or more.

    touch .??*
    

    Note this pattern also excludes any other file that begins with dot and is only two characters long (e.g. .x) but those files are uncommon.

    When using programmatic file-listers like readdir() I do have to exclude . and .. manually. Since these two files are supposed to be first in the list returned by readdir() you can do this:

    @files = readdir(DIR);
    for (1..2) { shift @files; } # get rid of . and ..
    # go on with your business
    
    0 讨论(0)
提交回复
热议问题