Nine fields and a type character
◈ 10 cardsEvery field of an `ls -l` line, and how to choose between `-l`, `-ld`, `-la` and `-R` for the question you are actually asking.
One line, nine fields
ls -l is the command you will read more often than any other in this course, and reading it is a mechanical skill worth making automatic. Take one line:
-rw-r--r-- 2 ada staff 8192 Sep 1 10:00 Chapter3
Left to right, that is nine fields:
- the type character —
-here, so an ordinary file - the nine permission characters —
rw-r--r--, three triples for owner, group and others (module 4's subject) - the hard link count —
2, meaning two directory entries name this file - the owner's login name —
ada - the group name —
staff(a number appears instead if the name cannot be resolved) - the size in bytes —
8192 - the date of last modification —
Sep 1 - the time of last modification —
10:00(an older file shows the year here instead of a time) - the filename —
Chapter3
Fields 1 and 2 are printed as one ten-character run, which is why people who learn them together end up counting on their fingers. Split them in your head: the first character answers what kind of thing is this, and the next nine answer who may do what to it.
Note what is not on the line: the inode number. Add -i for that, and ls -il is the combination you will use throughout the links lesson.
Worked example — four flags against one small tree
Build a tiny directory and ask four different questions about it. The tree is:
~/assignment2/
a.c
.notes (hidden — the leading dot is the only thing that makes it so)
drafts/
old.c
ls -l ~/assignment2 — long-form listing of what is inside:
total 8
-rw-r--r-- 1 ada staff 120 Sep 1 09:40 a.c
drwxr-xr-x 2 ada staff 4096 Sep 1 09:41 drafts
Two entries, because .notes is hidden and ls omits any name beginning with a dot unless you ask.
ls -ld ~/assignment2 — one line, describing the directory itself:
drwxr-xr-x 3 ada staff 4096 Sep 1 09:41 /home/ada/assignment2
This is the flag that trips people, so read it slowly: -d says do not descend into the argument; treat it as the thing to describe. One line out, and the fields on it are the directory's own — its own permissions, its own link count of 3 (its ., its parent's entry for it, and drafts’ ..), its own size.
ls -la ~/assignment2 — long form of everything inside, hidden entries included:
total 12
drwxr-xr-x 3 ada staff 4096 Sep 1 09:41 .
drwxr-xr-x 24 ada staff 4096 Sep 1 09:39 ..
-rw-r--r-- 1 ada staff 64 Sep 1 09:42 .notes
-rw-r--r-- 1 ada staff 120 Sep 1 09:40 a.c
drwxr-xr-x 2 ada staff 4096 Sep 1 09:41 drafts
-a adds all hidden entries, and that includes . and .., which is the clearest demonstration you will get that those two are ordinary directory entries rather than shell notation. This listing is also where the link-count column starts paying off: . shows 3 because it is assignment2.
ls -R ~/assignment2 — the contents, and then the contents of every directory below:
/home/ada/assignment2:
a.c drafts
/home/ada/assignment2/drafts:
old.c
-R is recursive. It is a capital R, and the lower-case -r is a completely different flag that reverses the sort order — mixing them up is the classic exam trap and the classic 3 a.m. mistake.
Choosing the flag from the question
- What is in this directory? →
ls -l - What are this directory's own permissions / owner / link count? →
ls -ld - Is there a dot file in here? →
ls -la - What is anywhere under here? →
ls -R - Which inode is this? →
ls -i, usuallyls -il
file answers a different question
ls reports metadata from the inode; it can never tell you what the bytes are. file reads the leading bytes and classifies them — ASCII text, ELF 64-bit LSB executable, PNG image data, directory, symbolic link to 'Chapter3', broken symbolic link to 'Chapter3'. Because an extension means nothing to the kernel, file is the only honest answer to "what is this?" — and its ability to say broken symbolic link is a small preview of the next two lessons.