less
The less
command in Linux is a command-line utility used for viewing the contents of text files and output streams in a scrollable interface. It's often preferred for larger files or long streams of data because it allows the user to navigate the content efficiently without loading the entire file into memory at once. Here's an overview of the less
command and its functionalities:
Viewing Files: To view a file with
less
, simply typeless filename
. This will open the file in a scrollable interface, displaying one screen of content at a time.Navigation: Inside
less
, you can navigate through the file using various keyboard shortcuts. For example:Spacebar
orf
: Scroll forward one screen.b
: Scroll backward one screen.Up/Down Arrows
: Scroll line by line.G
: Go to the end of the file.g
: Go to the start of the file./
: Search for a string going forward.?
: Search for a string going backward.n
: Repeat the previous search (in the same direction).N
: Repeat the previous search (in the opposite direction).
Efficient File Handling: Unlike commands like
cat
,less
doesn’t load the entire file at once, which makes it more efficient, especially for large files. It reads the content as needed, which reduces load times and memory usage.Viewing Large Amounts of Data:
less
is particularly useful for viewing the output of other commands that generate a lot of data. For instance,grep 'search-term' largefile | less
allows you to view the search results page by page.Viewing Multiple Files: You can view multiple files with
less
by listing them all in the command. You can then navigate between files using:n
(next file) and:p
(previous file).Exit
less
: To exitless
, pressq
.Other Features:
less
also supports other features like marking positions within the file and returning to them, displaying line numbers, and customizing the prompt.
Last updated