In the Unix/Linux world, I believe that .
and ..
exist "conceptually" in all directories on the file system.cs-a
is clearly present in any directory, but it seems a little useless to see .
or ..
.
Why does ls-a
display .
and ..
?
Related Questions
How to display all directories in a particular directory with the find command
The reason why it appears is that the directory actually exists as the hardlink named .
or ..
.Checking "inode" always matches .
when specified by directory name (as well as ..
).
#ls-id/root
3112961 / root /
# ls-id.
3112961 ./
# cd/tmp
# ls-id/tmp
1703937/tmp/
# ls-id.
1703937 ./
If you try to create a hard link with the ln
command, you are not allowed to create a directory if you run it with the command, and there is an option for superuser privileges, but if you actually try it, you will get an error (security reason?).
-d, -F, --directory
Allow superusers to create hard links to directories.
When creating a directory, it appears that .
and ..
hard links were created via callback.
(The link below is explained in ext2)
Note:
Parent/Current Directory - Linux Memorandum
.
How the starting file became a "hidden file"In the first place, ls
ignores .
and ..
as well as all files that start with .
are bugs that were created in the early stages of UNIX development.
It was in assembly then, but the code in question was equal to something like this:
if(name[0]=='.') continue;
This statement was a little shorter than what it should have been, which is
if(strcmp(name, ".")==0||strcmp(name, "...")==0) continue;
Many developers have taken over this bug and eventually developed the concept of "hidden files" and the habit of making configuration files hidden.
It's just a guess from here.
When the -a
option was added to ls
, the custom of starting the configuration file with .
and scattering it directly under the home directory was not established.
If so, the UNIX developer's intention to add -a
was to display shortcuts instead of displaying configuration files.
It may have been positioned as "an option to show that shortcuts .
. and ..
exist for sure." (Until now, the shortcuts are only .
and ..
but you might have thought about adding them enough to check.)
Maybe it's because it actually exists from the perspective of user programs such as ls, not conceptually.
For ls, if -A
is specified instead of -a
, it will intentionally exclude "."..." and display it in the program.
"It is not a direct answer to ""why"", but I will try to post it."
If you look at the ls command source, the list of files is calling
Portability Note:On some systems readdir may not return entries for .and..,even through these are always valid file names in any directory.
readdir
may not return .
or ..
depending on the implementation.Assuming that the person who wrote the ls command follows a policy that does not write implementation-dependent actions, it is the person who implemented readdir
who decided whether it was "a bit wasteful."
Now, I couldn't find out why readdir
would return them (or in some cases not).Maybe it's because it's more versatile as an API than it is.
© 2024 OneMinuteCode. All rights reserved.