CIS120Linux Fundementals
The Less Command
Lesson on the less
Command in Linux
Introduction to less
In Linux, less
is a command-line utility that allows users to view the contents of a file interactively. It is an improved version of the older more
command and provides features such as scrolling both forwards and backwards through text files, searching within files, and navigating large files efficiently.
Basic Usage
To use less
, simply type less
followed by the name of the file you want to view:
less filename.txt
This opens filename.txt
in the less
viewer, where you can navigate using the following keys:
Navigation
Here are the navigation keys you can use while viewing a file with less
:
Key Combination | Description |
---|---|
Up/Down Arrows or j/k | Scroll up and down one line at a time. |
Page Up/Page Down or [SPACE]/[b] | Scroll up and down one screen at a time. |
G | Go to the end of the file. |
1G or gg | Go to the beginning of the file. |
/pattern | Search forward for a pattern (n for next occurrence). |
?pattern | Search backward for a pattern (N for previous occurrence). |
Examples
Viewing a File:
less example.log
Navigating:
- Scroll down using j or the down arrow key.
- Scroll up using k or the up arrow key.
- Jump to the end of the file with G.
Searching:
- Search for the word "error" in the file:
/error
- Search for the word "error" in the file:
Common Options
Here are some common options you can use with less
:
Option | Description |
---|---|
-N |
Display line numbers. |
-i |
Ignore case in searches. |
-S |
Truncate long lines (useful for wide text files). |
-F |
Quit automatically if entire file can be displayed. |
+<line_number> |
Start viewing the file from a specific line number. |