Question about ls & Combining Options

When I am doing the following quiz,

I used ls -al command and got the following result.

I don’t know why the hard links of the the first line is 2. I think it should be the number of files and directories of the current directory, and it should be 5 right?

Is it a bug or something I understand wrong?

Thank you very much!

Technically it’s the number of hard links, check the man pages for ls whenever you’re unsure. Since directories cannot be hard-linked it’s the number of directories linked to that directory which in this case is just itself and the parent directory . and .. (try adding a new sub-directory and the number will increase).

1 Like

Hi tgrtim, thank you very much for your answer. In the picture, under ceramic folder there are three .txt file, are they hard link?

I created a folder in my own computer, which contained 2 .txt folder as well. The ls command return 4 hard link on the current directory. Why it don’t return 2 as in my question?

I’m new to study coding. I may still don’t understand what hard link mean in linux. I will check the article you attached and the man as well. Thank you!

The three files could be considered their own individual hard links. If you wanted to play around with it would help to look up inode numbers and use the -i flag for ls to see them. There’s plenty of online guidance for this if you’re curious.

Is that OSX? There are some quirks to OSX that I’m not overly familiar with so I’m afraid I don’t know.

1 Like

Okay, thank you for your help. My computer is OSX.

So I under the under the terminal in Codecademy, a txt file is not a hard link. When a folder only contents files, it will show 2 hard links under . (itself)

However, on my own Mac maybe because they create a hard link for a txt file by itself, so the directory it self also will count these txt file as hard link as well.

Is it right?

Thank you

Any single file you create is arguably a hard link on any *nix so far as I’m aware. It’s why basic files within a directory should have a link number of 1. You could create a new directory and add three files but the directory itself . typically has 2 links listed under ls -l (itself and the parent) whereas the files aren’t included in the “hard-link” count for a directory (it’s still 2 for the directory not 5).

There’s something we’re both missing here and I’d hazard a guess it’s something to do with OSX but off the top of my head I’m not sure what it is. OSX has a slightly different ls command to gnu linux so you can check the man page but I’m not sure that’s the cause of the count. If I had to guess it’s something to do with an automatic indexing or linking that OSX performs by default but that’s just a guess.

I don’t have access to OSX at the minute so I’m afraid I can’t test it. I wouldn’t worry about it overly at this point, there are arguably better ways to count the number of files/directories. If you’re determined to find out then looking up the right tools/commands to check for links would probably be the way to go.

1 Like

Thanks for your explanation. I get it now. > ,<