Module 1 · First steps in the terminal
ls in depth: -l, -a, -h, sorting, hidden files
Hidden dotfiles, human-readable sizes, sorting by time or size, reversing, and the handful of combinations - ls -la, ls -lh, ls -ltr - that experienced users type without thinking.
What you will learn
- Reveal hidden files with `-a` and `-A`.
- Read sizes comfortably with `-h`.
- Sort by time (`-t`), size (`-S`) and reverse (`-r`).
Lesson 4 gave you -F and -l. This lesson adds the rest of the everyday toolkit, and the way to think about it is as three independent questions: which entries to show, what to show about each, and in what order.
Which entries: hidden files
A name that begins with a dot is hidden: ls skips it. There is no separate attribute, no flag to set — the dot *is* the hiding. The convention exists so that configuration files programs drop in your home directory (.ash_history, .profile, .gitconfig) do not clutter the view. -a shows everything, including the . and .. entries from lesson 6; -A shows everything except those two, which is usually what you want.
~% ls
~% ls -a
. .. .ash_history
~% ls -la
total 4
drwx------ 2 root root 0 Oct 4 18:49 .
drwxr-xr-x 17 root root 0 Jul 21 2018 ..
-rw------- 1 root root 11 Oct 4 18:49 .ash_history
An empty-looking home directory is rarely empty. ls -la (or ls -al; order is free) is the first thing to type when a directory seems to be missing something.
What to show: human sizes
-l gives sizes in bytes, which stops being readable around five digits. -h (*human-readable*) rewrites them with units: 5120 becomes 5.0K, 592212 becomes 578.3K, and larger files get M and G. It only makes sense together with -l, so you will always see it as -lh.
What order: time, size, reverse
| Option | Order | Typical use |
|---|---|---|
| (none) | Alphabetical | Finding a name you know. |
-t | Newest first | "What changed just now?" |
-S | Largest first | "What is eating the disk?" |
-r | Reverse whichever order is active | -ltr: newest last, right above the prompt. |
-X | By extension | Grouping .txt, .log, .csv. |
~% ls -lhS lab/l9/data
total 20
-rw-r--r-- 1 root root 8.0K Oct 4 18:49 archive.bin
-rw-r--r-- 1 root root 1.5K Oct 4 18:49 report.txt
-rw-r--r-- 1 root root 120 Oct 4 18:49 notes.txt
-rw-r--r-- 1 root root 0 Jan 1 2020 old.txt
~% ls -ltr lab/l9/data
total 20
-rw-r--r-- 1 root root 0 Jan 1 2020 old.txt
-rw-r--r-- 1 root root 120 Oct 4 18:49 notes.txt
-rw-r--r-- 1 root root 1.5K Oct 4 18:49 report.txt
-rw-r--r-- 1 root root 8.0K Oct 4 18:49 archive.bin
ls -ltr deserves a special mention. In a directory with hundreds of log files, the ones you care about are the newest, and with -ltr they appear at the bottom, right above your prompt, with no scrolling. Many administrators have this as a muscle memory and an alias.
The combinations worth memorising
ls -la— everything, with details. The default "show me this directory properly".ls -lh— details with readable sizes.ls -ltr— details, newest at the bottom.ls -lS— details, biggest first.ls -1(digit one) — one name per line; the shape other commands like to consume, from module 3 on.
Commands in this lesson
| Command | What it does |
|---|---|
ls -a / ls -A | Include hidden entries (with / without `.` and `..`). |
ls -lh | Long listing with K/M/G sizes. |
ls -lt | Newest first. |
ls -ltr | Newest last. |
ls -lS | Largest first. |
ls -1 | One name per line. |
Quiz
What makes a file hidden from plain `ls`?
- A hidden attribute set by `chmod`
- Its name starts with a dot
- It is owned by root
Which command lists a directory with the most recently modified file at the bottom?
- `ls -lt`
- `ls -ltr`
- `ls -lS`
What does `-h` do in `ls -lh`?
- Shows hidden files
- Prints sizes with K, M, G units
- Prints help
What is the difference between `ls -a` and `ls -A`?
- `-A` also shows `.` and `..`; `-a` does not
- `-a` also shows `.` and `..`; `-A` leaves them out
- They are identical
`ls -S` and `ls -s` are…
- The same option
- Different: `-S` sorts by size, `-s` shows block counts
- Both invalid
Practice
/root/lab/l9 has three directories: alpha, beta and gamma. Exactly one of them contains a hidden file. Find it with `ls -a` and create an empty file called `found.txt` inside that directory.
List /root/lab/l9/data in long format, largest file first, with human-readable sizes.
List /root/lab/l9/data in long format with the newest file first.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.