Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Syntax of less command in Linux
A quick example to illustrate
Using less with pipelines
Commonly Used Options in less command
Example Usage of less command in Linux
Viewing a File with Line Numbers
Case-Insensitive Search within a File
Viewing Files with ANSI Colors
Combining Multiple Options
Searching for Multiple Terms
Exiting less
Viewing Multiple Files
Monitoring Real-Time Data with less
Navigating Large Files Efficiently
Frequently Asked Questions
Can less open binary files?
How do I search for text within a file using less?
Is it possible to edit a file while using less?
Last Updated: Mar 27, 2024

Less Command in Linux

Create a resume that lands you SDE interviews at MAANG
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM


Let's dive­ into the less command in Linux. It's like discove­ring a hidden treasure that improve­s how we work with text files. This basic Linux tool le­ts you read file contents page­ by page. This makes long file re­ading easier without nee­ding a traditional editor. When you finish this piece­, you'll know the less command basics, how to use it alongside­ pipelines for bette­r workflows, and engage with its popular options. You'll also see­ pragmatic examples. 

Less Command in Linux

This article will give you the­ skills to read large amounts of text data with e­ase, bringing fun and efficiency to your Linux use­.

Syntax of less command in Linux

At its core, the less command is beautifully simple, yet it packs a punch in terms of functionality. To kick things off, let's break down the basic syntax:

less [options] file_name

Here, file_name refers to the name of the file you wish to view. The command loads the file, allowing you to scroll through it using your keyboard. Navigation is intuitive: the arrow keys let you move line by line, while Page Up and Page Down keys allow for faster scrolling by pages. Pressing q will exit the viewer, bringing you back to the command line.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

A quick example to illustrate

less myfile.txt

This command opens myfile.txt in a scrollable view. Now, imagine you have a lengthy log file or a dense script to sift through; less becomes your go-to for a quick peek without the need to open an editor, making it a perfect tool for on-the-fly file examinations.

Using less with pipelines

The true power of the less command is unlocked when combined with pipelines, a fundamental concept in Linux that allows the output of one command to serve as the input to another. This feature is particularly handy when dealing with commands that generate a lot of output, making it hard to grasp all at once.

For instance, consider the grep command, which searches for specific patterns within files. When used alone, grep might flood your terminal with lines of text. Enter less, which can tame this deluge, providing a manageable way to view the results:

grep 'search_pattern' largefile.log | less

In this example, grep searches for 'search_pattern' in largefile.log. Instead of displaying all matching lines at once, piping the output to less (| less) presents the results in a scrollable format. Now, you can leisurely navigate through the matches, making it easier to analyze large datasets or logs.

Another common use case involves the | (pipe) operator with commands like ls, which lists directory contents. For directories with many files, the list can be overwhelming. Piping ls into less offers a more digestible view:

ls -l | less

This command lists files in long format (-l), with the output fed into less. You can now scroll through your files, inspecting details like permissions, ownership, and size at your own pace.

Commonly Used Options in less command

The less command is highly versatile, offering numerous options to tailor your viewing experience. Below is an expanded table featuring a broader range of commonly used options, each designed to enhance your interaction with files in Linux:

Option Description
-N Displays line numbers alongside the text, aiding in navigation and reference.
-i Enables case-insensitive searching, treating uppercase and lowercase characters as equivalent.
-g Highlights only the last search match, reducing clutter on the screen.
-s Squeezes consecutive blank lines down to a single blank line, simplifying the display.
-R Ensures that ANSI color escape sequences are displayed in color, which is particularly useful for viewing logs with color-coded levels.
-m Displays a more verbose prompt, including percentage through the file, which aids in understanding your location within the document.
-E Exits automatically upon reaching the end of the file, useful for quickly checking file contents.
-F Automatically exits if the entire file can be displayed on the first screen, streamlining the viewing of short files.
-X Prevents less from clearing the screen upon exit, allowing you to see the last viewed page of the file after you quit less.
-? Displays help for less commands, useful for quick reference on available commands and options within less.

For instance, if you want to view a log file with ANSI color codes, line numbers for easy reference, and case-insensitive search enabled, you could use the following command:

less -N -i -R server_logs.log

This command not only makes the log file easier to read but also allows you to quickly search for text regardless of case, all while keeping the visual cues provided by color coding.

Example Usage of less command in Linux

Let's dive into some practical examples to showcase the less command in action. These examples will illustrate how less can be effectively utilized in various scenarios, making file navigation and analysis more manageable and efficient.

Viewing a File with Line Numbers

To open a file and display line numbers alongside each line, use the -N option:

less -N example.txt

This command is particularly useful for source code review or when discussing a file with others, as it allows you to reference specific lines easily.

Case-Insensitive Search within a File

If you're searching for a term but are unsure about the case (uppercase or lowercase), the -i option comes in handy:

less -i example.txt

Once inside less, press / followed by your search term and hit Enter. less will ignore case in your search, making it easier to find what you're looking for.

Viewing Files with ANSI Colors

Log files often use ANSI colors to highlight different levels of log messages. To preserve these colors, use the -R option:

less -R colored_log.log

This command ensures that the color coding is maintained, making the log file easier to read and analyze.

Combining Multiple Options

You can combine several options to tailor the viewing experience to your needs. For instance, to open a file with line numbers, case-insensitive search, and color support, you can use:

less -N -i -R example.log

Searching for Multiple Terms

Inside less, you can search for a term by pressing / followed by the term and hitting Enter. To search for a new term, simply press / again, type the new term, and press Enter. To navigate through search results, use n for the next match and N for the previous match.

Exiting less

To exit less, simply press q. This will bring you back to the command line.

Viewing Multiple Files

less also allows you to view multiple files sequentially:

less file1.txt file2.txt

Use :n to move to the next file and :p to go back to the previous file.

Monitoring Real-Time Data with less

The +F option allows you to use less similarly to tail -f, which is useful for viewing log files in real-time:

less +F growing_log.log

Press Ctrl+C to stop the real-time view and use less commands normally.

Navigating Large Files Efficiently

For large files, jumping directly to a specific line number can save time. Inside less, type 100g to jump to the 100th line or G to jump to the end of the file.

Frequently Asked Questions

Can less open binary files?

While less can open binary files, it's not designed for this purpose. Binary data may render as garbled text or non-printable characters, making it challenging to interpret. For binary files, consider using a hex editor.

How do I search for text within a file using less?

Once you've opened a file with less, type / followed by your search query and press Enter. To navigate through the matches, use n for the next match and N for the previous one.

Is it possible to edit a file while using less?

less is primarily a viewer, not an editor. However, you can press v while viewing a file to open it in your default editor, allowing you to make changes. Ensure you have an editor set, like vi or nano.


The less command stands as a testament to the elegance and efficiency of Linux utilities. It's not just about viewing files; it's about navigating through the vast sea of data with precision and ease. From basic file exploration to advanced data analysis with pipelining and searching, less equips you with the necessary tools to interact with your data in a more meaningful way. Whether you're a seasoned developer or a newcomer to the Linux environment, mastering less can significantly enhance your command-line productivity and data management capabilities. Embrace the power of less and unlock new levels of command-line proficiency in your Linux journey.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Cat Command in Linux
Next article
Sort Command in Linux
Live masterclass