Listing and inspecting files

Beginner · 15 min

Read ls -l column by column, show hidden files, sort by time or size, and inspect files with stat and file, including BSD vs GNU differences on macOS.

Before thisMoving around the filesystem

Learn Linux TV · 28 min · from 2:26

Listing and inspecting files

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ls lists names only. ls -l adds type and permissions, link count, owner, group, size in bytes, modification time, and name.
  • Names starting with a dot are hidden from plain ls. ls -a shows them, including . and .. themselves.
  • -t sorts by modification time, -S by size, and -r reverses either. -h prints sizes like 12K, and -d lists a directory itself instead of its contents.
  • stat prints the raw metadata for one file and file guesses a file's type from its contents. Their flags differ between macOS (BSD) and Linux (GNU).
  • ls output is for people. In a script, prefer globs or find over parsing ls.

Looking before you act

Before you copy, delete or edit something, you want to see what is there. The shell has no window of icons, so the question “what’s in this directory?” is answered by a program, ls. It is probably the command you will type most.

I built a small tree to practise on: the web-log fixture from this roadmap (logs/access.log, data/people.csv, data/users.json), plus a script, a hidden file and a symlink. Every command below was run on macOS with BSD tools, with file times set on purpose.

$ ls
data
logs
people-link
run.sh
src

Names only, alphabetical, and no .env. That is because .env is a hidden file. Anything whose name starts with a dot is skipped, a convention for configuration files, not a security feature:

$ ls -a
.
..
.env
data
logs
people-link
run.sh
src

-a shows everything. . and .. are the directory itself and its parent, which exist in every directory, as the previous lesson showed. -A leaves those two out.

Reading ls -l

The long format is where ls earns its keep:

$ ls -l
total 8
drwxr-xr-x@ 4 me  wheel  128 Nov 23 02:43 data
drwxr-xr-x@ 3 me  wheel   96 Nov 23 02:43 logs
lrwxr-xr-x@ 1 me  wheel   15 Nov 23 02:43 people-link -> data/people.csv
-rwxr-xr-x@ 1 me  wheel    8 Nov 22 12:00 run.sh
drwxr-xr-x@ 4 me  wheel  128 Nov 23 02:43 src

(me stands in for my real username.) Take run.sh column by column:

  • -rwxr-xr-x: the first character is the type. - is a file, d a directory, l a symlink. The next nine are permissions, which the permissions lesson decodes. @ is a macOS marker for extended attributes and won’t show on Linux.
  • 1: the number of hard links to the file. For directories, Linux tutorials say this counts subdirectories plus 2. On this Mac it did not: data holds two files and no subdirectories and shows 4. Don’t read meaning into the number for a directory.
  • me wheel: owner and group. macOS often shows wheel or staff; Linux usually shows a group named after the user.
  • 8: size in bytes. The total line above is disk blocks, not a file count.
  • Nov 22 12:00: modification time. If a file is older than about six months, ls swaps the time for the year.
  • run.sh: the name. For a symlink, -> shows what it points at.

-h makes sizes human-readable: ls -lh src printed 12K for a 12000-byte file and 64B for the empty directory.

Sorting and choosing what to list

By default, output is alphabetical. Two flags change the order, and -r flips either:

$ ls -lt data
-rw-r--r--@ 1 me  wheel  133 Nov 20 07:30 users.json
-rw-r--r--@ 1 me  wheel  145 Oct 15 08:00 people.csv
$ ls -lSr data
-rw-r--r--@ 1 me  wheel  133 Nov 20 07:30 users.json
-rw-r--r--@ 1 me  wheel  145 Oct 15 08:00 people.csv

-t puts newest first. -S puts largest first, so -Sr shows smallest first, which here happens to match. ls -F adds a marker: / for directories, * for executables, @ for links. ls -R recurses into subdirectories.

One more flag trips people up: -d. ls -l data lists what’s inside data. To see the directory’s own entry, which is how you check its permissions, use -d:

$ ls -ld data
drwxr-xr-x@ 4 me  wheel  128 Nov 23 02:43 data

stat, file and the BSD split

ls -l is a summary. stat shows the raw numbers. Here the two ecosystems disagree, and a copied one-liner from a Linux tutorial will fail:

$ stat -c '%n %s' run.sh
stat: illegal option -- c
usage: stat [-FLnq] [-f format | -l | -r | -s | -x] [-t timefmt] [file ...]
$ stat -f '%Sp %Su %z %N' run.sh logs/access.log
-rwxr-xr-x me 8 run.sh
-rw-r--r-- me 14362 logs/access.log

GNU stat formats with -c and %A %U %s %n. BSD stat (macOS) formats with -f and %Sp %Su %z %N. Both mean permissions, owner, size in bytes, name. I didn’t run the GNU form, since it doesn’t exist on this machine; that mapping comes from the manuals as I remember them.

file looks at a file’s contents, not its name:

$ file run.sh logs/access.log data/users.json
run.sh:          ASCII text
logs/access.log: ASCII text
data/users.json: JSON data

It recognised the JSON by content. It also reported /bin/ls as a universal Mach-O binary with three architectures, which is macOS-specific. file on the symlink printed people-link: CSV text in my run, so it followed the link, unlike ls -l, which describes the link itself.

Don’t parse ls

ls output is designed for eyes. Filenames can contain spaces or even newlines, and columns shift between systems. When a script needs a list of files, use a glob like *.log or find, the next lessons. Use ls interactively, and stat when you need a precise value.

About the video

Learn Linux TV’s “How to Use The ls Command on the Linux Command Line” was published 3 January 2023 and runs 27 minutes 35 seconds. I clipped it from 2:26 to 17:38. The description’s time codes put “What is the ls command” at 2:26, the command with its options and arguments at 4:07, -h at 6:15, hidden files at 7:10, combining -lha at 8:33, the alias that your distro probably sets up at 9:04, colour at 11:12, more options at 12:09 and -t and -r at 15:09, and the next chapter, “Jay’s favorite variation”, at 17:38. The clip skips the intro and the sponsor segment (1:03 to 2:26) and the alias-creation chapters at the end. English per title and description. I couldn’t read a transcript, so I haven’t confirmed how it explains the -l columns. It says nothing about stat or file in its chapters, so that section is mine. It is Linux-framed: its colour and alias steps may differ on a Mac, where the BSD ls needs -G for colour. I didn’t run that. Veronica Explains’ five-minute video, which replaced this as second view, covers only the basics of ls (chapters: arguments, redirecting to a file, why it still matters).

What I did not run

GNU ls and GNU stat, and ls -A. ACL output with ls -le, and file flags with ls -O, which printed - for my file. tree, exa and eza, which the video’s description alludes to.

Next

Looking is safe. Changing things is not, so the next lesson covers creating, copying, moving and removing files.

Check yourself

  1. 01In `-rwxr-xr-x 1 me wheel 8 Nov 22 12:00 run.sh`, what does the first character mean and what is the 8?

    The leading - means a regular file (d would be a directory, l a symlink). The 8 is the size in bytes, the column after the group.

  2. 02You run ls and don't see .env even though it is there. Why?

    Names that begin with a dot are hidden by default. Run ls -a to include them.

  3. 03How do you list the 3 most recently modified files in a directory?

    ls -t | head -3 sorts newest first and keeps three. ls -lt | head -4 includes the long-listing total line as well.

  4. 04stat -c '%s' file works on your Linux box and fails on your Mac. What is the macOS form?

    stat -f '%z' file. GNU stat uses -c with %s, while BSD stat uses -f with %z for the size in bytes.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark