Cheat Sheet #day53 - less
less
Command Cheatsheet
The less
command in Unix/Linux is a terminal pager program used to view (but not modify) the contents of a file one screen at a time. It allows for both forward and backward navigation through the file.
Basic Syntax
less [OPTIONS] [FILE]
Navigation Commands
Space
orf
: Move forward one screen.b
: Move backward one screen.Enter
: Move forward one line.y
: Move backward one line.d
: Move forward half a screen.u
: Move backward half a screen.g
: Go to the beginning of the file.G
: Go to the end of the file.ng
: Go to line numbern
./pattern
: Search forward forpattern
.?pattern
: Search backward forpattern
.n
: Repeat previous search (forward).N
: Repeat previous search (backward).
Options
-N
: Display line numbers.-S
: Chop long lines (don't wrap).-X
: Disable termcap initialization/deinitialization (useful in scripts).-F
: Automatically exit if the file fits on one screen.-R
: Display raw control characters.-i
: Ignore case in searches.-m
: Display a more verbose prompt.
Useful Commands
h
: Display help screen.q
: Quitless
.&pattern
: Display only lines matching the pattern.m<letter>
: Mark the current position with<letter>
.'letter
: Return to a previously marked position.=
,Ctrl+g
: Display the current file name, line number, and percentage through the file.F
: Follow the end of the file as it grows (liketail -f
).
Examples and Use Cases
View a File
less filename.txt
- Opens
filename.txt
for viewing.
View with Line Numbers
less -N filename.txt
- Opens
filename.txt
with line numbers displayed.
Search for a Pattern
less filename.txt
- Inside
less
, type/pattern
and pressEnter
to search forpattern
.
Ignore Case in Searches
less -i filename.txt
- Opens
filename.txt
and ignores case in searches.
Follow File Growth
less +F filename.txt
- Opens
filename.txt
and follows the end of the file as it grows.
Display Long Lines Without Wrapping
less -S filename.txt
- Opens
filename.txt
and does not wrap long lines.
View Command Output
command | less
- Pipes the output of
command
toless
for easier viewing.
View Multiple Files
less file1.txt file2.txt
- Opens
file1.txt
, and after quitting,file2.txt
will be opened.
Jump to a Line Number
less filename.txt
- Inside
less
, type50g
to jump to line 50.
This cheatsheet provides a quick reference to the most common usages and options for the less
command. For more detailed information, you can always refer to the less
man page by typing man less
in your terminal.
Subscribe to my newsletter
Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by