Why does `ls-a` display `.` or `..`?

Asked 2 years ago, Updated 2 years ago, 102 views

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

linux unix

2022-09-30 21:30

4 Answers

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


2022-09-30 21:30

.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;

--Alesson in shortcuts.

Many developers have taken over this bug and eventually developed the concept of "hidden files" and the habit of making configuration files hidden.

Guessing Developer Intent

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.)


2022-09-30 21:30

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.


2022-09-30 21:30

"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 .The document (p.404) in gnu says:

struct dirent*readdir (DIR*dirstream)

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.


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.