less command
less
- file perusal filter for crt viewing
The less
command is a powerful and efficient tool in Linux for viewing the contents of files, particularly large ones. Unlike some text editors or viewers that load an entire file into memory, less
displays content one page at a time, making it fast and resource-friendly.
Usage: less [OPTION]... [FILE]...
OPTION
: Flags which enhances theless
abilities.FILE
: The file you want to view.
Examples
-
Opening a File and Navigating
To view a file, simply pass its name to
less
.$ less logfile.txt
$ less -N logfile.txt-
Opens
logfile.txt
in theless
viewer. -
The first page of the file is displayed, with a
:
prompt at the bottom. -
-N
option displayslogfile.txt
with line numbers on the left. -
less
uses keyboard commands to move around. Here are the essentials:Key Action Space
orf
Scroll down one page b
Scroll up one page Down Arrow
Move down one line Up Arrow
Move up one line g
Go to the start of the file G
Go to the end of the file q
Quit less
-N
Show line numbers -i
Make searches case-insensitive -S
Chop long lines (don’t wrap) -F
Quit if the file fits on one screen -X
Don’t clear the screen on exit
-
-
Searching in a File
Search for text using the
/
key followed by your pattern.$ /pattern
- Type
/error
and pressEnter
to find the next occurrence of "error". - Press
n
to jump to the next match,N
for the previous match.
- Type
-
Jumping to a Specific Line
Go to a specific line number by typing it and pressing
g
.$ 50g
- Jumps to line 50 in the file.
-
Viewing Multiple Files
You can open multiple files and switch between them.
$ less file1.txt file2.txt
- Opens
file1.txt
first. - Use
:n
to switch to the next file (file2.txt
). - Use
:p
to go back to the previous file.
- Opens
-
Real-Time Updates
Use
less +F
to watch a file as it grows (liketail -f
).$ less +F logfile.txt
- Shows the end of
logfile.txt
and updates as new lines are added. - Press
Ctrl+C
to stop following and enter normalless
mode, thenq
to quit
- Shows the end of
-
Large Files: Use
less
instead ofcat
for big files to avoid terminal overload. -
Search Power: Combine
/
withn
/N
to navigate logs or code efficiently. -
Pipe Input: View command output with
less
(e.g.,ls -l | less
). -
Customize: Set options in the
LESS
environment variable (e.g.,export LESS="-N -i"
in.bashrc
). -
Using
less
with PipesYou can pipe command outputs to
less
for paginated viewing. For instance, to browse a long directory listing:$ ls -l | less
This lets you scroll through the output one page at a time instead of it flooding your terminal.