2

Today I discovered that sudo ls shows hidden files (that is, those that have names starting with .) on OS X. This surprised me so much that I asked a question about this behaviour, which I still find somewhat strange and unexpected.

Turns out, this behaviour goes back to 2BSD in 1979.

Given that, now I’d like to ask the following question. Why doesn’t ls on Linux behave this way?
Was there a period of time when ls on some other kinds of *nixes had this behaviour? Are there any documents, commit messages, emails explaining who and why decided that this feature should not be copied at all or that it should be dropped if it was copied initially?

  • Technically, I don't think Linux is a direct fork of Unix. – saiarcot895 Jun 23 '15 at 21:42
  • @saiarcot895 That's irrelevant — this behavior was introduced in BSD, not in Unix™. But Linux isn't a fork of BSD either. – Gilles Jun 23 '15 at 21:45
  • Ok, I changed “Unix” to “*nix” in the question to make it even more general. I’m not really strong in the history of *nixes, but the point is that ls in coreutils and ls in FreeBSD have almost the same ancestry. – kirelagin Jun 23 '15 at 22:03
5

The POSIX standard says:

"Filenames beginning with a ( '.' ) and any associated information shall not be written out unless explicitly referenced, the -A or -a option is supplied, or an implementation-defined condition causes them to be written."

Being root is evidently not considered a condition which causes hidden files to be written by the GNU Coreutils implementation of ls that is commonly packaged in Linux distros.

There are good reasons not to have the behavior of programs influenced by global variables, like which user ID is in effect. A script developed as non-root will change behavior when run as root.

The hiding of files that begin with dot is not a security mechanism; it shouldn't be connected to security contexts. It conceals things that we normally don't want to see, like the .git directory among your .c source files or whatever. If you have read access to another user's directory, you can list their hidden files. The dot hides items whose presence is expected and uninteresting, not whose presence is intended to be secret.

Dotted directory entries other than .. and . have no special operating system status; just ls treats them specially.

I just tried Solaris 10; its ls also has no such behavior. It is not a universal "Unixism", which explains why the POSIX requirement is worded that way.

  • Nice to know that both implementations are POSIX compliant, but that’s clearly not the answer I am looking for. – kirelagin Jun 23 '15 at 22:17
  • 1
    The answer is that GNU Coreutils arent derived form 2BSD, and it's a dumb idea for ls to add "-a" to ls just because the effective user ID is root. – Kaz Jun 23 '15 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.