Module 1 · First steps in the terminal
Listing files with ls
The command you will type more than any other: what ls shows by default, how -F and -l reveal what each entry is, and how to read a long listing.
What you will learn
- List the current directory and any other directory by path.
- Tell files from directories with `ls -F` and `ls -l`.
- Read the columns of a long listing.
ls lists the contents of a directory. With no argument it lists the one you are standing in; with a path it lists that one instead. By default it prints names only, sorted alphabetically, in columns if they fit. That bare output hides a lot — you cannot tell a directory from a file, or a 2-byte file from a 2-gigabyte one — so most of the time you will add an option or two.
Marking what each entry is: -F
~% ls /
bin etc lib media opt root sbin tmp var
dev init lib32 mnt proc run sys usr
~% ls -F /
bin/ etc/ lib/ media/ opt/ root/ sbin/ tmp/ var/
dev/ init* lib32@ mnt/ proc/ run/ sys/ usr/
-F appends one character to each name: / for a directory, * for an executable program, @ for a symbolic link (a pointer to another file; lesson 19), and nothing for an ordinary file. Here it tells you at a glance that init is a program and lib32 is a link. If you just want the slashes, -p adds only those.
The long listing: -l
~% ls -l /root/lab/l4
total 4
drwxr-xr-x 2 root root 60 Oct 4 18:49 box
-rw-r--r-- 1 root root 18 Oct 4 18:49 notes.txt
-rw-r--r-- 1 root root 0 Oct 4 18:49 empty.txt
-l (lowercase L, for *long*) prints one line per entry with seven columns. Read them left to right:
| Column | Example | Meaning |
|---|---|---|
| Type and permissions | drwxr-xr-x | First character: d directory, - regular file, l link. The other nine are permissions (module 5). |
| Links | 2 | How many names point at this entry. Ignore for now. |
| Owner | root | The user who owns it. |
| Group | root | The group that owns it. |
| Size | 18 | In bytes. For directories, the size of the directory entry itself, not of its contents. |
| Date | Oct 4 18:49 | Last modification. Older than six months: the year replaces the time. |
| Name | notes.txt | The entry's name. |
The total 4 line at the top is the number of disk blocks the listed files occupy. It is rarely useful; skip it. Two facts from the table are worth repeating because they trip people up for years: the size of a directory is not the size of what is inside it (du, in lesson 64, measures that), and the first character of the line is the fastest way to know what you are looking at.
Several paths, and one directory itself
ls accepts several arguments and labels each group: ls /bin /sbin prints /bin: followed by its contents, then /sbin:. If you give it a file instead of a directory, it just prints that file's line — handy with -l to check a single file. And when you want to see a directory's *own* line (its permissions and date) rather than its contents, add -d: ls -ld /root.
Commands in this lesson
| Command | What it does |
|---|---|
ls | Names in the current directory. |
ls PATH... | Contents of one or more directories, each with a header. |
ls -F | Append `/` to directories, `*` to programs, `@` to links. |
ls -l | Long listing: type, permissions, owner, size, date. |
ls -d DIR | The directory's own line, not its contents. |
Quiz
In `ls -F` output, what does a trailing `/` mean?
- The entry is a directory
- The entry is executable
- The entry is hidden
A line of `ls -l` starts with `d`. What is it?
- A deleted file
- A directory
- A device
`ls -l` shows a directory with size 60. What does that number measure?
- The total size of everything inside it
- The size of the directory entry itself
- The number of files inside
Which command shows the permissions and date of `/etc` itself, not of the files inside?
- `ls -l /etc`
- `ls -ld /etc`
- `ls -F /etc`
Is `ls -lF` the same as `ls -F -l`?
- Yes; single-letter options can be combined and reordered
- No; `-lF` is a different option
- Only in bash
Practice
List the contents of /root/lab/l4 in long format.
The entries in /root/lab/l4/box have no extensions, and exactly one of them is a directory. Find out which with `ls -F` or `ls -l`, then create an empty file called `found.txt` inside that directory.
List the root directory `/` so that directories are marked with a trailing slash and programs with `*`.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.