Less Command Guide
The less command is a powerful and versatile utility for viewing the contents of text files on Unix-like operating systems. It allows users to navigate through files interactively, making it ideal for examining large log files, configuration files, or any text-based data without loading the entire file into memory.
Essential Less Navigation Commands
Here are some fundamental commands to navigate within the less utility:
- Go to end of file:
G - Go to start of file:
g - Next result (after searching):
n - Previous result (after searching):
N - Move to next file (if multiple files opened):
:n - Move to previous file:
:p
Less Search and Filter Commands
less provides robust searching and filtering capabilities:
- Search in file:
/(Press Enter to search) - Filter content:
<pattern>(Displays only lines matching the pattern) - Toggle search highlighting:
Alt-u
Less Editing and Following Commands
You can also edit files or follow their changes in real-time:
- To edit the current file in $EDITOR:
v - Follow (tail -f) a file after opening it:
F - To start less in follow-mode:
less +F <filename> - To disable the terminal refresh when exiting:
less -X
Less Line Wrapping and Saving
Control how lines are displayed and save output:
- Don't wrap long lines:
less -S <filename>or-Swithin less. - To save the contents to a file (Method 1 - Only works when input is a pipe):
s <filename> - To save the contents to a file (Method 2 - Works with pipes or files): Type
gor<, then| $(pipe then dollar), thencat > <filename>and press Enter.
Mastering the less command is crucial for efficient command-line work. Its ability to handle large files and provide interactive navigation makes it an indispensable tool for developers and system administrators.