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
They are reported because they are stored in the directory listing. That's the way unices have always worked.
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