CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / less

less

Browse files and output: scroll, search, filter, follow

Beginneressentialviewingpagerlogs

less is the pager you should use for anything longer than a screen: it opens multi-gigabyte logs instantly, scrolls both ways, searches forwards and backwards, filters to matching lines, and can follow a growing file like tail -f. It's also what man, git log and systemctl use to show their output.

Syntaxless [OPTIONS] [+COMMAND] FILE... COMMAND | less

Watch it run

You need to read through a big log at your own pace, jumping to the end and back.

devops@devops-lab: /var/log/app — zsh — 96×28
➜ /var/log/app
Step 1 of 4
less app.log

less fills the screen and shows the file name on the bottom line. It hasn't read the rest of the file yet, which is why big files open instantly.

Anatomy of a command

$
commandless

Open the file in the pager. Only the part on screen is read, so huge files open instantly.

How it works

less reads its input lazily: it seeks to the part of the file it needs to display and keeps a small index of where lines start, so opening a 20 GB log is instant and memory use stays small. Jumping to the end (G) only reads the end, though line numbers (-N, =) may force it to count newlines through the file. For piped input it buffers what it has read so you can still scroll back, which more can't do. It talks to the terminal through terminfo and the alternate screen, which is why the file disappears from your screen when you quit (-X keeps it). Searching (/ forwards, ? backwards) uses regular expressions and highlights matches; & hides non-matching lines. F (or +F) turns less into tail -f: it waits at the end for new data until you press Ctrl+C. The environment variable LESS sets default options, and LESSOPEN lets a preprocessor (Ubuntu's lesspipe) show .gz files, archives and PDFs as text.

Options you'll actually use

Space / bForward / back one screen (also PgDn / PgUp)
g / GGo to the first / last line (50g = line 50)
/pattern ?patternSearch forwards / backwards (regex)
n / NNext / previous match
&patternShow only lines matching pattern (& alone clears)
FFollow the end of the file like tail -f (Ctrl+C stops)
-NShow line numbers
-SChop long lines instead of wrapping
-iCase-insensitive search unless the pattern has capitals
-RShow ANSI colours (for colourised output)
-X -FDon't clear the screen; quit if it fits on one screen
qQuit

Common mistakes

vim or nano on a 5 GB log just to read it

Editors load (and may copy) the whole file into memory, which is slow and can exhaust RAM on a busy server.

less file (reads only what's on screen)
cat huge.log | less

Works, but less can no longer seek in the file, so G has to read everything through the pipe and the percentage is unknown.

less huge.log
Editing in less and expecting it to save

less is read-only. v opens the file in $VISUAL/$EDITOR at the current line.

Press v, or open the file in an editor
Leaving less +F running in a session you then forget

It keeps the file open (and a deleted log's space can't be freed while it's open).

Ctrl+C then q when done; check lsof +L1 if disk space doesn't come back

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/logs/app.log in less, jump to the end, then search backwards for the most recent ERROR.

  2. 2

    In ~/logs/access.log, show only lines containing ' 500 ' while inside less.

  3. 3

    Read the compressed ~/logs/app.log.2.gz without decompressing it to disk.

  4. 4

    Open ~/logs/access.log with line numbers and long lines chopped, starting at line 100.

Related commands

morePage through text one screen at a timetailShow the last lines of a file and follow it livegrepSearch text for lines that match a patternjournalctlQuery systemd's journal: service, boot and kernel logs
PreviousmoreNextvim
Linux / File Viewing & Editing / less

less

Browse files and output: scroll, search, filter, follow

Beginneressentialviewingpagerlogs

less is the pager you should use for anything longer than a screen: it opens multi-gigabyte logs instantly, scrolls both ways, searches forwards and backwards, filters to matching lines, and can follow a growing file like tail -f. It's also what man, git log and systemctl use to show their output.

Syntaxless [OPTIONS] [+COMMAND] FILE... COMMAND | less

Watch it run

You need to read through a big log at your own pace, jumping to the end and back.

devops@devops-lab: /var/log/app — zsh — 96×28
➜ /var/log/app
Step 1 of 4
less app.log

less fills the screen and shows the file name on the bottom line. It hasn't read the rest of the file yet, which is why big files open instantly.

Anatomy of a command

$
commandless

Open the file in the pager. Only the part on screen is read, so huge files open instantly.

How it works

less reads its input lazily: it seeks to the part of the file it needs to display and keeps a small index of where lines start, so opening a 20 GB log is instant and memory use stays small. Jumping to the end (G) only reads the end, though line numbers (-N, =) may force it to count newlines through the file. For piped input it buffers what it has read so you can still scroll back, which more can't do. It talks to the terminal through terminfo and the alternate screen, which is why the file disappears from your screen when you quit (-X keeps it). Searching (/ forwards, ? backwards) uses regular expressions and highlights matches; & hides non-matching lines. F (or +F) turns less into tail -f: it waits at the end for new data until you press Ctrl+C. The environment variable LESS sets default options, and LESSOPEN lets a preprocessor (Ubuntu's lesspipe) show .gz files, archives and PDFs as text.

Options you'll actually use

Space / bForward / back one screen (also PgDn / PgUp)
g / GGo to the first / last line (50g = line 50)
/pattern ?patternSearch forwards / backwards (regex)
n / NNext / previous match
&patternShow only lines matching pattern (& alone clears)
FFollow the end of the file like tail -f (Ctrl+C stops)
-NShow line numbers
-SChop long lines instead of wrapping
-iCase-insensitive search unless the pattern has capitals
-RShow ANSI colours (for colourised output)
-X -FDon't clear the screen; quit if it fits on one screen
qQuit

Common mistakes

vim or nano on a 5 GB log just to read it

Editors load (and may copy) the whole file into memory, which is slow and can exhaust RAM on a busy server.

less file (reads only what's on screen)
cat huge.log | less

Works, but less can no longer seek in the file, so G has to read everything through the pipe and the percentage is unknown.

less huge.log
Editing in less and expecting it to save

less is read-only. v opens the file in $VISUAL/$EDITOR at the current line.

Press v, or open the file in an editor
Leaving less +F running in a session you then forget

It keeps the file open (and a deleted log's space can't be freed while it's open).

Ctrl+C then q when done; check lsof +L1 if disk space doesn't come back

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Open ~/logs/app.log in less, jump to the end, then search backwards for the most recent ERROR.

  2. 2

    In ~/logs/access.log, show only lines containing ' 500 ' while inside less.

  3. 3

    Read the compressed ~/logs/app.log.2.gz without decompressing it to disk.

  4. 4

    Open ~/logs/access.log with line numbers and long lines chopped, starting at line 100.

Related commands

morePage through text one screen at a timetailShow the last lines of a file and follow it livegrepSearch text for lines that match a patternjournalctlQuery systemd's journal: service, boot and kernel logs
PreviousmoreNextvim