$_ CLI In Depth
Learning progress 0%
lesson filesystem inodes navigation

Listing Files with ls: The Flags That Matter

5 min read Module 2: Filesystem Navigation & Inodes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:17

What ls prints by default

Run ls with no arguments and it lists the entries in the current directory, skipping names that start with a dot. When the output goes to a terminal, the names are arranged in columns, sorted alphabetically by the current locale, and often colored by file type. When the output goes to a pipe or a file, ls prints one name per line. This difference is deliberate. The program checks whether standard output is a terminal before it decides how to format the list.

The default view is for people. Its names are useful, but they do not tell you what kind of entry each name is, how large it is, or who may change it. For that you need the long format.

BASH2 lines
ls
ls -l /etc | head -n 5

Reading the long listing column by column

The flag -l asks for one line per entry with these fields in order: the file type and permission bits, the number of hard links, the owner name, the group name, the size in bytes, the modification time, and the name. A symbolic link shows the target after an arrow. The first line of output is often a total in blocks, which you can ignore for now.

ColumnMeaning
First characterFile type: - regular, d directory, l symbolic link, and others covered later in this unit
Next nine charactersPermission bits for owner, group, and others, read in threes
Second fieldHard link count. For a directory, it counts subdirectories plus two
Owner and groupNames looked up from the numeric user and group IDs stored in the inode
SizeLength in bytes. For a directory it is the size of the entry table, not the contents
TimeModification time by default, shown in your locale

Every column comes from the inode, except the name, which comes from the directory entry. The next units explain why that split matters.

Hidden files, all files, and directories

A name that starts with a dot is hidden by convention. The flag -a shows every entry, including . and ... The flag -A shows the hidden entries but leaves out . and .., which is usually what you want. When you pass a directory as an argument, ls lists its contents. The flag -d changes that: it shows the directory entry itself, which is necessary when you want to read the permissions of a directory rather than the files inside it.

BASH2 lines
ls -ld /workspace
ls -lA /workspace

Sorting and sizes

By default the entries are sorted by name. The flag -S sorts by size, largest first, and -t sorts by modification time, newest first. Add -r to reverse any order. The flag -h prints sizes in human units such as K and M, but it changes only the presentation, not the order. These options combine: ls -lhtr shows the newest entry last, which is convenient at the bottom of a long listing.

The flag -R recurses into subdirectories. For a large tree, the find command from a later module is more precise, because it gives you tests on names, sizes, and times and it does not print the directory headers that make recursive output hard to parse.

Tip

Use ls -1 when you want one name per line in a predictable format, for example to pipe into wc -l or grep. Even so, scripts should prefer globs or find over parsing ls, because file names can contain spaces and newlines.

Reading times and sizes correctly

The time column in a long listing is the modification time, and its format changes with the age of the file. Recent files show the time of day, while older files show the date and the year. The rule is intentional, and it helps a person scan a listing. It does not help a script, which should ask for an exact value with stat instead. The option --time-style in GNU ls lets you choose a fixed format, such as full ISO times, when you need the output to be stable to read.

Sizes need the same care. The number in the size column is the length of the file in bytes, which is the logical size. It is not the amount of disk space the file occupies. A file of one byte still occupies at least one block, and a file with holes may occupy much less than its logical size. The flag -s prints the allocated size in blocks, and the difference between the two numbers is a quick way to see whether a file is sparse. Combine -s with -h when you want a readable allocation.

BASH4 lines
cd /workspace
truncate -s 10M sparse.img
ls -lh sparse.img
ls -sh sparse.img

The first listing shows a logical size of ten megabytes, while the second shows how much space is actually allocated, which is close to zero because the file has no written data. This distinction explains why copying a sparse file without special options can make it much larger.

Why scripts should not parse ls

The output of ls -l is designed for a person, and its format is not a stable interface. Column widths change with the data, the time format depends on the locale and on how old the file is, and a name that contains a newline breaks any line-based parser. A safe script uses a glob such as *.txt, which the shell expands before the program runs, or it uses find -print0 with a null-delimited consumer. Use ls for looking at things and the other tools for doing things with them.

Practice in the lab

In the lab you will create a small directory, list it three ways, sort it by size, and save each result in /workspace. Notice how the hidden file appears only with -a or -A, and how the order changes with -S.

Visualizer

A directory is a list of (name, inode number) pairs. The inode holds the file itself. Hard links add names; symlinks store a path.

1 / 5
directory /workspace
nameinode
a1201
inode table
inodetypelinksdata
1201regular1"hi\n"

echo hi > a allocates inode 1201 and adds the entry a → 1201 to the directory. The link count is 1.

cd /workspace; echo hi > a; ln a b; ls -li a b; rm a; cat b; stat -c "%h links" b
Terminal lab · 3 steps · real Linux in your browser

Read ls output three ways

Produce a long listing, reveal hidden entries, and sort by size.

01
Make a long listing
The directory listing holds two files and one hidden file. Save the long listing: ``bash ls -l listing > long.txt ``
Show hint for step 1
Each line of the long listing ends with the name of one entry.
02
Show hidden entries
Save a listing that includes hidden entries and the dot entries: ``bash ls -la listing > all.txt ``
Show hint for step 2
The -a option includes names that start with a dot.
03
Sort by size
Save a long listing sorted by size, largest first: ``bash ls -lS listing > by-size.txt ``
Show hint for step 3
The -S option sorts by size with the largest entry first.
Check yourself Question 1 of 4
Concept Check Showing hidden entries recall

Which ls option shows every entry, including the names . and ..?

Recap & Key Takeaways

ls reads metadata from each inode, and the long format shows type, permissions, links, owner, group, size, and time.
-a shows hidden entries including . and .., -A shows hidden entries without those two, and -d shows a directory itself.
-S sorts by size, -t sorts by time, and -r reverses either order.
Scripts should use globs or find instead of parsing the human-readable output of ls.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue