3

When I hit tab on cd ~, I see a lost of directories starting with _ so something like _dirName which are not see when I list ls ~. I also see myUsername, root, daemon, nobody, Guest directories. What are all these and what is the difference when using cd ~/ which seem to list the files that are listed by ls ~.

Also, is there a way to ls command to list those directories?

2
  • Just to clarify, are you typing ls ~ then Tab with no whitespace or just ls ~ then Return?
    – Allan
    May 27 at 21:17
  • @Allan I was typing ls ~ then Return.
    – darthV
    May 27 at 21:30

2 Answers 2

3

This falls under the concept of parameter expansion in Bash and filename expansion in Zsh (essentially the same thing). Basically, there are two conditions:

  • the tilde as the beginning part of a word
  • the tilde as a character by itself

If used as the latter, it refers to the HOME variable. When you type ls ~ followed by ⏎ Return with no whitespace, you’re listing the directory ~ expands to; typically HOME.

When you type a cd ~ followed by a Tab (assuming no whitespace in between), it treats it as part of the filename. Zsh is trying to “guess” what you could want; it’s no longer treating it as a singular character. Since it has no clue, it’s giving you what it “thinks” could be possibilities.

To focus it on the HOME directory (assuming that’s what you want) use a /. Exampe: cd ~/ then Tab. That will delineate between expanding ~ as the HOME variable or ~ as part of the filename.

In the end, ls ~ isn’t “hiding” directories from you (to see hidden files use ls -a). What you’re seeing is Zsh’s guesses from the builtin Tab completion function

3
  • Why is that the results are same irrespective of my current working directory when using cd ~ or ls ~ and Tab?
    – darthV
    May 27 at 22:30
  • 4
    Zsh's "guesses" (as you call them) are valid completions and they do refer to real directories.
    – HTNW
    May 28 at 5:29
  • @darthV The results are the same because zsh is expanding the list of other users’ home directories. It’s not “doing a bad job at guessing”. See the other (more correct) answer
    – Josh
    Jun 6 at 13:52
5

~ alone expands to your own home directory. There is also a syntax ~username that expands to the home directory of a given username. So if myUsername is my username, then ~myUsername is the same as ~, and expands to /Users/myUsername. Meanwhile, ~root expands to root's home directory, which (on my system) is /var/root. Most of the other possibilities, like ~nobody, seem to go to /var/empty.

The big list of names you are seeing are when you press Tab after cd ~ is simply a list of usernames on your system. You can complete the line with any of the usernames and the resulting cd command will (try to) take you to that user's home directory. Since the directories ~root, ~nobody etc. are not in your ~, it is no surprise that ls ~ doesn't show them.

Note that it is typical for there to be many more "users" on a system than actual human users, since, for security reasons, it is often beneficial to run programs or services as their own user. Since programs can't access things their users don't have permissions for, this keeps programs from touching things they shouldn't.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .