How to Use the less Command in Linux

How to Use the less Command in Linux

In `less -N app.log`, all four file lines appear with numbers in the left gutter. I value that the numbers map to file lines, not rows on your terminal screen.

Read the file a screen at a time, then move through its text without changing it. Look at what `less` puts on the terminal screen.

What less does

less is a terminal pager that displays text one screen at a time, so you can move backward or search without editing the source.

Open a log or manual page directly. For printing or joining files, the cat and more commands cover related tasks.

less can display the beginning of a large input before it has read the whole file. The status line shows the current position while the original text remains unchanged.

The first screenshot shows an open file and its name in the status line, not a page transition.

Less Command Display
less command output

less leaves the file untouched when you quit. If you need to edit it, follow these terminal file-editing steps.

What you need to use less

Start with a terminal and either a readable file or text produced by another command. less must also be available as a command in your shell.

  • A Linux terminal or shell that accepts keyboard input.
  • A file your account can read, or a command that writes text to standard output.
  • The less program installed and available to the shell.

Open and navigate a file

Pass a file name after less to open it. Replace app.log with a readable path when you run the example.

printf '%s\n' 'INFO startup complete' 'WARN disk usage high' 'ERROR service unavailable' 'INFO retry scheduled' > app.log
less app.log

On a file that fits on screen, less shows (END) but stays open. Longer files use the same pager, with movement keys bringing other lines into view.

Press q to close the pager and return to the shell. less does not write changes to app.log.

To inspect command output instead of a named file, pipe it into less.

printf '%s\n' 'INFO startup complete' 'WARN disk usage high' 'ERROR service unavailable' 'INFO retry scheduled' | less

The pipe sends the printf output to less as standard input. less controls the screen, and q closes the pager after printf finishes.

These keys move through the text without leaving less.

KeyAction
Space or fMove down one screen
b or Page UpMove up one screen
j or Down ArrowMove down one line
k or Up ArrowMove up one line
gGo to the start of the file
GGo to the end of the file
hShow the less command help
qQuit less and return to the shell

Search inside the file

Forward search starts with a slash, and backward search starts with a question mark. The search prompt stays open until you submit the term.

A forward search starts from the current position. Press g first when you want to begin at the top of the file.

  1. Press / and type Sherlock to search forward.
  2. Press Enter to move to the first match.
  3. Press n for the next match or N for the previous one.

This screenshot shows the term entered at the prompt before Enter submits it.

Searching In A Text File
Searching In A Text File

After you press Enter, less moves to a match and highlights it in the file. The next screenshot shows the result state.

Matches Are Highlighted
Matches Are Highlighted

I used a forward search on app.log, and less highlighted both INFO matches. Press n for the next match and N for the previous one, or start with ? to search toward the beginning.

less -p ERROR app.log

When the search term is known in advance, -p opens at its first match. Use the slash prompt instead when you want to enter or change the term while reading.

Show line numbers and jump to a line

I opened app.log with -N and saw its four file lines numbered in the left gutter. The numbers map to the file, not the rows on your terminal screen.

less -N app.log

This screenshot shows the line-number gutter beside the file text.

Displaying Line Numbers In Less Command
Displaying line numbers in the less command

-N goes on the command line before the file name. Uppercase N typed inside less repeats the search in the opposite direction.

To jump to a line, type its number followed by G. For example, 3G opens line three, while g goes to the first line and G goes to the end.

If you need the total line count rather than a line to inspect, use our guide to count the number of lines in a file.

Follow a log as it changes

Use +F when you want less to wait at the end of a growing file and display new lines as they arrive.

less +F app.log

When less catches up to the file, it shows “Waiting for data…” and stays open. I press Ctrl+C to return to normal browsing without closing the file.

For a log you only need to stream, the tail command offers another route. Press q to leave less.

When less does not behave as expected

Check that less is available first. Then confirm the path and your read access.

What you seeWhat it meansNext action
less is unavailableThe shell cannot locate less as a command.Check that less is installed and available to the shell.
The requested file does not openThe file name or path does not point to the intended file.Check the current directory and the path you entered.
Read access is deniedYour account cannot read that file.Choose a file you can read or ask an administrator to grant access.

Use a pager to read, an editor to change

less displays the file without changing it, so use an editor when the text itself needs an edit.

When an option behaves unexpectedly, the installed manual is the next place to look.

man less

Press q to return from the manual page. The man command in Linux opens other local manuals, while the less manual lists its keys and options.

Frequently asked questions

How do I exit less?

Press q while less is open to close the pager and return to the shell.

How do I search in less?

Press /, enter a search term, and press Enter. Use n to repeat the current search direction and N to repeat it the other way, starting with ? for a backward search.

How do I show line numbers in less?

Start less with the -N option, such as less -N app.log. The displayed numbers refer to lines in the file.

How is less different from more?

Both are terminal pagers. less supports backward movement and search, so you can navigate within the file while reading.