Read

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

OptionOrderTypical use
(none)AlphabeticalFinding a name you know.
-tNewest first"What changed just now?"
-SLargest first"What is eating the disk?"
-rReverse whichever order is active-ltr: newest last, right above the prompt.
-XBy extensionGrouping .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

CommandWhat it does
ls -a / ls -AInclude hidden entries (with / without `.` and `..`).
ls -lhLong listing with K/M/G sizes.
ls -ltNewest first.
ls -ltrNewest last.
ls -lSLargest first.
ls -1One name per line.

Quiz

  1. 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
  2. Which command lists a directory with the most recently modified file at the bottom?

    • `ls -lt`
    • `ls -ltr`
    • `ls -lS`
  3. What does `-h` do in `ls -lh`?

    • Shows hidden files
    • Prints sizes with K, M, G units
    • Prints help
  4. 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
  5. `ls -S` and `ls -s` are…

    • The same option
    • Different: `-S` sorts by size, `-s` shows block counts
    • Both invalid

Practice

  1. /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.

  2. List /root/lab/l9/data in long format, largest file first, with human-readable sizes.

  3. 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.