8

I'm using Ubuntu 16.04.

Step 1) I logged into my root user account.

Step 2) I used cd to navigate to a different user account's home directory.

Step 3) I typed ls to examine the contents of that directory.

Step 4) The contents came back as empty.

Step 5) I typed mkdir .ssh to create a directory.

Result) mkdir: cannot create directory '.ssh': File exists

Question: Why is the directory listed as empty if an .ssh folder exists inside of it?

-- update --

I logged into root because this is a test server. I'm repeatedly creating and destroying it.

DR01D
  • 350
  • 1
  • 3
  • 11

5 Answers5

35

ls by itself does not show hidden directories (hidden directories and files are ones that start with a ., such as .ssh)

Try using ls -a in the directory.

From the ls manpage:

-a, --all

do not ignore entries starting with .

As noted in the comments, "hidden" directories and files are not technically a thing, there is just code built into a lot of common tools that treat . and .. with special meaning, the result being that . is usually considered "hidden" by most tools. The reason I used this term is because it's common to hear it referred to that way.

Additionally . and .. usually have special meaning to most filesystems, indicating current directory and parent directory, respectively.

Patrick
  • 498
  • 4
    Note that "hidden directories" are not a thing per se, they are hidden because ls has special code to not show them when they start with a dot. – PlasmaHH Nov 29 '17 at 10:19
  • 2
    @PlasmaHH Well, ls and a whole bunch of other tools. But in principle you are absolutely right; then again, even on systems that trace their lineage back to MS-DOS and its file attributes, it requires special code to handle the case of "hidden" files (which there means "files with the hidden attribute set" rather than "files which have named on this particular form"). – user Nov 29 '17 at 12:05
  • 3
    @MichaelKjörling: on a lot of these systems it is however the other way round. There you normally ask the system to "give me everything" and when you want to display hidden files you say "give me everything plus the hidden stuff" or "give me all hidden stuff". So it is doing extra effort to hide files vs. doing extra effort to display hidden files too. – PlasmaHH Nov 29 '17 at 12:10
  • 12
    Fun fact, the invention of hidden files was a bug, ls was only supposed to hide . and .., so they added code to see if it started with a dot – Ferrybig Nov 29 '17 at 12:40
  • 2
    @PlasmaHH I don't know an OS where this is the case, do you? – heinrich5991 Nov 29 '17 at 13:10
  • 2
    @heinrich5991: it has been a long time, I am not sure, but I know for sure that dos int21,4e had an attribute parameter that. – PlasmaHH Nov 29 '17 at 13:17
  • 2
    In theory, there could be some weird filesystem that actually had hidden files, i.e. ones that would not be shown by readdir/getdents, but would work normally with stat or open etc. Shouldn't be too impossible to do that with e.g. FUSE. – ilkkachu Nov 29 '17 at 15:39
  • 2
    . and .. have special meaning in the filesystem itself (that is not just a feature of ls and so on). The special meaning attached to other files whose name starts with . is a feature of ls and shell globbing. – Martin Bonner supports Monica Nov 29 '17 at 17:16
  • 4
    I'm concerned about the "logged in as root" part combined with apparent unfamiliarity with *nixes. It would be very easy for OP to accidentally destroy something important. – shoover Nov 29 '17 at 17:51
  • @MartinBonner That's a good point. I should probably add that in to the answer as well. – Patrick Nov 29 '17 at 17:54
  • and both . and .. are just hard links and you could even remove those from filesystem. however they do have some specialized code on fs driver level and normal tools dont be happy if you remove either. – Sampo Sarrala Nov 29 '17 at 23:52
  • @PlasmaHH: That literally makes them a thing. If you require a file link to have hidden metadata stored on disk with, say, a bit set to 1 iff the file is "hidden" (as opposed to requiring the definition to be "name starts with .") then that's something that you have decided for yourself, not a "technical" limitation. – Lightness Races in Orbit Nov 30 '17 at 12:49
4

In your step 3, type ls -a to list the contents of directory.

File or directory whose name starts with . will be ignored by ls.

Bruce
  • 151
0

.ssh is a hidden directory, as it starts with a .

To list hidden files and directories, use ls -a instead of just ls.

Jenny D
  • 28,148
-2

"." is reserved for hidden folders/files. ls by default will not list the hidden files and directories. ("." and ".." are two default hidden directories in any directory). To view all the files inside a folder, use ls -a or ls -al (if you want more details about each file/folder.

-4

For creating the directory you need to remove the existing directory

Use rm -rf .ssh it will remove the directory

Or if already exist You can use cd .ssh